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