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