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