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