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	"strconv"
19	"strings"
20)
21
22// APIOperationClient contains the methods for the APIOperation group.
23// Don't use this type directly, use NewAPIOperationClient() instead.
24type APIOperationClient struct {
25	con            *armcore.Connection
26	subscriptionID string
27}
28
29// NewAPIOperationClient creates a new instance of APIOperationClient with the specified values.
30func NewAPIOperationClient(con *armcore.Connection, subscriptionID string) *APIOperationClient {
31	return &APIOperationClient{con: con, subscriptionID: subscriptionID}
32}
33
34// CreateOrUpdate - Creates a new operation in the API or updates an existing one.
35// If the operation fails it returns the *ErrorResponse error type.
36func (client *APIOperationClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serviceName string, apiID string, operationID string, parameters OperationContract, options *APIOperationCreateOrUpdateOptions) (APIOperationCreateOrUpdateResponse, error) {
37	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, serviceName, apiID, operationID, parameters, options)
38	if err != nil {
39		return APIOperationCreateOrUpdateResponse{}, err
40	}
41	resp, err := client.con.Pipeline().Do(req)
42	if err != nil {
43		return APIOperationCreateOrUpdateResponse{}, err
44	}
45	if !resp.HasStatusCode(http.StatusOK, http.StatusCreated) {
46		return APIOperationCreateOrUpdateResponse{}, client.createOrUpdateHandleError(resp)
47	}
48	return client.createOrUpdateHandleResponse(resp)
49}
50
51// createOrUpdateCreateRequest creates the CreateOrUpdate request.
52func (client *APIOperationClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, apiID string, operationID string, parameters OperationContract, options *APIOperationCreateOrUpdateOptions) (*azcore.Request, error) {
53	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}/operations/{operationId}"
54	if resourceGroupName == "" {
55		return nil, errors.New("parameter resourceGroupName cannot be empty")
56	}
57	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
58	if serviceName == "" {
59		return nil, errors.New("parameter serviceName cannot be empty")
60	}
61	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
62	if apiID == "" {
63		return nil, errors.New("parameter apiID cannot be empty")
64	}
65	urlPath = strings.ReplaceAll(urlPath, "{apiId}", url.PathEscape(apiID))
66	if operationID == "" {
67		return nil, errors.New("parameter operationID cannot be empty")
68	}
69	urlPath = strings.ReplaceAll(urlPath, "{operationId}", url.PathEscape(operationID))
70	if client.subscriptionID == "" {
71		return nil, errors.New("parameter client.subscriptionID cannot be empty")
72	}
73	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
74	req, err := azcore.NewRequest(ctx, http.MethodPut, azcore.JoinPaths(client.con.Endpoint(), urlPath))
75	if err != nil {
76		return nil, err
77	}
78	req.Telemetry(telemetryInfo)
79	reqQP := req.URL.Query()
80	reqQP.Set("api-version", "2020-12-01")
81	req.URL.RawQuery = reqQP.Encode()
82	if options != nil && options.IfMatch != nil {
83		req.Header.Set("If-Match", *options.IfMatch)
84	}
85	req.Header.Set("Accept", "application/json")
86	return req, req.MarshalAsJSON(parameters)
87}
88
89// createOrUpdateHandleResponse handles the CreateOrUpdate response.
90func (client *APIOperationClient) createOrUpdateHandleResponse(resp *azcore.Response) (APIOperationCreateOrUpdateResponse, error) {
91	result := APIOperationCreateOrUpdateResponse{RawResponse: resp.Response}
92	if val := resp.Header.Get("ETag"); val != "" {
93		result.ETag = &val
94	}
95	if err := resp.UnmarshalAsJSON(&result.OperationContract); err != nil {
96		return APIOperationCreateOrUpdateResponse{}, err
97	}
98	return result, nil
99}
100
101// createOrUpdateHandleError handles the CreateOrUpdate error response.
102func (client *APIOperationClient) createOrUpdateHandleError(resp *azcore.Response) error {
103	body, err := resp.Payload()
104	if err != nil {
105		return azcore.NewResponseError(err, resp.Response)
106	}
107	errType := ErrorResponse{raw: string(body)}
108	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
109		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
110	}
111	return azcore.NewResponseError(&errType, resp.Response)
112}
113
114// Delete - Deletes the specified operation in the API.
115// If the operation fails it returns the *ErrorResponse error type.
116func (client *APIOperationClient) Delete(ctx context.Context, resourceGroupName string, serviceName string, apiID string, operationID string, ifMatch string, options *APIOperationDeleteOptions) (APIOperationDeleteResponse, error) {
117	req, err := client.deleteCreateRequest(ctx, resourceGroupName, serviceName, apiID, operationID, ifMatch, options)
118	if err != nil {
119		return APIOperationDeleteResponse{}, err
120	}
121	resp, err := client.con.Pipeline().Do(req)
122	if err != nil {
123		return APIOperationDeleteResponse{}, err
124	}
125	if !resp.HasStatusCode(http.StatusOK, http.StatusNoContent) {
126		return APIOperationDeleteResponse{}, client.deleteHandleError(resp)
127	}
128	return APIOperationDeleteResponse{RawResponse: resp.Response}, nil
129}
130
131// deleteCreateRequest creates the Delete request.
132func (client *APIOperationClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, apiID string, operationID string, ifMatch string, options *APIOperationDeleteOptions) (*azcore.Request, error) {
133	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}/operations/{operationId}"
134	if resourceGroupName == "" {
135		return nil, errors.New("parameter resourceGroupName cannot be empty")
136	}
137	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
138	if serviceName == "" {
139		return nil, errors.New("parameter serviceName cannot be empty")
140	}
141	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
142	if apiID == "" {
143		return nil, errors.New("parameter apiID cannot be empty")
144	}
145	urlPath = strings.ReplaceAll(urlPath, "{apiId}", url.PathEscape(apiID))
146	if operationID == "" {
147		return nil, errors.New("parameter operationID cannot be empty")
148	}
149	urlPath = strings.ReplaceAll(urlPath, "{operationId}", url.PathEscape(operationID))
150	if client.subscriptionID == "" {
151		return nil, errors.New("parameter client.subscriptionID cannot be empty")
152	}
153	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
154	req, err := azcore.NewRequest(ctx, http.MethodDelete, azcore.JoinPaths(client.con.Endpoint(), urlPath))
155	if err != nil {
156		return nil, err
157	}
158	req.Telemetry(telemetryInfo)
159	reqQP := req.URL.Query()
160	reqQP.Set("api-version", "2020-12-01")
161	req.URL.RawQuery = reqQP.Encode()
162	req.Header.Set("If-Match", ifMatch)
163	req.Header.Set("Accept", "application/json")
164	return req, nil
165}
166
167// deleteHandleError handles the Delete error response.
168func (client *APIOperationClient) deleteHandleError(resp *azcore.Response) error {
169	body, err := resp.Payload()
170	if err != nil {
171		return azcore.NewResponseError(err, resp.Response)
172	}
173	errType := ErrorResponse{raw: string(body)}
174	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
175		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
176	}
177	return azcore.NewResponseError(&errType, resp.Response)
178}
179
180// Get - Gets the details of the API Operation specified by its identifier.
181// If the operation fails it returns the *ErrorResponse error type.
182func (client *APIOperationClient) Get(ctx context.Context, resourceGroupName string, serviceName string, apiID string, operationID string, options *APIOperationGetOptions) (APIOperationGetResponse, error) {
183	req, err := client.getCreateRequest(ctx, resourceGroupName, serviceName, apiID, operationID, options)
184	if err != nil {
185		return APIOperationGetResponse{}, err
186	}
187	resp, err := client.con.Pipeline().Do(req)
188	if err != nil {
189		return APIOperationGetResponse{}, err
190	}
191	if !resp.HasStatusCode(http.StatusOK) {
192		return APIOperationGetResponse{}, client.getHandleError(resp)
193	}
194	return client.getHandleResponse(resp)
195}
196
197// getCreateRequest creates the Get request.
198func (client *APIOperationClient) getCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, apiID string, operationID string, options *APIOperationGetOptions) (*azcore.Request, error) {
199	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}/operations/{operationId}"
200	if resourceGroupName == "" {
201		return nil, errors.New("parameter resourceGroupName cannot be empty")
202	}
203	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
204	if serviceName == "" {
205		return nil, errors.New("parameter serviceName cannot be empty")
206	}
207	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
208	if apiID == "" {
209		return nil, errors.New("parameter apiID cannot be empty")
210	}
211	urlPath = strings.ReplaceAll(urlPath, "{apiId}", url.PathEscape(apiID))
212	if operationID == "" {
213		return nil, errors.New("parameter operationID cannot be empty")
214	}
215	urlPath = strings.ReplaceAll(urlPath, "{operationId}", url.PathEscape(operationID))
216	if client.subscriptionID == "" {
217		return nil, errors.New("parameter client.subscriptionID cannot be empty")
218	}
219	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
220	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
221	if err != nil {
222		return nil, err
223	}
224	req.Telemetry(telemetryInfo)
225	reqQP := req.URL.Query()
226	reqQP.Set("api-version", "2020-12-01")
227	req.URL.RawQuery = reqQP.Encode()
228	req.Header.Set("Accept", "application/json")
229	return req, nil
230}
231
232// getHandleResponse handles the Get response.
233func (client *APIOperationClient) getHandleResponse(resp *azcore.Response) (APIOperationGetResponse, error) {
234	result := APIOperationGetResponse{RawResponse: resp.Response}
235	if val := resp.Header.Get("ETag"); val != "" {
236		result.ETag = &val
237	}
238	if err := resp.UnmarshalAsJSON(&result.OperationContract); err != nil {
239		return APIOperationGetResponse{}, err
240	}
241	return result, nil
242}
243
244// getHandleError handles the Get error response.
245func (client *APIOperationClient) getHandleError(resp *azcore.Response) error {
246	body, err := resp.Payload()
247	if err != nil {
248		return azcore.NewResponseError(err, resp.Response)
249	}
250	errType := ErrorResponse{raw: string(body)}
251	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
252		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
253	}
254	return azcore.NewResponseError(&errType, resp.Response)
255}
256
257// GetEntityTag - Gets the entity state (Etag) version of the API operation specified by its identifier.
258// If the operation fails it returns the *ErrorResponse error type.
259func (client *APIOperationClient) GetEntityTag(ctx context.Context, resourceGroupName string, serviceName string, apiID string, operationID string, options *APIOperationGetEntityTagOptions) (APIOperationGetEntityTagResponse, error) {
260	req, err := client.getEntityTagCreateRequest(ctx, resourceGroupName, serviceName, apiID, operationID, options)
261	if err != nil {
262		return APIOperationGetEntityTagResponse{}, err
263	}
264	resp, err := client.con.Pipeline().Do(req)
265	if err != nil {
266		return APIOperationGetEntityTagResponse{}, err
267	}
268	return client.getEntityTagHandleResponse(resp)
269}
270
271// getEntityTagCreateRequest creates the GetEntityTag request.
272func (client *APIOperationClient) getEntityTagCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, apiID string, operationID string, options *APIOperationGetEntityTagOptions) (*azcore.Request, error) {
273	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}/operations/{operationId}"
274	if resourceGroupName == "" {
275		return nil, errors.New("parameter resourceGroupName cannot be empty")
276	}
277	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
278	if serviceName == "" {
279		return nil, errors.New("parameter serviceName cannot be empty")
280	}
281	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
282	if apiID == "" {
283		return nil, errors.New("parameter apiID cannot be empty")
284	}
285	urlPath = strings.ReplaceAll(urlPath, "{apiId}", url.PathEscape(apiID))
286	if operationID == "" {
287		return nil, errors.New("parameter operationID cannot be empty")
288	}
289	urlPath = strings.ReplaceAll(urlPath, "{operationId}", url.PathEscape(operationID))
290	if client.subscriptionID == "" {
291		return nil, errors.New("parameter client.subscriptionID cannot be empty")
292	}
293	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
294	req, err := azcore.NewRequest(ctx, http.MethodHead, azcore.JoinPaths(client.con.Endpoint(), urlPath))
295	if err != nil {
296		return nil, err
297	}
298	req.Telemetry(telemetryInfo)
299	reqQP := req.URL.Query()
300	reqQP.Set("api-version", "2020-12-01")
301	req.URL.RawQuery = reqQP.Encode()
302	req.Header.Set("Accept", "application/json")
303	return req, nil
304}
305
306// getEntityTagHandleResponse handles the GetEntityTag response.
307func (client *APIOperationClient) getEntityTagHandleResponse(resp *azcore.Response) (APIOperationGetEntityTagResponse, error) {
308	result := APIOperationGetEntityTagResponse{RawResponse: resp.Response}
309	if val := resp.Header.Get("ETag"); val != "" {
310		result.ETag = &val
311	}
312	if resp.StatusCode >= 200 && resp.StatusCode < 300 {
313		result.Success = true
314	}
315	return result, nil
316}
317
318// ListByAPI - Lists a collection of the operations for the specified API.
319// If the operation fails it returns the *ErrorResponse error type.
320func (client *APIOperationClient) ListByAPI(resourceGroupName string, serviceName string, apiID string, options *APIOperationListByAPIOptions) APIOperationListByAPIPager {
321	return &apiOperationListByAPIPager{
322		client: client,
323		requester: func(ctx context.Context) (*azcore.Request, error) {
324			return client.listByAPICreateRequest(ctx, resourceGroupName, serviceName, apiID, options)
325		},
326		advancer: func(ctx context.Context, resp APIOperationListByAPIResponse) (*azcore.Request, error) {
327			return azcore.NewRequest(ctx, http.MethodGet, *resp.OperationCollection.NextLink)
328		},
329	}
330}
331
332// listByAPICreateRequest creates the ListByAPI request.
333func (client *APIOperationClient) listByAPICreateRequest(ctx context.Context, resourceGroupName string, serviceName string, apiID string, options *APIOperationListByAPIOptions) (*azcore.Request, error) {
334	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}/operations"
335	if resourceGroupName == "" {
336		return nil, errors.New("parameter resourceGroupName cannot be empty")
337	}
338	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
339	if serviceName == "" {
340		return nil, errors.New("parameter serviceName cannot be empty")
341	}
342	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
343	if apiID == "" {
344		return nil, errors.New("parameter apiID cannot be empty")
345	}
346	urlPath = strings.ReplaceAll(urlPath, "{apiId}", url.PathEscape(apiID))
347	if client.subscriptionID == "" {
348		return nil, errors.New("parameter client.subscriptionID cannot be empty")
349	}
350	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
351	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
352	if err != nil {
353		return nil, err
354	}
355	req.Telemetry(telemetryInfo)
356	reqQP := req.URL.Query()
357	if options != nil && options.Filter != nil {
358		reqQP.Set("$filter", *options.Filter)
359	}
360	if options != nil && options.Top != nil {
361		reqQP.Set("$top", strconv.FormatInt(int64(*options.Top), 10))
362	}
363	if options != nil && options.Skip != nil {
364		reqQP.Set("$skip", strconv.FormatInt(int64(*options.Skip), 10))
365	}
366	if options != nil && options.Tags != nil {
367		reqQP.Set("tags", *options.Tags)
368	}
369	reqQP.Set("api-version", "2020-12-01")
370	req.URL.RawQuery = reqQP.Encode()
371	req.Header.Set("Accept", "application/json")
372	return req, nil
373}
374
375// listByAPIHandleResponse handles the ListByAPI response.
376func (client *APIOperationClient) listByAPIHandleResponse(resp *azcore.Response) (APIOperationListByAPIResponse, error) {
377	result := APIOperationListByAPIResponse{RawResponse: resp.Response}
378	if err := resp.UnmarshalAsJSON(&result.OperationCollection); err != nil {
379		return APIOperationListByAPIResponse{}, err
380	}
381	return result, nil
382}
383
384// listByAPIHandleError handles the ListByAPI error response.
385func (client *APIOperationClient) listByAPIHandleError(resp *azcore.Response) error {
386	body, err := resp.Payload()
387	if err != nil {
388		return azcore.NewResponseError(err, resp.Response)
389	}
390	errType := ErrorResponse{raw: string(body)}
391	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
392		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
393	}
394	return azcore.NewResponseError(&errType, resp.Response)
395}
396
397// Update - Updates the details of the operation in the API specified by its identifier.
398// If the operation fails it returns the *ErrorResponse error type.
399func (client *APIOperationClient) Update(ctx context.Context, resourceGroupName string, serviceName string, apiID string, operationID string, ifMatch string, parameters OperationUpdateContract, options *APIOperationUpdateOptions) (APIOperationUpdateResponse, error) {
400	req, err := client.updateCreateRequest(ctx, resourceGroupName, serviceName, apiID, operationID, ifMatch, parameters, options)
401	if err != nil {
402		return APIOperationUpdateResponse{}, err
403	}
404	resp, err := client.con.Pipeline().Do(req)
405	if err != nil {
406		return APIOperationUpdateResponse{}, err
407	}
408	if !resp.HasStatusCode(http.StatusOK) {
409		return APIOperationUpdateResponse{}, client.updateHandleError(resp)
410	}
411	return client.updateHandleResponse(resp)
412}
413
414// updateCreateRequest creates the Update request.
415func (client *APIOperationClient) updateCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, apiID string, operationID string, ifMatch string, parameters OperationUpdateContract, options *APIOperationUpdateOptions) (*azcore.Request, error) {
416	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}/operations/{operationId}"
417	if resourceGroupName == "" {
418		return nil, errors.New("parameter resourceGroupName cannot be empty")
419	}
420	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
421	if serviceName == "" {
422		return nil, errors.New("parameter serviceName cannot be empty")
423	}
424	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
425	if apiID == "" {
426		return nil, errors.New("parameter apiID cannot be empty")
427	}
428	urlPath = strings.ReplaceAll(urlPath, "{apiId}", url.PathEscape(apiID))
429	if operationID == "" {
430		return nil, errors.New("parameter operationID cannot be empty")
431	}
432	urlPath = strings.ReplaceAll(urlPath, "{operationId}", url.PathEscape(operationID))
433	if client.subscriptionID == "" {
434		return nil, errors.New("parameter client.subscriptionID cannot be empty")
435	}
436	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
437	req, err := azcore.NewRequest(ctx, http.MethodPatch, azcore.JoinPaths(client.con.Endpoint(), urlPath))
438	if err != nil {
439		return nil, err
440	}
441	req.Telemetry(telemetryInfo)
442	reqQP := req.URL.Query()
443	reqQP.Set("api-version", "2020-12-01")
444	req.URL.RawQuery = reqQP.Encode()
445	req.Header.Set("If-Match", ifMatch)
446	req.Header.Set("Accept", "application/json")
447	return req, req.MarshalAsJSON(parameters)
448}
449
450// updateHandleResponse handles the Update response.
451func (client *APIOperationClient) updateHandleResponse(resp *azcore.Response) (APIOperationUpdateResponse, error) {
452	result := APIOperationUpdateResponse{RawResponse: resp.Response}
453	if val := resp.Header.Get("ETag"); val != "" {
454		result.ETag = &val
455	}
456	if err := resp.UnmarshalAsJSON(&result.OperationContract); err != nil {
457		return APIOperationUpdateResponse{}, err
458	}
459	return result, nil
460}
461
462// updateHandleError handles the Update error response.
463func (client *APIOperationClient) updateHandleError(resp *azcore.Response) error {
464	body, err := resp.Payload()
465	if err != nil {
466		return azcore.NewResponseError(err, resp.Response)
467	}
468	errType := ErrorResponse{raw: string(body)}
469	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
470		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
471	}
472	return azcore.NewResponseError(&errType, resp.Response)
473}
474