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// APIRevisionClient is the apiManagement Client
30type APIRevisionClient struct {
31	BaseClient
32}
33
34// NewAPIRevisionClient creates an instance of the APIRevisionClient client.
35func NewAPIRevisionClient(subscriptionID string) APIRevisionClient {
36	return NewAPIRevisionClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewAPIRevisionClientWithBaseURI creates an instance of the APIRevisionClient client using a custom endpoint.  Use
40// this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
41func NewAPIRevisionClientWithBaseURI(baseURI string, subscriptionID string) APIRevisionClient {
42	return APIRevisionClient{NewWithBaseURI(baseURI, subscriptionID)}
43}
44
45// ListByService lists all revisions of an API.
46// Parameters:
47// resourceGroupName - the name of the resource group.
48// serviceName - the name of the API Management service.
49// apiid - API identifier. Must be unique in the current API Management service instance.
50// filter - |   Field     |     Usage     |     Supported operators     |     Supported functions
51// |</br>|-------------|-------------|-------------|-------------|</br>| apiRevision | filter | ge, le, eq, ne,
52// gt, lt | substringof, contains, startswith, endswith | </br>
53// top - number of records to return.
54// skip - number of records to skip.
55func (client APIRevisionClient) ListByService(ctx context.Context, resourceGroupName string, serviceName string, apiid string, filter string, top *int32, skip *int32) (result APIRevisionCollectionPage, err error) {
56	if tracing.IsEnabled() {
57		ctx = tracing.StartSpan(ctx, fqdn+"/APIRevisionClient.ListByService")
58		defer func() {
59			sc := -1
60			if result.arc.Response.Response != nil {
61				sc = result.arc.Response.Response.StatusCode
62			}
63			tracing.EndSpan(ctx, sc, err)
64		}()
65	}
66	if err := validation.Validate([]validation.Validation{
67		{TargetValue: serviceName,
68			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
69				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
70				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, 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}}},
74		{TargetValue: top,
75			Constraints: []validation.Constraint{{Target: "top", Name: validation.Null, Rule: false,
76				Chain: []validation.Constraint{{Target: "top", Name: validation.InclusiveMinimum, Rule: int64(1), Chain: nil}}}}},
77		{TargetValue: skip,
78			Constraints: []validation.Constraint{{Target: "skip", Name: validation.Null, Rule: false,
79				Chain: []validation.Constraint{{Target: "skip", Name: validation.InclusiveMinimum, Rule: int64(0), Chain: nil}}}}}}); err != nil {
80		return result, validation.NewError("apimanagement.APIRevisionClient", "ListByService", err.Error())
81	}
82
83	result.fn = client.listByServiceNextResults
84	req, err := client.ListByServicePreparer(ctx, resourceGroupName, serviceName, apiid, filter, top, skip)
85	if err != nil {
86		err = autorest.NewErrorWithError(err, "apimanagement.APIRevisionClient", "ListByService", nil, "Failure preparing request")
87		return
88	}
89
90	resp, err := client.ListByServiceSender(req)
91	if err != nil {
92		result.arc.Response = autorest.Response{Response: resp}
93		err = autorest.NewErrorWithError(err, "apimanagement.APIRevisionClient", "ListByService", resp, "Failure sending request")
94		return
95	}
96
97	result.arc, err = client.ListByServiceResponder(resp)
98	if err != nil {
99		err = autorest.NewErrorWithError(err, "apimanagement.APIRevisionClient", "ListByService", resp, "Failure responding to request")
100		return
101	}
102	if result.arc.hasNextLink() && result.arc.IsEmpty() {
103		err = result.NextWithContext(ctx)
104		return
105	}
106
107	return
108}
109
110// ListByServicePreparer prepares the ListByService request.
111func (client APIRevisionClient) ListByServicePreparer(ctx context.Context, resourceGroupName string, serviceName string, apiid string, filter string, top *int32, skip *int32) (*http.Request, error) {
112	pathParameters := map[string]interface{}{
113		"apiId":             autorest.Encode("path", apiid),
114		"resourceGroupName": autorest.Encode("path", resourceGroupName),
115		"serviceName":       autorest.Encode("path", serviceName),
116		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
117	}
118
119	const APIVersion = "2019-12-01-preview"
120	queryParameters := map[string]interface{}{
121		"api-version": APIVersion,
122	}
123	if len(filter) > 0 {
124		queryParameters["$filter"] = autorest.Encode("query", filter)
125	}
126	if top != nil {
127		queryParameters["$top"] = autorest.Encode("query", *top)
128	}
129	if skip != nil {
130		queryParameters["$skip"] = autorest.Encode("query", *skip)
131	}
132
133	preparer := autorest.CreatePreparer(
134		autorest.AsGet(),
135		autorest.WithBaseURL(client.BaseURI),
136		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}/revisions", pathParameters),
137		autorest.WithQueryParameters(queryParameters))
138	return preparer.Prepare((&http.Request{}).WithContext(ctx))
139}
140
141// ListByServiceSender sends the ListByService request. The method will close the
142// http.Response Body if it receives an error.
143func (client APIRevisionClient) ListByServiceSender(req *http.Request) (*http.Response, error) {
144	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
145}
146
147// ListByServiceResponder handles the response to the ListByService request. The method always
148// closes the http.Response Body.
149func (client APIRevisionClient) ListByServiceResponder(resp *http.Response) (result APIRevisionCollection, err error) {
150	err = autorest.Respond(
151		resp,
152		azure.WithErrorUnlessStatusCode(http.StatusOK),
153		autorest.ByUnmarshallingJSON(&result),
154		autorest.ByClosing())
155	result.Response = autorest.Response{Response: resp}
156	return
157}
158
159// listByServiceNextResults retrieves the next set of results, if any.
160func (client APIRevisionClient) listByServiceNextResults(ctx context.Context, lastResults APIRevisionCollection) (result APIRevisionCollection, err error) {
161	req, err := lastResults.aPIRevisionCollectionPreparer(ctx)
162	if err != nil {
163		return result, autorest.NewErrorWithError(err, "apimanagement.APIRevisionClient", "listByServiceNextResults", nil, "Failure preparing next results request")
164	}
165	if req == nil {
166		return
167	}
168	resp, err := client.ListByServiceSender(req)
169	if err != nil {
170		result.Response = autorest.Response{Response: resp}
171		return result, autorest.NewErrorWithError(err, "apimanagement.APIRevisionClient", "listByServiceNextResults", resp, "Failure sending next results request")
172	}
173	result, err = client.ListByServiceResponder(resp)
174	if err != nil {
175		err = autorest.NewErrorWithError(err, "apimanagement.APIRevisionClient", "listByServiceNextResults", resp, "Failure responding to next results request")
176	}
177	return
178}
179
180// ListByServiceComplete enumerates all values, automatically crossing page boundaries as required.
181func (client APIRevisionClient) ListByServiceComplete(ctx context.Context, resourceGroupName string, serviceName string, apiid string, filter string, top *int32, skip *int32) (result APIRevisionCollectionIterator, err error) {
182	if tracing.IsEnabled() {
183		ctx = tracing.StartSpan(ctx, fqdn+"/APIRevisionClient.ListByService")
184		defer func() {
185			sc := -1
186			if result.Response().Response.Response != nil {
187				sc = result.page.Response().Response.Response.StatusCode
188			}
189			tracing.EndSpan(ctx, sc, err)
190		}()
191	}
192	result.page, err = client.ListByService(ctx, resourceGroupName, serviceName, apiid, filter, top, skip)
193	return
194}
195