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