1// +build go1.13
2
3// Copyright (c) Microsoft Corporation. All rights reserved.
4// Licensed under the MIT License. See License.txt in the project root for license information.
5// Code generated by Microsoft (R) AutoRest Code Generator.
6// Changes may cause incorrect behavior and will be lost if the code is regenerated.
7
8package armapimanagement
9
10import (
11	"context"
12	"errors"
13	"fmt"
14	"github.com/Azure/azure-sdk-for-go/sdk/armcore"
15	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
16	"net/http"
17	"net/url"
18	"strings"
19)
20
21// APIExportClient contains the methods for the APIExport group.
22// Don't use this type directly, use NewAPIExportClient() instead.
23type APIExportClient struct {
24	con            *armcore.Connection
25	subscriptionID string
26}
27
28// NewAPIExportClient creates a new instance of APIExportClient with the specified values.
29func NewAPIExportClient(con *armcore.Connection, subscriptionID string) *APIExportClient {
30	return &APIExportClient{con: con, subscriptionID: subscriptionID}
31}
32
33// Get - Gets the details of the API specified by its identifier in the format specified to the Storage Blob with SAS Key valid for 5 minutes.
34// If the operation fails it returns the *ErrorResponse error type.
35func (client *APIExportClient) Get(ctx context.Context, resourceGroupName string, serviceName string, apiID string, formatParam ExportFormat, export ExportAPI, options *APIExportGetOptions) (APIExportGetResponse, error) {
36	req, err := client.getCreateRequest(ctx, resourceGroupName, serviceName, apiID, formatParam, export, options)
37	if err != nil {
38		return APIExportGetResponse{}, err
39	}
40	resp, err := client.con.Pipeline().Do(req)
41	if err != nil {
42		return APIExportGetResponse{}, err
43	}
44	if !resp.HasStatusCode(http.StatusOK) {
45		return APIExportGetResponse{}, client.getHandleError(resp)
46	}
47	return client.getHandleResponse(resp)
48}
49
50// getCreateRequest creates the Get request.
51func (client *APIExportClient) getCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, apiID string, formatParam ExportFormat, export ExportAPI, options *APIExportGetOptions) (*azcore.Request, error) {
52	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}"
53	if resourceGroupName == "" {
54		return nil, errors.New("parameter resourceGroupName cannot be empty")
55	}
56	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
57	if serviceName == "" {
58		return nil, errors.New("parameter serviceName cannot be empty")
59	}
60	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
61	if apiID == "" {
62		return nil, errors.New("parameter apiID cannot be empty")
63	}
64	urlPath = strings.ReplaceAll(urlPath, "{apiId}", url.PathEscape(apiID))
65	if client.subscriptionID == "" {
66		return nil, errors.New("parameter client.subscriptionID cannot be empty")
67	}
68	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
69	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
70	if err != nil {
71		return nil, err
72	}
73	req.Telemetry(telemetryInfo)
74	reqQP := req.URL.Query()
75	reqQP.Set("format", string(formatParam))
76	reqQP.Set("export", string(export))
77	reqQP.Set("api-version", "2020-12-01")
78	req.URL.RawQuery = reqQP.Encode()
79	req.Header.Set("Accept", "application/json")
80	return req, nil
81}
82
83// getHandleResponse handles the Get response.
84func (client *APIExportClient) getHandleResponse(resp *azcore.Response) (APIExportGetResponse, error) {
85	result := APIExportGetResponse{RawResponse: resp.Response}
86	if err := resp.UnmarshalAsJSON(&result.APIExportResult); err != nil {
87		return APIExportGetResponse{}, err
88	}
89	return result, nil
90}
91
92// getHandleError handles the Get error response.
93func (client *APIExportClient) getHandleError(resp *azcore.Response) error {
94	body, err := resp.Payload()
95	if err != nil {
96		return azcore.NewResponseError(err, resp.Response)
97	}
98	errType := ErrorResponse{raw: string(body)}
99	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
100		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
101	}
102	return azcore.NewResponseError(&errType, resp.Response)
103}
104