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// QuotaByPeriodKeysClient is the apiManagement Client
30type QuotaByPeriodKeysClient struct {
31	BaseClient
32}
33
34// NewQuotaByPeriodKeysClient creates an instance of the QuotaByPeriodKeysClient client.
35func NewQuotaByPeriodKeysClient(subscriptionID string) QuotaByPeriodKeysClient {
36	return NewQuotaByPeriodKeysClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewQuotaByPeriodKeysClientWithBaseURI creates an instance of the QuotaByPeriodKeysClient client using a custom
40// endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure
41// stack).
42func NewQuotaByPeriodKeysClientWithBaseURI(baseURI string, subscriptionID string) QuotaByPeriodKeysClient {
43	return QuotaByPeriodKeysClient{NewWithBaseURI(baseURI, subscriptionID)}
44}
45
46// Get gets the value of the quota counter associated with the counter-key in the policy for the specific period in
47// service instance.
48// Parameters:
49// resourceGroupName - the name of the resource group.
50// serviceName - the name of the API Management service.
51// quotaCounterKey - quota counter key identifier.
52// quotaPeriodKey - quota period key identifier.
53func (client QuotaByPeriodKeysClient) Get(ctx context.Context, resourceGroupName string, serviceName string, quotaCounterKey string, quotaPeriodKey string) (result QuotaCounterContract, err error) {
54	if tracing.IsEnabled() {
55		ctx = tracing.StartSpan(ctx, fqdn+"/QuotaByPeriodKeysClient.Get")
56		defer func() {
57			sc := -1
58			if result.Response.Response != nil {
59				sc = result.Response.Response.StatusCode
60			}
61			tracing.EndSpan(ctx, sc, err)
62		}()
63	}
64	if err := validation.Validate([]validation.Validation{
65		{TargetValue: serviceName,
66			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
67				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
68				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
69		return result, validation.NewError("apimanagement.QuotaByPeriodKeysClient", "Get", err.Error())
70	}
71
72	req, err := client.GetPreparer(ctx, resourceGroupName, serviceName, quotaCounterKey, quotaPeriodKey)
73	if err != nil {
74		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByPeriodKeysClient", "Get", nil, "Failure preparing request")
75		return
76	}
77
78	resp, err := client.GetSender(req)
79	if err != nil {
80		result.Response = autorest.Response{Response: resp}
81		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByPeriodKeysClient", "Get", resp, "Failure sending request")
82		return
83	}
84
85	result, err = client.GetResponder(resp)
86	if err != nil {
87		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByPeriodKeysClient", "Get", resp, "Failure responding to request")
88	}
89
90	return
91}
92
93// GetPreparer prepares the Get request.
94func (client QuotaByPeriodKeysClient) GetPreparer(ctx context.Context, resourceGroupName string, serviceName string, quotaCounterKey string, quotaPeriodKey string) (*http.Request, error) {
95	pathParameters := map[string]interface{}{
96		"quotaCounterKey":   autorest.Encode("path", quotaCounterKey),
97		"quotaPeriodKey":    autorest.Encode("path", quotaPeriodKey),
98		"resourceGroupName": autorest.Encode("path", resourceGroupName),
99		"serviceName":       autorest.Encode("path", serviceName),
100		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
101	}
102
103	const APIVersion = "2016-10-10"
104	queryParameters := map[string]interface{}{
105		"api-version": APIVersion,
106	}
107
108	preparer := autorest.CreatePreparer(
109		autorest.AsGet(),
110		autorest.WithBaseURL(client.BaseURI),
111		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/quotas/{quotaCounterKey}/{quotaPeriodKey}", pathParameters),
112		autorest.WithQueryParameters(queryParameters))
113	return preparer.Prepare((&http.Request{}).WithContext(ctx))
114}
115
116// GetSender sends the Get request. The method will close the
117// http.Response Body if it receives an error.
118func (client QuotaByPeriodKeysClient) GetSender(req *http.Request) (*http.Response, error) {
119	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
120}
121
122// GetResponder handles the response to the Get request. The method always
123// closes the http.Response Body.
124func (client QuotaByPeriodKeysClient) GetResponder(resp *http.Response) (result QuotaCounterContract, err error) {
125	err = autorest.Respond(
126		resp,
127		client.ByInspecting(),
128		azure.WithErrorUnlessStatusCode(http.StatusOK),
129		autorest.ByUnmarshallingJSON(&result),
130		autorest.ByClosing())
131	result.Response = autorest.Response{Response: resp}
132	return
133}
134
135// Update updates an existing quota counter value in the specified service instance.
136// Parameters:
137// resourceGroupName - the name of the resource group.
138// serviceName - the name of the API Management service.
139// quotaCounterKey - quota counter key identifier.
140// quotaPeriodKey - quota period key identifier.
141// parameters - the value of the Quota counter to be applied on the specified period.
142func (client QuotaByPeriodKeysClient) Update(ctx context.Context, resourceGroupName string, serviceName string, quotaCounterKey string, quotaPeriodKey string, parameters QuotaCounterValueContract) (result autorest.Response, err error) {
143	if tracing.IsEnabled() {
144		ctx = tracing.StartSpan(ctx, fqdn+"/QuotaByPeriodKeysClient.Update")
145		defer func() {
146			sc := -1
147			if result.Response != nil {
148				sc = result.Response.StatusCode
149			}
150			tracing.EndSpan(ctx, sc, err)
151		}()
152	}
153	if err := validation.Validate([]validation.Validation{
154		{TargetValue: serviceName,
155			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
156				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
157				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
158		return result, validation.NewError("apimanagement.QuotaByPeriodKeysClient", "Update", err.Error())
159	}
160
161	req, err := client.UpdatePreparer(ctx, resourceGroupName, serviceName, quotaCounterKey, quotaPeriodKey, parameters)
162	if err != nil {
163		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByPeriodKeysClient", "Update", nil, "Failure preparing request")
164		return
165	}
166
167	resp, err := client.UpdateSender(req)
168	if err != nil {
169		result.Response = resp
170		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByPeriodKeysClient", "Update", resp, "Failure sending request")
171		return
172	}
173
174	result, err = client.UpdateResponder(resp)
175	if err != nil {
176		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByPeriodKeysClient", "Update", resp, "Failure responding to request")
177	}
178
179	return
180}
181
182// UpdatePreparer prepares the Update request.
183func (client QuotaByPeriodKeysClient) UpdatePreparer(ctx context.Context, resourceGroupName string, serviceName string, quotaCounterKey string, quotaPeriodKey string, parameters QuotaCounterValueContract) (*http.Request, error) {
184	pathParameters := map[string]interface{}{
185		"quotaCounterKey":   autorest.Encode("path", quotaCounterKey),
186		"quotaPeriodKey":    autorest.Encode("path", quotaPeriodKey),
187		"resourceGroupName": autorest.Encode("path", resourceGroupName),
188		"serviceName":       autorest.Encode("path", serviceName),
189		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
190	}
191
192	const APIVersion = "2016-10-10"
193	queryParameters := map[string]interface{}{
194		"api-version": APIVersion,
195	}
196
197	preparer := autorest.CreatePreparer(
198		autorest.AsContentType("application/json; charset=utf-8"),
199		autorest.AsPatch(),
200		autorest.WithBaseURL(client.BaseURI),
201		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/quotas/{quotaCounterKey}/{quotaPeriodKey}", pathParameters),
202		autorest.WithJSON(parameters),
203		autorest.WithQueryParameters(queryParameters))
204	return preparer.Prepare((&http.Request{}).WithContext(ctx))
205}
206
207// UpdateSender sends the Update request. The method will close the
208// http.Response Body if it receives an error.
209func (client QuotaByPeriodKeysClient) UpdateSender(req *http.Request) (*http.Response, error) {
210	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
211}
212
213// UpdateResponder handles the response to the Update request. The method always
214// closes the http.Response Body.
215func (client QuotaByPeriodKeysClient) UpdateResponder(resp *http.Response) (result autorest.Response, err error) {
216	err = autorest.Respond(
217		resp,
218		client.ByInspecting(),
219		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
220		autorest.ByClosing())
221	result.Response = resp
222	return
223}
224