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		return
89	}
90
91	return
92}
93
94// GetPreparer prepares the Get request.
95func (client QuotaByPeriodKeysClient) GetPreparer(ctx context.Context, resourceGroupName string, serviceName string, quotaCounterKey string, quotaPeriodKey string) (*http.Request, error) {
96	pathParameters := map[string]interface{}{
97		"quotaCounterKey":   autorest.Encode("path", quotaCounterKey),
98		"quotaPeriodKey":    autorest.Encode("path", quotaPeriodKey),
99		"resourceGroupName": autorest.Encode("path", resourceGroupName),
100		"serviceName":       autorest.Encode("path", serviceName),
101		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
102	}
103
104	const APIVersion = "2016-07-07"
105	queryParameters := map[string]interface{}{
106		"api-version": APIVersion,
107	}
108
109	preparer := autorest.CreatePreparer(
110		autorest.AsGet(),
111		autorest.WithBaseURL(client.BaseURI),
112		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/quotas/{quotaCounterKey}/{quotaPeriodKey}", pathParameters),
113		autorest.WithQueryParameters(queryParameters))
114	return preparer.Prepare((&http.Request{}).WithContext(ctx))
115}
116
117// GetSender sends the Get request. The method will close the
118// http.Response Body if it receives an error.
119func (client QuotaByPeriodKeysClient) GetSender(req *http.Request) (*http.Response, error) {
120	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
121}
122
123// GetResponder handles the response to the Get request. The method always
124// closes the http.Response Body.
125func (client QuotaByPeriodKeysClient) GetResponder(resp *http.Response) (result QuotaCounterContract, err error) {
126	err = autorest.Respond(
127		resp,
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		return
178	}
179
180	return
181}
182
183// UpdatePreparer prepares the Update request.
184func (client QuotaByPeriodKeysClient) UpdatePreparer(ctx context.Context, resourceGroupName string, serviceName string, quotaCounterKey string, quotaPeriodKey string, parameters QuotaCounterValueContract) (*http.Request, error) {
185	pathParameters := map[string]interface{}{
186		"quotaCounterKey":   autorest.Encode("path", quotaCounterKey),
187		"quotaPeriodKey":    autorest.Encode("path", quotaPeriodKey),
188		"resourceGroupName": autorest.Encode("path", resourceGroupName),
189		"serviceName":       autorest.Encode("path", serviceName),
190		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
191	}
192
193	const APIVersion = "2016-07-07"
194	queryParameters := map[string]interface{}{
195		"api-version": APIVersion,
196	}
197
198	preparer := autorest.CreatePreparer(
199		autorest.AsContentType("application/json; charset=utf-8"),
200		autorest.AsPatch(),
201		autorest.WithBaseURL(client.BaseURI),
202		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/quotas/{quotaCounterKey}/{quotaPeriodKey}", pathParameters),
203		autorest.WithJSON(parameters),
204		autorest.WithQueryParameters(queryParameters))
205	return preparer.Prepare((&http.Request{}).WithContext(ctx))
206}
207
208// UpdateSender sends the Update request. The method will close the
209// http.Response Body if it receives an error.
210func (client QuotaByPeriodKeysClient) UpdateSender(req *http.Request) (*http.Response, error) {
211	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
212}
213
214// UpdateResponder handles the response to the Update request. The method always
215// closes the http.Response Body.
216func (client QuotaByPeriodKeysClient) UpdateResponder(resp *http.Response) (result autorest.Response, err error) {
217	err = autorest.Respond(
218		resp,
219		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
220		autorest.ByClosing())
221	result.Response = resp
222	return
223}
224