1// +build go1.13
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// Code generated by Microsoft (R) AutoRest Code Generator.
6// Changes may cause incorrect behavior and will be lost if the code is regenerated.
7
8package armapimanagement
9
10import (
11	"context"
12	"errors"
13	"fmt"
14	"github.com/Azure/azure-sdk-for-go/sdk/armcore"
15	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
16	"net/http"
17	"net/url"
18	"strconv"
19	"strings"
20)
21
22// ProductSubscriptionsClient contains the methods for the ProductSubscriptions group.
23// Don't use this type directly, use NewProductSubscriptionsClient() instead.
24type ProductSubscriptionsClient struct {
25	con            *armcore.Connection
26	subscriptionID string
27}
28
29// NewProductSubscriptionsClient creates a new instance of ProductSubscriptionsClient with the specified values.
30func NewProductSubscriptionsClient(con *armcore.Connection, subscriptionID string) *ProductSubscriptionsClient {
31	return &ProductSubscriptionsClient{con: con, subscriptionID: subscriptionID}
32}
33
34// List - Lists the collection of subscriptions to the specified product.
35// If the operation fails it returns the *ErrorResponse error type.
36func (client *ProductSubscriptionsClient) List(resourceGroupName string, serviceName string, productID string, options *ProductSubscriptionsListOptions) ProductSubscriptionsListPager {
37	return &productSubscriptionsListPager{
38		client: client,
39		requester: func(ctx context.Context) (*azcore.Request, error) {
40			return client.listCreateRequest(ctx, resourceGroupName, serviceName, productID, options)
41		},
42		advancer: func(ctx context.Context, resp ProductSubscriptionsListResponse) (*azcore.Request, error) {
43			return azcore.NewRequest(ctx, http.MethodGet, *resp.SubscriptionCollection.NextLink)
44		},
45	}
46}
47
48// listCreateRequest creates the List request.
49func (client *ProductSubscriptionsClient) listCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, productID string, options *ProductSubscriptionsListOptions) (*azcore.Request, error) {
50	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/products/{productId}/subscriptions"
51	if resourceGroupName == "" {
52		return nil, errors.New("parameter resourceGroupName cannot be empty")
53	}
54	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
55	if serviceName == "" {
56		return nil, errors.New("parameter serviceName cannot be empty")
57	}
58	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
59	if productID == "" {
60		return nil, errors.New("parameter productID cannot be empty")
61	}
62	urlPath = strings.ReplaceAll(urlPath, "{productId}", url.PathEscape(productID))
63	if client.subscriptionID == "" {
64		return nil, errors.New("parameter client.subscriptionID cannot be empty")
65	}
66	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
67	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
68	if err != nil {
69		return nil, err
70	}
71	req.Telemetry(telemetryInfo)
72	reqQP := req.URL.Query()
73	if options != nil && options.Filter != nil {
74		reqQP.Set("$filter", *options.Filter)
75	}
76	if options != nil && options.Top != nil {
77		reqQP.Set("$top", strconv.FormatInt(int64(*options.Top), 10))
78	}
79	if options != nil && options.Skip != nil {
80		reqQP.Set("$skip", strconv.FormatInt(int64(*options.Skip), 10))
81	}
82	reqQP.Set("api-version", "2020-12-01")
83	req.URL.RawQuery = reqQP.Encode()
84	req.Header.Set("Accept", "application/json")
85	return req, nil
86}
87
88// listHandleResponse handles the List response.
89func (client *ProductSubscriptionsClient) listHandleResponse(resp *azcore.Response) (ProductSubscriptionsListResponse, error) {
90	result := ProductSubscriptionsListResponse{RawResponse: resp.Response}
91	if err := resp.UnmarshalAsJSON(&result.SubscriptionCollection); err != nil {
92		return ProductSubscriptionsListResponse{}, err
93	}
94	return result, nil
95}
96
97// listHandleError handles the List error response.
98func (client *ProductSubscriptionsClient) listHandleError(resp *azcore.Response) error {
99	body, err := resp.Payload()
100	if err != nil {
101		return azcore.NewResponseError(err, resp.Response)
102	}
103	errType := ErrorResponse{raw: string(body)}
104	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
105		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
106	}
107	return azcore.NewResponseError(&errType, resp.Response)
108}
109