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// ProductSubscriptionsClient is the client for the ProductSubscriptions methods of the Apimanagement service.
30type ProductSubscriptionsClient struct {
31	BaseClient
32}
33
34// NewProductSubscriptionsClient creates an instance of the ProductSubscriptionsClient client.
35func NewProductSubscriptionsClient() ProductSubscriptionsClient {
36	return ProductSubscriptionsClient{New()}
37}
38
39// List lists the collection of subscriptions to the specified product.
40// Parameters:
41// apimBaseURL - the management endpoint of the API Management service, for example
42// https://myapimservice.management.azure-api.net.
43// productID - product identifier. Must be unique in the current API Management service instance.
44// filter - | Field        | Supported operators    | Supported functions                         |
45// |--------------|------------------------|---------------------------------------------|
46// | id           | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
47// | name         | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
48// | stateComment | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
49// | userId       | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
50// | productId    | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
51// | state        | eq                     |                                             |
52// top - number of records to return.
53// skip - number of records to skip.
54func (client ProductSubscriptionsClient) List(ctx context.Context, apimBaseURL string, productID string, filter string, top *int32, skip *int32) (result SubscriptionCollectionPage, err error) {
55	if tracing.IsEnabled() {
56		ctx = tracing.StartSpan(ctx, fqdn+"/ProductSubscriptionsClient.List")
57		defer func() {
58			sc := -1
59			if result.sc.Response.Response != nil {
60				sc = result.sc.Response.Response.StatusCode
61			}
62			tracing.EndSpan(ctx, sc, err)
63		}()
64	}
65	if err := validation.Validate([]validation.Validation{
66		{TargetValue: productID,
67			Constraints: []validation.Constraint{{Target: "productID", Name: validation.MaxLength, Rule: 256, Chain: nil},
68				{Target: "productID", Name: validation.MinLength, Rule: 1, Chain: nil},
69				{Target: "productID", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, 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, apimBaseURL, 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	}
97
98	return
99}
100
101// ListPreparer prepares the List request.
102func (client ProductSubscriptionsClient) ListPreparer(ctx context.Context, apimBaseURL string, productID string, filter string, top *int32, skip *int32) (*http.Request, error) {
103	urlParameters := map[string]interface{}{
104		"apimBaseUrl": apimBaseURL,
105	}
106
107	pathParameters := map[string]interface{}{
108		"productId": autorest.Encode("path", productID),
109	}
110
111	const APIVersion = "2017-03-01"
112	queryParameters := map[string]interface{}{
113		"api-version": APIVersion,
114	}
115	if len(filter) > 0 {
116		queryParameters["$filter"] = autorest.Encode("query", filter)
117	}
118	if top != nil {
119		queryParameters["$top"] = autorest.Encode("query", *top)
120	}
121	if skip != nil {
122		queryParameters["$skip"] = autorest.Encode("query", *skip)
123	}
124
125	preparer := autorest.CreatePreparer(
126		autorest.AsGet(),
127		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
128		autorest.WithPathParameters("/products/{productId}/subscriptions", pathParameters),
129		autorest.WithQueryParameters(queryParameters))
130	return preparer.Prepare((&http.Request{}).WithContext(ctx))
131}
132
133// ListSender sends the List request. The method will close the
134// http.Response Body if it receives an error.
135func (client ProductSubscriptionsClient) ListSender(req *http.Request) (*http.Response, error) {
136	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
137}
138
139// ListResponder handles the response to the List request. The method always
140// closes the http.Response Body.
141func (client ProductSubscriptionsClient) ListResponder(resp *http.Response) (result SubscriptionCollection, err error) {
142	err = autorest.Respond(
143		resp,
144		client.ByInspecting(),
145		azure.WithErrorUnlessStatusCode(http.StatusOK),
146		autorest.ByUnmarshallingJSON(&result),
147		autorest.ByClosing())
148	result.Response = autorest.Response{Response: resp}
149	return
150}
151
152// listNextResults retrieves the next set of results, if any.
153func (client ProductSubscriptionsClient) listNextResults(ctx context.Context, lastResults SubscriptionCollection) (result SubscriptionCollection, err error) {
154	req, err := lastResults.subscriptionCollectionPreparer(ctx)
155	if err != nil {
156		return result, autorest.NewErrorWithError(err, "apimanagement.ProductSubscriptionsClient", "listNextResults", nil, "Failure preparing next results request")
157	}
158	if req == nil {
159		return
160	}
161	resp, err := client.ListSender(req)
162	if err != nil {
163		result.Response = autorest.Response{Response: resp}
164		return result, autorest.NewErrorWithError(err, "apimanagement.ProductSubscriptionsClient", "listNextResults", resp, "Failure sending next results request")
165	}
166	result, err = client.ListResponder(resp)
167	if err != nil {
168		err = autorest.NewErrorWithError(err, "apimanagement.ProductSubscriptionsClient", "listNextResults", resp, "Failure responding to next results request")
169	}
170	return
171}
172
173// ListComplete enumerates all values, automatically crossing page boundaries as required.
174func (client ProductSubscriptionsClient) ListComplete(ctx context.Context, apimBaseURL string, productID string, filter string, top *int32, skip *int32) (result SubscriptionCollectionIterator, err error) {
175	if tracing.IsEnabled() {
176		ctx = tracing.StartSpan(ctx, fqdn+"/ProductSubscriptionsClient.List")
177		defer func() {
178			sc := -1
179			if result.Response().Response.Response != nil {
180				sc = result.page.Response().Response.Response.StatusCode
181			}
182			tracing.EndSpan(ctx, sc, err)
183		}()
184	}
185	result.page, err = client.List(ctx, apimBaseURL, productID, filter, top, skip)
186	return
187}
188