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