1package apimanagement
2
3// Copyright (c) Microsoft and contributors.  All rights reserved.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13//
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17// Code generated by Microsoft (R) AutoRest Code Generator.
18// Changes may cause incorrect behavior and will be lost if the code is regenerated.
19
20import (
21	"context"
22	"github.com/Azure/go-autorest/autorest"
23	"github.com/Azure/go-autorest/autorest/azure"
24	"github.com/Azure/go-autorest/autorest/validation"
25	"github.com/Azure/go-autorest/tracing"
26	"net/http"
27)
28
29// GatewayAPIClient is the apiManagement Client
30type GatewayAPIClient struct {
31	BaseClient
32}
33
34// NewGatewayAPIClient creates an instance of the GatewayAPIClient client.
35func NewGatewayAPIClient(subscriptionID string) GatewayAPIClient {
36	return NewGatewayAPIClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewGatewayAPIClientWithBaseURI creates an instance of the GatewayAPIClient client using a custom endpoint.  Use this
40// when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
41func NewGatewayAPIClientWithBaseURI(baseURI string, subscriptionID string) GatewayAPIClient {
42	return GatewayAPIClient{NewWithBaseURI(baseURI, subscriptionID)}
43}
44
45// CreateOrUpdate adds an API to the specified Gateway.
46// Parameters:
47// resourceGroupName - the name of the resource group.
48// serviceName - the name of the API Management service.
49// gatewayID - gateway entity identifier. Must be unique in the current API Management service instance. Must
50// not have value 'managed'
51// apiid - API identifier. Must be unique in the current API Management service instance.
52func (client GatewayAPIClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serviceName string, gatewayID string, apiid string, parameters *AssociationContract) (result APIContract, err error) {
53	if tracing.IsEnabled() {
54		ctx = tracing.StartSpan(ctx, fqdn+"/GatewayAPIClient.CreateOrUpdate")
55		defer func() {
56			sc := -1
57			if result.Response.Response != nil {
58				sc = result.Response.Response.StatusCode
59			}
60			tracing.EndSpan(ctx, sc, err)
61		}()
62	}
63	if err := validation.Validate([]validation.Validation{
64		{TargetValue: serviceName,
65			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
66				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
67				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
68		{TargetValue: gatewayID,
69			Constraints: []validation.Constraint{{Target: "gatewayID", Name: validation.MaxLength, Rule: 80, Chain: nil},
70				{Target: "gatewayID", Name: validation.MinLength, Rule: 1, Chain: nil}}},
71		{TargetValue: apiid,
72			Constraints: []validation.Constraint{{Target: "apiid", Name: validation.MaxLength, Rule: 80, Chain: nil},
73				{Target: "apiid", Name: validation.MinLength, Rule: 1, Chain: nil}}}}); err != nil {
74		return result, validation.NewError("apimanagement.GatewayAPIClient", "CreateOrUpdate", err.Error())
75	}
76
77	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, serviceName, gatewayID, apiid, parameters)
78	if err != nil {
79		err = autorest.NewErrorWithError(err, "apimanagement.GatewayAPIClient", "CreateOrUpdate", nil, "Failure preparing request")
80		return
81	}
82
83	resp, err := client.CreateOrUpdateSender(req)
84	if err != nil {
85		result.Response = autorest.Response{Response: resp}
86		err = autorest.NewErrorWithError(err, "apimanagement.GatewayAPIClient", "CreateOrUpdate", resp, "Failure sending request")
87		return
88	}
89
90	result, err = client.CreateOrUpdateResponder(resp)
91	if err != nil {
92		err = autorest.NewErrorWithError(err, "apimanagement.GatewayAPIClient", "CreateOrUpdate", resp, "Failure responding to request")
93		return
94	}
95
96	return
97}
98
99// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
100func (client GatewayAPIClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, serviceName string, gatewayID string, apiid string, parameters *AssociationContract) (*http.Request, error) {
101	pathParameters := map[string]interface{}{
102		"apiId":             autorest.Encode("path", apiid),
103		"gatewayId":         autorest.Encode("path", gatewayID),
104		"resourceGroupName": autorest.Encode("path", resourceGroupName),
105		"serviceName":       autorest.Encode("path", serviceName),
106		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
107	}
108
109	const APIVersion = "2019-12-01"
110	queryParameters := map[string]interface{}{
111		"api-version": APIVersion,
112	}
113
114	preparer := autorest.CreatePreparer(
115		autorest.AsContentType("application/json; charset=utf-8"),
116		autorest.AsPut(),
117		autorest.WithBaseURL(client.BaseURI),
118		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/gateways/{gatewayId}/apis/{apiId}", pathParameters),
119		autorest.WithQueryParameters(queryParameters))
120	if parameters != nil {
121		preparer = autorest.DecoratePreparer(preparer,
122			autorest.WithJSON(parameters))
123	}
124	return preparer.Prepare((&http.Request{}).WithContext(ctx))
125}
126
127// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
128// http.Response Body if it receives an error.
129func (client GatewayAPIClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
130	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
131}
132
133// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
134// closes the http.Response Body.
135func (client GatewayAPIClient) CreateOrUpdateResponder(resp *http.Response) (result APIContract, err error) {
136	err = autorest.Respond(
137		resp,
138		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
139		autorest.ByUnmarshallingJSON(&result),
140		autorest.ByClosing())
141	result.Response = autorest.Response{Response: resp}
142	return
143}
144
145// Delete deletes the specified API from the specified Gateway.
146// Parameters:
147// resourceGroupName - the name of the resource group.
148// serviceName - the name of the API Management service.
149// gatewayID - gateway entity identifier. Must be unique in the current API Management service instance. Must
150// not have value 'managed'
151// apiid - API identifier. Must be unique in the current API Management service instance.
152func (client GatewayAPIClient) Delete(ctx context.Context, resourceGroupName string, serviceName string, gatewayID string, apiid string) (result autorest.Response, err error) {
153	if tracing.IsEnabled() {
154		ctx = tracing.StartSpan(ctx, fqdn+"/GatewayAPIClient.Delete")
155		defer func() {
156			sc := -1
157			if result.Response != nil {
158				sc = result.Response.StatusCode
159			}
160			tracing.EndSpan(ctx, sc, err)
161		}()
162	}
163	if err := validation.Validate([]validation.Validation{
164		{TargetValue: serviceName,
165			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
166				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
167				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
168		{TargetValue: gatewayID,
169			Constraints: []validation.Constraint{{Target: "gatewayID", Name: validation.MaxLength, Rule: 80, Chain: nil},
170				{Target: "gatewayID", Name: validation.MinLength, Rule: 1, Chain: nil}}},
171		{TargetValue: apiid,
172			Constraints: []validation.Constraint{{Target: "apiid", Name: validation.MaxLength, Rule: 80, Chain: nil},
173				{Target: "apiid", Name: validation.MinLength, Rule: 1, Chain: nil}}}}); err != nil {
174		return result, validation.NewError("apimanagement.GatewayAPIClient", "Delete", err.Error())
175	}
176
177	req, err := client.DeletePreparer(ctx, resourceGroupName, serviceName, gatewayID, apiid)
178	if err != nil {
179		err = autorest.NewErrorWithError(err, "apimanagement.GatewayAPIClient", "Delete", nil, "Failure preparing request")
180		return
181	}
182
183	resp, err := client.DeleteSender(req)
184	if err != nil {
185		result.Response = resp
186		err = autorest.NewErrorWithError(err, "apimanagement.GatewayAPIClient", "Delete", resp, "Failure sending request")
187		return
188	}
189
190	result, err = client.DeleteResponder(resp)
191	if err != nil {
192		err = autorest.NewErrorWithError(err, "apimanagement.GatewayAPIClient", "Delete", resp, "Failure responding to request")
193		return
194	}
195
196	return
197}
198
199// DeletePreparer prepares the Delete request.
200func (client GatewayAPIClient) DeletePreparer(ctx context.Context, resourceGroupName string, serviceName string, gatewayID string, apiid string) (*http.Request, error) {
201	pathParameters := map[string]interface{}{
202		"apiId":             autorest.Encode("path", apiid),
203		"gatewayId":         autorest.Encode("path", gatewayID),
204		"resourceGroupName": autorest.Encode("path", resourceGroupName),
205		"serviceName":       autorest.Encode("path", serviceName),
206		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
207	}
208
209	const APIVersion = "2019-12-01"
210	queryParameters := map[string]interface{}{
211		"api-version": APIVersion,
212	}
213
214	preparer := autorest.CreatePreparer(
215		autorest.AsDelete(),
216		autorest.WithBaseURL(client.BaseURI),
217		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/gateways/{gatewayId}/apis/{apiId}", pathParameters),
218		autorest.WithQueryParameters(queryParameters))
219	return preparer.Prepare((&http.Request{}).WithContext(ctx))
220}
221
222// DeleteSender sends the Delete request. The method will close the
223// http.Response Body if it receives an error.
224func (client GatewayAPIClient) DeleteSender(req *http.Request) (*http.Response, error) {
225	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
226}
227
228// DeleteResponder handles the response to the Delete request. The method always
229// closes the http.Response Body.
230func (client GatewayAPIClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
231	err = autorest.Respond(
232		resp,
233		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
234		autorest.ByClosing())
235	result.Response = resp
236	return
237}
238
239// GetEntityTag checks that API entity specified by identifier is associated with the Gateway entity.
240// Parameters:
241// resourceGroupName - the name of the resource group.
242// serviceName - the name of the API Management service.
243// gatewayID - gateway entity identifier. Must be unique in the current API Management service instance. Must
244// not have value 'managed'
245// apiid - API identifier. Must be unique in the current API Management service instance.
246func (client GatewayAPIClient) GetEntityTag(ctx context.Context, resourceGroupName string, serviceName string, gatewayID string, apiid string) (result autorest.Response, err error) {
247	if tracing.IsEnabled() {
248		ctx = tracing.StartSpan(ctx, fqdn+"/GatewayAPIClient.GetEntityTag")
249		defer func() {
250			sc := -1
251			if result.Response != nil {
252				sc = result.Response.StatusCode
253			}
254			tracing.EndSpan(ctx, sc, err)
255		}()
256	}
257	if err := validation.Validate([]validation.Validation{
258		{TargetValue: serviceName,
259			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
260				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
261				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
262		{TargetValue: gatewayID,
263			Constraints: []validation.Constraint{{Target: "gatewayID", Name: validation.MaxLength, Rule: 80, Chain: nil},
264				{Target: "gatewayID", Name: validation.MinLength, Rule: 1, Chain: nil}}},
265		{TargetValue: apiid,
266			Constraints: []validation.Constraint{{Target: "apiid", Name: validation.MaxLength, Rule: 80, Chain: nil},
267				{Target: "apiid", Name: validation.MinLength, Rule: 1, Chain: nil}}}}); err != nil {
268		return result, validation.NewError("apimanagement.GatewayAPIClient", "GetEntityTag", err.Error())
269	}
270
271	req, err := client.GetEntityTagPreparer(ctx, resourceGroupName, serviceName, gatewayID, apiid)
272	if err != nil {
273		err = autorest.NewErrorWithError(err, "apimanagement.GatewayAPIClient", "GetEntityTag", nil, "Failure preparing request")
274		return
275	}
276
277	resp, err := client.GetEntityTagSender(req)
278	if err != nil {
279		result.Response = resp
280		err = autorest.NewErrorWithError(err, "apimanagement.GatewayAPIClient", "GetEntityTag", resp, "Failure sending request")
281		return
282	}
283
284	result, err = client.GetEntityTagResponder(resp)
285	if err != nil {
286		err = autorest.NewErrorWithError(err, "apimanagement.GatewayAPIClient", "GetEntityTag", resp, "Failure responding to request")
287		return
288	}
289
290	return
291}
292
293// GetEntityTagPreparer prepares the GetEntityTag request.
294func (client GatewayAPIClient) GetEntityTagPreparer(ctx context.Context, resourceGroupName string, serviceName string, gatewayID string, apiid string) (*http.Request, error) {
295	pathParameters := map[string]interface{}{
296		"apiId":             autorest.Encode("path", apiid),
297		"gatewayId":         autorest.Encode("path", gatewayID),
298		"resourceGroupName": autorest.Encode("path", resourceGroupName),
299		"serviceName":       autorest.Encode("path", serviceName),
300		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
301	}
302
303	const APIVersion = "2019-12-01"
304	queryParameters := map[string]interface{}{
305		"api-version": APIVersion,
306	}
307
308	preparer := autorest.CreatePreparer(
309		autorest.AsHead(),
310		autorest.WithBaseURL(client.BaseURI),
311		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/gateways/{gatewayId}/apis/{apiId}", pathParameters),
312		autorest.WithQueryParameters(queryParameters))
313	return preparer.Prepare((&http.Request{}).WithContext(ctx))
314}
315
316// GetEntityTagSender sends the GetEntityTag request. The method will close the
317// http.Response Body if it receives an error.
318func (client GatewayAPIClient) GetEntityTagSender(req *http.Request) (*http.Response, error) {
319	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
320}
321
322// GetEntityTagResponder handles the response to the GetEntityTag request. The method always
323// closes the http.Response Body.
324func (client GatewayAPIClient) GetEntityTagResponder(resp *http.Response) (result autorest.Response, err error) {
325	err = autorest.Respond(
326		resp,
327		azure.WithErrorUnlessStatusCode(http.StatusOK),
328		autorest.ByClosing())
329	result.Response = resp
330	return
331}
332
333// ListByService lists a collection of the APIs associated with a gateway.
334// Parameters:
335// resourceGroupName - the name of the resource group.
336// serviceName - the name of the API Management service.
337// gatewayID - gateway entity identifier. Must be unique in the current API Management service instance. Must
338// not have value 'managed'
339// top - number of records to return.
340// skip - number of records to skip.
341func (client GatewayAPIClient) ListByService(ctx context.Context, resourceGroupName string, serviceName string, gatewayID string, top *int32, skip *int32) (result APICollectionPage, err error) {
342	if tracing.IsEnabled() {
343		ctx = tracing.StartSpan(ctx, fqdn+"/GatewayAPIClient.ListByService")
344		defer func() {
345			sc := -1
346			if result.ac.Response.Response != nil {
347				sc = result.ac.Response.Response.StatusCode
348			}
349			tracing.EndSpan(ctx, sc, err)
350		}()
351	}
352	if err := validation.Validate([]validation.Validation{
353		{TargetValue: serviceName,
354			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
355				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
356				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
357		{TargetValue: gatewayID,
358			Constraints: []validation.Constraint{{Target: "gatewayID", Name: validation.MaxLength, Rule: 80, Chain: nil},
359				{Target: "gatewayID", Name: validation.MinLength, Rule: 1, Chain: nil}}},
360		{TargetValue: top,
361			Constraints: []validation.Constraint{{Target: "top", Name: validation.Null, Rule: false,
362				Chain: []validation.Constraint{{Target: "top", Name: validation.InclusiveMinimum, Rule: int64(1), Chain: nil}}}}},
363		{TargetValue: skip,
364			Constraints: []validation.Constraint{{Target: "skip", Name: validation.Null, Rule: false,
365				Chain: []validation.Constraint{{Target: "skip", Name: validation.InclusiveMinimum, Rule: int64(0), Chain: nil}}}}}}); err != nil {
366		return result, validation.NewError("apimanagement.GatewayAPIClient", "ListByService", err.Error())
367	}
368
369	result.fn = client.listByServiceNextResults
370	req, err := client.ListByServicePreparer(ctx, resourceGroupName, serviceName, gatewayID, top, skip)
371	if err != nil {
372		err = autorest.NewErrorWithError(err, "apimanagement.GatewayAPIClient", "ListByService", nil, "Failure preparing request")
373		return
374	}
375
376	resp, err := client.ListByServiceSender(req)
377	if err != nil {
378		result.ac.Response = autorest.Response{Response: resp}
379		err = autorest.NewErrorWithError(err, "apimanagement.GatewayAPIClient", "ListByService", resp, "Failure sending request")
380		return
381	}
382
383	result.ac, err = client.ListByServiceResponder(resp)
384	if err != nil {
385		err = autorest.NewErrorWithError(err, "apimanagement.GatewayAPIClient", "ListByService", resp, "Failure responding to request")
386		return
387	}
388	if result.ac.hasNextLink() && result.ac.IsEmpty() {
389		err = result.NextWithContext(ctx)
390		return
391	}
392
393	return
394}
395
396// ListByServicePreparer prepares the ListByService request.
397func (client GatewayAPIClient) ListByServicePreparer(ctx context.Context, resourceGroupName string, serviceName string, gatewayID string, top *int32, skip *int32) (*http.Request, error) {
398	pathParameters := map[string]interface{}{
399		"gatewayId":         autorest.Encode("path", gatewayID),
400		"resourceGroupName": autorest.Encode("path", resourceGroupName),
401		"serviceName":       autorest.Encode("path", serviceName),
402		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
403	}
404
405	const APIVersion = "2019-12-01"
406	queryParameters := map[string]interface{}{
407		"api-version": APIVersion,
408	}
409	if top != nil {
410		queryParameters["$top"] = autorest.Encode("query", *top)
411	}
412	if skip != nil {
413		queryParameters["$skip"] = autorest.Encode("query", *skip)
414	}
415
416	preparer := autorest.CreatePreparer(
417		autorest.AsGet(),
418		autorest.WithBaseURL(client.BaseURI),
419		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/gateways/{gatewayId}/apis", pathParameters),
420		autorest.WithQueryParameters(queryParameters))
421	return preparer.Prepare((&http.Request{}).WithContext(ctx))
422}
423
424// ListByServiceSender sends the ListByService request. The method will close the
425// http.Response Body if it receives an error.
426func (client GatewayAPIClient) ListByServiceSender(req *http.Request) (*http.Response, error) {
427	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
428}
429
430// ListByServiceResponder handles the response to the ListByService request. The method always
431// closes the http.Response Body.
432func (client GatewayAPIClient) ListByServiceResponder(resp *http.Response) (result APICollection, err error) {
433	err = autorest.Respond(
434		resp,
435		azure.WithErrorUnlessStatusCode(http.StatusOK),
436		autorest.ByUnmarshallingJSON(&result),
437		autorest.ByClosing())
438	result.Response = autorest.Response{Response: resp}
439	return
440}
441
442// listByServiceNextResults retrieves the next set of results, if any.
443func (client GatewayAPIClient) listByServiceNextResults(ctx context.Context, lastResults APICollection) (result APICollection, err error) {
444	req, err := lastResults.aPICollectionPreparer(ctx)
445	if err != nil {
446		return result, autorest.NewErrorWithError(err, "apimanagement.GatewayAPIClient", "listByServiceNextResults", nil, "Failure preparing next results request")
447	}
448	if req == nil {
449		return
450	}
451	resp, err := client.ListByServiceSender(req)
452	if err != nil {
453		result.Response = autorest.Response{Response: resp}
454		return result, autorest.NewErrorWithError(err, "apimanagement.GatewayAPIClient", "listByServiceNextResults", resp, "Failure sending next results request")
455	}
456	result, err = client.ListByServiceResponder(resp)
457	if err != nil {
458		err = autorest.NewErrorWithError(err, "apimanagement.GatewayAPIClient", "listByServiceNextResults", resp, "Failure responding to next results request")
459	}
460	return
461}
462
463// ListByServiceComplete enumerates all values, automatically crossing page boundaries as required.
464func (client GatewayAPIClient) ListByServiceComplete(ctx context.Context, resourceGroupName string, serviceName string, gatewayID string, top *int32, skip *int32) (result APICollectionIterator, err error) {
465	if tracing.IsEnabled() {
466		ctx = tracing.StartSpan(ctx, fqdn+"/GatewayAPIClient.ListByService")
467		defer func() {
468			sc := -1
469			if result.Response().Response.Response != nil {
470				sc = result.page.Response().Response.Response.StatusCode
471			}
472			tracing.EndSpan(ctx, sc, err)
473		}()
474	}
475	result.page, err = client.ListByService(ctx, resourceGroupName, serviceName, gatewayID, top, skip)
476	return
477}
478