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/tracing"
25	"net/http"
26)
27
28// QuotaByPeriodKeysClient is the client for the QuotaByPeriodKeys methods of the Apimanagement service.
29type QuotaByPeriodKeysClient struct {
30	BaseClient
31}
32
33// NewQuotaByPeriodKeysClient creates an instance of the QuotaByPeriodKeysClient client.
34func NewQuotaByPeriodKeysClient() QuotaByPeriodKeysClient {
35	return QuotaByPeriodKeysClient{New()}
36}
37
38// Get gets the value of the quota counter associated with the counter-key in the policy for the specific period in
39// service instance.
40// Parameters:
41// apimBaseURL - the management endpoint of the API Management service, for example
42// https://myapimservice.management.azure-api.net.
43// quotaCounterKey - quota counter key identifier.This is the result of expression defined in counter-key
44// attribute of the quota-by-key policy.For Example, if you specify counter-key="boo" in the policy, then it’s
45// accessible by "boo" counter key. But if it’s defined as counter-key="@("b"+"a")" then it will be accessible
46// by "ba" key
47// quotaPeriodKey - quota period key identifier.
48func (client QuotaByPeriodKeysClient) Get(ctx context.Context, apimBaseURL string, quotaCounterKey string, quotaPeriodKey string) (result QuotaCounterContract, err error) {
49	if tracing.IsEnabled() {
50		ctx = tracing.StartSpan(ctx, fqdn+"/QuotaByPeriodKeysClient.Get")
51		defer func() {
52			sc := -1
53			if result.Response.Response != nil {
54				sc = result.Response.Response.StatusCode
55			}
56			tracing.EndSpan(ctx, sc, err)
57		}()
58	}
59	req, err := client.GetPreparer(ctx, apimBaseURL, quotaCounterKey, quotaPeriodKey)
60	if err != nil {
61		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByPeriodKeysClient", "Get", nil, "Failure preparing request")
62		return
63	}
64
65	resp, err := client.GetSender(req)
66	if err != nil {
67		result.Response = autorest.Response{Response: resp}
68		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByPeriodKeysClient", "Get", resp, "Failure sending request")
69		return
70	}
71
72	result, err = client.GetResponder(resp)
73	if err != nil {
74		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByPeriodKeysClient", "Get", resp, "Failure responding to request")
75		return
76	}
77
78	return
79}
80
81// GetPreparer prepares the Get request.
82func (client QuotaByPeriodKeysClient) GetPreparer(ctx context.Context, apimBaseURL string, quotaCounterKey string, quotaPeriodKey string) (*http.Request, error) {
83	urlParameters := map[string]interface{}{
84		"apimBaseUrl": apimBaseURL,
85	}
86
87	pathParameters := map[string]interface{}{
88		"quotaCounterKey": autorest.Encode("path", quotaCounterKey),
89		"quotaPeriodKey":  autorest.Encode("path", quotaPeriodKey),
90	}
91
92	const APIVersion = "2017-03-01"
93	queryParameters := map[string]interface{}{
94		"api-version": APIVersion,
95	}
96
97	preparer := autorest.CreatePreparer(
98		autorest.AsGet(),
99		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
100		autorest.WithPathParameters("/quotas/{quotaCounterKey}/{quotaPeriodKey}", pathParameters),
101		autorest.WithQueryParameters(queryParameters))
102	return preparer.Prepare((&http.Request{}).WithContext(ctx))
103}
104
105// GetSender sends the Get request. The method will close the
106// http.Response Body if it receives an error.
107func (client QuotaByPeriodKeysClient) GetSender(req *http.Request) (*http.Response, error) {
108	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
109}
110
111// GetResponder handles the response to the Get request. The method always
112// closes the http.Response Body.
113func (client QuotaByPeriodKeysClient) GetResponder(resp *http.Response) (result QuotaCounterContract, err error) {
114	err = autorest.Respond(
115		resp,
116		azure.WithErrorUnlessStatusCode(http.StatusOK),
117		autorest.ByUnmarshallingJSON(&result),
118		autorest.ByClosing())
119	result.Response = autorest.Response{Response: resp}
120	return
121}
122
123// Update updates an existing quota counter value in the specified service instance.
124// Parameters:
125// apimBaseURL - the management endpoint of the API Management service, for example
126// https://myapimservice.management.azure-api.net.
127// quotaCounterKey - quota counter key identifier.This is the result of expression defined in counter-key
128// attribute of the quota-by-key policy.For Example, if you specify counter-key="boo" in the policy, then it’s
129// accessible by "boo" counter key. But if it’s defined as counter-key="@("b"+"a")" then it will be accessible
130// by "ba" key
131// quotaPeriodKey - quota period key identifier.
132// parameters - the value of the Quota counter to be applied on the specified period.
133func (client QuotaByPeriodKeysClient) Update(ctx context.Context, apimBaseURL string, quotaCounterKey string, quotaPeriodKey string, parameters QuotaCounterValueContractProperties) (result autorest.Response, err error) {
134	if tracing.IsEnabled() {
135		ctx = tracing.StartSpan(ctx, fqdn+"/QuotaByPeriodKeysClient.Update")
136		defer func() {
137			sc := -1
138			if result.Response != nil {
139				sc = result.Response.StatusCode
140			}
141			tracing.EndSpan(ctx, sc, err)
142		}()
143	}
144	req, err := client.UpdatePreparer(ctx, apimBaseURL, quotaCounterKey, quotaPeriodKey, parameters)
145	if err != nil {
146		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByPeriodKeysClient", "Update", nil, "Failure preparing request")
147		return
148	}
149
150	resp, err := client.UpdateSender(req)
151	if err != nil {
152		result.Response = resp
153		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByPeriodKeysClient", "Update", resp, "Failure sending request")
154		return
155	}
156
157	result, err = client.UpdateResponder(resp)
158	if err != nil {
159		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByPeriodKeysClient", "Update", resp, "Failure responding to request")
160		return
161	}
162
163	return
164}
165
166// UpdatePreparer prepares the Update request.
167func (client QuotaByPeriodKeysClient) UpdatePreparer(ctx context.Context, apimBaseURL string, quotaCounterKey string, quotaPeriodKey string, parameters QuotaCounterValueContractProperties) (*http.Request, error) {
168	urlParameters := map[string]interface{}{
169		"apimBaseUrl": apimBaseURL,
170	}
171
172	pathParameters := map[string]interface{}{
173		"quotaCounterKey": autorest.Encode("path", quotaCounterKey),
174		"quotaPeriodKey":  autorest.Encode("path", quotaPeriodKey),
175	}
176
177	const APIVersion = "2017-03-01"
178	queryParameters := map[string]interface{}{
179		"api-version": APIVersion,
180	}
181
182	preparer := autorest.CreatePreparer(
183		autorest.AsContentType("application/json; charset=utf-8"),
184		autorest.AsPatch(),
185		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
186		autorest.WithPathParameters("/quotas/{quotaCounterKey}/{quotaPeriodKey}", pathParameters),
187		autorest.WithJSON(parameters),
188		autorest.WithQueryParameters(queryParameters))
189	return preparer.Prepare((&http.Request{}).WithContext(ctx))
190}
191
192// UpdateSender sends the Update request. The method will close the
193// http.Response Body if it receives an error.
194func (client QuotaByPeriodKeysClient) UpdateSender(req *http.Request) (*http.Response, error) {
195	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
196}
197
198// UpdateResponder handles the response to the Update request. The method always
199// closes the http.Response Body.
200func (client QuotaByPeriodKeysClient) UpdateResponder(resp *http.Response) (result autorest.Response, err error) {
201	err = autorest.Respond(
202		resp,
203		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
204		autorest.ByClosing())
205	result.Response = resp
206	return
207}
208