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	"strings"
19)
20
21// QuotaByCounterKeysClient contains the methods for the QuotaByCounterKeys group.
22// Don't use this type directly, use NewQuotaByCounterKeysClient() instead.
23type QuotaByCounterKeysClient struct {
24	con            *armcore.Connection
25	subscriptionID string
26}
27
28// NewQuotaByCounterKeysClient creates a new instance of QuotaByCounterKeysClient with the specified values.
29func NewQuotaByCounterKeysClient(con *armcore.Connection, subscriptionID string) *QuotaByCounterKeysClient {
30	return &QuotaByCounterKeysClient{con: con, subscriptionID: subscriptionID}
31}
32
33// ListByService - Lists a collection of current quota counter periods associated with the counter-key configured in the policy on the specified service
34// instance. The api does not support paging yet.
35// If the operation fails it returns the *ErrorResponse error type.
36func (client *QuotaByCounterKeysClient) ListByService(ctx context.Context, resourceGroupName string, serviceName string, quotaCounterKey string, options *QuotaByCounterKeysListByServiceOptions) (QuotaByCounterKeysListByServiceResponse, error) {
37	req, err := client.listByServiceCreateRequest(ctx, resourceGroupName, serviceName, quotaCounterKey, options)
38	if err != nil {
39		return QuotaByCounterKeysListByServiceResponse{}, err
40	}
41	resp, err := client.con.Pipeline().Do(req)
42	if err != nil {
43		return QuotaByCounterKeysListByServiceResponse{}, err
44	}
45	if !resp.HasStatusCode(http.StatusOK) {
46		return QuotaByCounterKeysListByServiceResponse{}, client.listByServiceHandleError(resp)
47	}
48	return client.listByServiceHandleResponse(resp)
49}
50
51// listByServiceCreateRequest creates the ListByService request.
52func (client *QuotaByCounterKeysClient) listByServiceCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, quotaCounterKey string, options *QuotaByCounterKeysListByServiceOptions) (*azcore.Request, error) {
53	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/quotas/{quotaCounterKey}"
54	if resourceGroupName == "" {
55		return nil, errors.New("parameter resourceGroupName cannot be empty")
56	}
57	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
58	if serviceName == "" {
59		return nil, errors.New("parameter serviceName cannot be empty")
60	}
61	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
62	if quotaCounterKey == "" {
63		return nil, errors.New("parameter quotaCounterKey cannot be empty")
64	}
65	urlPath = strings.ReplaceAll(urlPath, "{quotaCounterKey}", url.PathEscape(quotaCounterKey))
66	if client.subscriptionID == "" {
67		return nil, errors.New("parameter client.subscriptionID cannot be empty")
68	}
69	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
70	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
71	if err != nil {
72		return nil, err
73	}
74	req.Telemetry(telemetryInfo)
75	reqQP := req.URL.Query()
76	reqQP.Set("api-version", "2020-12-01")
77	req.URL.RawQuery = reqQP.Encode()
78	req.Header.Set("Accept", "application/json")
79	return req, nil
80}
81
82// listByServiceHandleResponse handles the ListByService response.
83func (client *QuotaByCounterKeysClient) listByServiceHandleResponse(resp *azcore.Response) (QuotaByCounterKeysListByServiceResponse, error) {
84	result := QuotaByCounterKeysListByServiceResponse{RawResponse: resp.Response}
85	if err := resp.UnmarshalAsJSON(&result.QuotaCounterCollection); err != nil {
86		return QuotaByCounterKeysListByServiceResponse{}, err
87	}
88	return result, nil
89}
90
91// listByServiceHandleError handles the ListByService error response.
92func (client *QuotaByCounterKeysClient) listByServiceHandleError(resp *azcore.Response) error {
93	body, err := resp.Payload()
94	if err != nil {
95		return azcore.NewResponseError(err, resp.Response)
96	}
97	errType := ErrorResponse{raw: string(body)}
98	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
99		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
100	}
101	return azcore.NewResponseError(&errType, resp.Response)
102}
103
104// Update - Updates all the quota counter values specified with the existing quota counter key to a value in the specified service instance. This should
105// be used for reset of the quota counter values.
106// If the operation fails it returns the *ErrorResponse error type.
107func (client *QuotaByCounterKeysClient) Update(ctx context.Context, resourceGroupName string, serviceName string, quotaCounterKey string, parameters QuotaCounterValueUpdateContract, options *QuotaByCounterKeysUpdateOptions) (QuotaByCounterKeysUpdateResponse, error) {
108	req, err := client.updateCreateRequest(ctx, resourceGroupName, serviceName, quotaCounterKey, parameters, options)
109	if err != nil {
110		return QuotaByCounterKeysUpdateResponse{}, err
111	}
112	resp, err := client.con.Pipeline().Do(req)
113	if err != nil {
114		return QuotaByCounterKeysUpdateResponse{}, err
115	}
116	if !resp.HasStatusCode(http.StatusOK) {
117		return QuotaByCounterKeysUpdateResponse{}, client.updateHandleError(resp)
118	}
119	return client.updateHandleResponse(resp)
120}
121
122// updateCreateRequest creates the Update request.
123func (client *QuotaByCounterKeysClient) updateCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, quotaCounterKey string, parameters QuotaCounterValueUpdateContract, options *QuotaByCounterKeysUpdateOptions) (*azcore.Request, error) {
124	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/quotas/{quotaCounterKey}"
125	if resourceGroupName == "" {
126		return nil, errors.New("parameter resourceGroupName cannot be empty")
127	}
128	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
129	if serviceName == "" {
130		return nil, errors.New("parameter serviceName cannot be empty")
131	}
132	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
133	if quotaCounterKey == "" {
134		return nil, errors.New("parameter quotaCounterKey cannot be empty")
135	}
136	urlPath = strings.ReplaceAll(urlPath, "{quotaCounterKey}", url.PathEscape(quotaCounterKey))
137	if client.subscriptionID == "" {
138		return nil, errors.New("parameter client.subscriptionID cannot be empty")
139	}
140	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
141	req, err := azcore.NewRequest(ctx, http.MethodPatch, azcore.JoinPaths(client.con.Endpoint(), urlPath))
142	if err != nil {
143		return nil, err
144	}
145	req.Telemetry(telemetryInfo)
146	reqQP := req.URL.Query()
147	reqQP.Set("api-version", "2020-12-01")
148	req.URL.RawQuery = reqQP.Encode()
149	req.Header.Set("Accept", "application/json")
150	return req, req.MarshalAsJSON(parameters)
151}
152
153// updateHandleResponse handles the Update response.
154func (client *QuotaByCounterKeysClient) updateHandleResponse(resp *azcore.Response) (QuotaByCounterKeysUpdateResponse, error) {
155	result := QuotaByCounterKeysUpdateResponse{RawResponse: resp.Response}
156	if err := resp.UnmarshalAsJSON(&result.QuotaCounterCollection); err != nil {
157		return QuotaByCounterKeysUpdateResponse{}, err
158	}
159	return result, nil
160}
161
162// updateHandleError handles the Update error response.
163func (client *QuotaByCounterKeysClient) updateHandleError(resp *azcore.Response) error {
164	body, err := resp.Payload()
165	if err != nil {
166		return azcore.NewResponseError(err, resp.Response)
167	}
168	errType := ErrorResponse{raw: string(body)}
169	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
170		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
171	}
172	return azcore.NewResponseError(&errType, resp.Response)
173}
174