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// ProductSubscriptionsClient is the client for the ProductSubscriptions methods of the Apimanagement service.
29type ProductSubscriptionsClient struct {
30	BaseClient
31}
32
33// NewProductSubscriptionsClient creates an instance of the ProductSubscriptionsClient client.
34func NewProductSubscriptionsClient() ProductSubscriptionsClient {
35	return ProductSubscriptionsClient{New()}
36}
37
38// List lists the collection of subscriptions to the specified product.
39// Parameters:
40// apimBaseURL - the management endpoint of the API Management service, for example
41// https://myapimservice.management.azure-api.net.
42// productID - product identifier. Must be unique in the current API Management service instance.
43// filter - | Field        | Supported operators    | Supported functions                         |
44// |--------------|------------------------|---------------------------------------------|
45// | id           | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
46// | name         | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
47// | stateComment | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
48// | userId       | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
49// | productId    | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
50// | state        | eq                     |                                             |
51// top - number of records to return.
52// skip - number of records to skip.
53func (client ProductSubscriptionsClient) List(ctx context.Context, apimBaseURL string, productID string, filter string, top *int32, skip *int32) (result SubscriptionCollectionPage, err error) {
54	if err := validation.Validate([]validation.Validation{
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: top,
60			Constraints: []validation.Constraint{{Target: "top", Name: validation.Null, Rule: false,
61				Chain: []validation.Constraint{{Target: "top", Name: validation.InclusiveMinimum, Rule: 1, Chain: nil}}}}},
62		{TargetValue: skip,
63			Constraints: []validation.Constraint{{Target: "skip", Name: validation.Null, Rule: false,
64				Chain: []validation.Constraint{{Target: "skip", Name: validation.InclusiveMinimum, Rule: 0, Chain: nil}}}}}}); err != nil {
65		return result, validation.NewError("apimanagement.ProductSubscriptionsClient", "List", err.Error())
66	}
67
68	result.fn = client.listNextResults
69	req, err := client.ListPreparer(ctx, apimBaseURL, productID, filter, top, skip)
70	if err != nil {
71		err = autorest.NewErrorWithError(err, "apimanagement.ProductSubscriptionsClient", "List", nil, "Failure preparing request")
72		return
73	}
74
75	resp, err := client.ListSender(req)
76	if err != nil {
77		result.sc.Response = autorest.Response{Response: resp}
78		err = autorest.NewErrorWithError(err, "apimanagement.ProductSubscriptionsClient", "List", resp, "Failure sending request")
79		return
80	}
81
82	result.sc, err = client.ListResponder(resp)
83	if err != nil {
84		err = autorest.NewErrorWithError(err, "apimanagement.ProductSubscriptionsClient", "List", resp, "Failure responding to request")
85	}
86
87	return
88}
89
90// ListPreparer prepares the List request.
91func (client ProductSubscriptionsClient) ListPreparer(ctx context.Context, apimBaseURL string, productID string, filter string, top *int32, skip *int32) (*http.Request, error) {
92	urlParameters := map[string]interface{}{
93		"apimBaseUrl": apimBaseURL,
94	}
95
96	pathParameters := map[string]interface{}{
97		"productId": autorest.Encode("path", productID),
98	}
99
100	const APIVersion = "2017-03-01"
101	queryParameters := map[string]interface{}{
102		"api-version": APIVersion,
103	}
104	if len(filter) > 0 {
105		queryParameters["$filter"] = autorest.Encode("query", filter)
106	}
107	if top != nil {
108		queryParameters["$top"] = autorest.Encode("query", *top)
109	}
110	if skip != nil {
111		queryParameters["$skip"] = autorest.Encode("query", *skip)
112	}
113
114	preparer := autorest.CreatePreparer(
115		autorest.AsGet(),
116		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
117		autorest.WithPathParameters("/products/{productId}/subscriptions", pathParameters),
118		autorest.WithQueryParameters(queryParameters))
119	return preparer.Prepare((&http.Request{}).WithContext(ctx))
120}
121
122// ListSender sends the List request. The method will close the
123// http.Response Body if it receives an error.
124func (client ProductSubscriptionsClient) ListSender(req *http.Request) (*http.Response, error) {
125	return autorest.SendWithSender(client, req,
126		autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
127}
128
129// ListResponder handles the response to the List request. The method always
130// closes the http.Response Body.
131func (client ProductSubscriptionsClient) ListResponder(resp *http.Response) (result SubscriptionCollection, err error) {
132	err = autorest.Respond(
133		resp,
134		client.ByInspecting(),
135		azure.WithErrorUnlessStatusCode(http.StatusOK),
136		autorest.ByUnmarshallingJSON(&result),
137		autorest.ByClosing())
138	result.Response = autorest.Response{Response: resp}
139	return
140}
141
142// listNextResults retrieves the next set of results, if any.
143func (client ProductSubscriptionsClient) listNextResults(lastResults SubscriptionCollection) (result SubscriptionCollection, err error) {
144	req, err := lastResults.subscriptionCollectionPreparer()
145	if err != nil {
146		return result, autorest.NewErrorWithError(err, "apimanagement.ProductSubscriptionsClient", "listNextResults", nil, "Failure preparing next results request")
147	}
148	if req == nil {
149		return
150	}
151	resp, err := client.ListSender(req)
152	if err != nil {
153		result.Response = autorest.Response{Response: resp}
154		return result, autorest.NewErrorWithError(err, "apimanagement.ProductSubscriptionsClient", "listNextResults", resp, "Failure sending next results request")
155	}
156	result, err = client.ListResponder(resp)
157	if err != nil {
158		err = autorest.NewErrorWithError(err, "apimanagement.ProductSubscriptionsClient", "listNextResults", resp, "Failure responding to next results request")
159	}
160	return
161}
162
163// ListComplete enumerates all values, automatically crossing page boundaries as required.
164func (client ProductSubscriptionsClient) ListComplete(ctx context.Context, apimBaseURL string, productID string, filter string, top *int32, skip *int32) (result SubscriptionCollectionIterator, err error) {
165	result.page, err = client.List(ctx, apimBaseURL, productID, filter, top, skip)
166	return
167}
168