1package apimanagement
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//
6// Code generated by Microsoft (R) AutoRest Code Generator.
7// Changes may cause incorrect behavior and will be lost if the code is regenerated.
8
9import (
10	"context"
11	"github.com/Azure/go-autorest/autorest"
12	"github.com/Azure/go-autorest/autorest/azure"
13	"github.com/Azure/go-autorest/autorest/validation"
14	"github.com/Azure/go-autorest/tracing"
15	"net/http"
16)
17
18// QuotaByCounterKeysClient is the apiManagement Client
19type QuotaByCounterKeysClient struct {
20	BaseClient
21}
22
23// NewQuotaByCounterKeysClient creates an instance of the QuotaByCounterKeysClient client.
24func NewQuotaByCounterKeysClient(subscriptionID string) QuotaByCounterKeysClient {
25	return NewQuotaByCounterKeysClientWithBaseURI(DefaultBaseURI, subscriptionID)
26}
27
28// NewQuotaByCounterKeysClientWithBaseURI creates an instance of the QuotaByCounterKeysClient client using a custom
29// endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure
30// stack).
31func NewQuotaByCounterKeysClientWithBaseURI(baseURI string, subscriptionID string) QuotaByCounterKeysClient {
32	return QuotaByCounterKeysClient{NewWithBaseURI(baseURI, subscriptionID)}
33}
34
35// ListByService lists a collection of current quota counter periods associated with the counter-key configured in the
36// policy on the specified service instance. The api does not support paging yet.
37// Parameters:
38// resourceGroupName - the name of the resource group.
39// serviceName - the name of the API Management service.
40// quotaCounterKey - quota counter key identifier.This is the result of expression defined in counter-key
41// attribute of the quota-by-key policy.For Example, if you specify counter-key="boo" in the policy, then it’s
42// accessible by "boo" counter key. But if it’s defined as counter-key="@("b"+"a")" then it will be accessible
43// by "ba" key
44func (client QuotaByCounterKeysClient) ListByService(ctx context.Context, resourceGroupName string, serviceName string, quotaCounterKey string) (result QuotaCounterCollection, err error) {
45	if tracing.IsEnabled() {
46		ctx = tracing.StartSpan(ctx, fqdn+"/QuotaByCounterKeysClient.ListByService")
47		defer func() {
48			sc := -1
49			if result.Response.Response != nil {
50				sc = result.Response.Response.StatusCode
51			}
52			tracing.EndSpan(ctx, sc, err)
53		}()
54	}
55	if err := validation.Validate([]validation.Validation{
56		{TargetValue: serviceName,
57			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
58				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
59				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
60		return result, validation.NewError("apimanagement.QuotaByCounterKeysClient", "ListByService", err.Error())
61	}
62
63	req, err := client.ListByServicePreparer(ctx, resourceGroupName, serviceName, quotaCounterKey)
64	if err != nil {
65		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByCounterKeysClient", "ListByService", nil, "Failure preparing request")
66		return
67	}
68
69	resp, err := client.ListByServiceSender(req)
70	if err != nil {
71		result.Response = autorest.Response{Response: resp}
72		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByCounterKeysClient", "ListByService", resp, "Failure sending request")
73		return
74	}
75
76	result, err = client.ListByServiceResponder(resp)
77	if err != nil {
78		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByCounterKeysClient", "ListByService", resp, "Failure responding to request")
79		return
80	}
81
82	return
83}
84
85// ListByServicePreparer prepares the ListByService request.
86func (client QuotaByCounterKeysClient) ListByServicePreparer(ctx context.Context, resourceGroupName string, serviceName string, quotaCounterKey string) (*http.Request, error) {
87	pathParameters := map[string]interface{}{
88		"quotaCounterKey":   autorest.Encode("path", quotaCounterKey),
89		"resourceGroupName": autorest.Encode("path", resourceGroupName),
90		"serviceName":       autorest.Encode("path", serviceName),
91		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
92	}
93
94	const APIVersion = "2020-12-01"
95	queryParameters := map[string]interface{}{
96		"api-version": APIVersion,
97	}
98
99	preparer := autorest.CreatePreparer(
100		autorest.AsGet(),
101		autorest.WithBaseURL(client.BaseURI),
102		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/quotas/{quotaCounterKey}", pathParameters),
103		autorest.WithQueryParameters(queryParameters))
104	return preparer.Prepare((&http.Request{}).WithContext(ctx))
105}
106
107// ListByServiceSender sends the ListByService request. The method will close the
108// http.Response Body if it receives an error.
109func (client QuotaByCounterKeysClient) ListByServiceSender(req *http.Request) (*http.Response, error) {
110	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
111}
112
113// ListByServiceResponder handles the response to the ListByService request. The method always
114// closes the http.Response Body.
115func (client QuotaByCounterKeysClient) ListByServiceResponder(resp *http.Response) (result QuotaCounterCollection, err error) {
116	err = autorest.Respond(
117		resp,
118		azure.WithErrorUnlessStatusCode(http.StatusOK),
119		autorest.ByUnmarshallingJSON(&result),
120		autorest.ByClosing())
121	result.Response = autorest.Response{Response: resp}
122	return
123}
124
125// Update updates all the quota counter values specified with the existing quota counter key to a value in the
126// specified service instance. This should be used for reset of the quota counter values.
127// Parameters:
128// resourceGroupName - the name of the resource group.
129// serviceName - the name of the API Management service.
130// quotaCounterKey - quota counter key identifier.This is the result of expression defined in counter-key
131// attribute of the quota-by-key policy.For Example, if you specify counter-key="boo" in the policy, then it’s
132// accessible by "boo" counter key. But if it’s defined as counter-key="@("b"+"a")" then it will be accessible
133// by "ba" key
134// parameters - the value of the quota counter to be applied to all quota counter periods.
135func (client QuotaByCounterKeysClient) Update(ctx context.Context, resourceGroupName string, serviceName string, quotaCounterKey string, parameters QuotaCounterValueUpdateContract) (result QuotaCounterCollection, err error) {
136	if tracing.IsEnabled() {
137		ctx = tracing.StartSpan(ctx, fqdn+"/QuotaByCounterKeysClient.Update")
138		defer func() {
139			sc := -1
140			if result.Response.Response != nil {
141				sc = result.Response.Response.StatusCode
142			}
143			tracing.EndSpan(ctx, sc, err)
144		}()
145	}
146	if err := validation.Validate([]validation.Validation{
147		{TargetValue: serviceName,
148			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
149				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
150				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
151		return result, validation.NewError("apimanagement.QuotaByCounterKeysClient", "Update", err.Error())
152	}
153
154	req, err := client.UpdatePreparer(ctx, resourceGroupName, serviceName, quotaCounterKey, parameters)
155	if err != nil {
156		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByCounterKeysClient", "Update", nil, "Failure preparing request")
157		return
158	}
159
160	resp, err := client.UpdateSender(req)
161	if err != nil {
162		result.Response = autorest.Response{Response: resp}
163		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByCounterKeysClient", "Update", resp, "Failure sending request")
164		return
165	}
166
167	result, err = client.UpdateResponder(resp)
168	if err != nil {
169		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByCounterKeysClient", "Update", resp, "Failure responding to request")
170		return
171	}
172
173	return
174}
175
176// UpdatePreparer prepares the Update request.
177func (client QuotaByCounterKeysClient) UpdatePreparer(ctx context.Context, resourceGroupName string, serviceName string, quotaCounterKey string, parameters QuotaCounterValueUpdateContract) (*http.Request, error) {
178	pathParameters := map[string]interface{}{
179		"quotaCounterKey":   autorest.Encode("path", quotaCounterKey),
180		"resourceGroupName": autorest.Encode("path", resourceGroupName),
181		"serviceName":       autorest.Encode("path", serviceName),
182		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
183	}
184
185	const APIVersion = "2020-12-01"
186	queryParameters := map[string]interface{}{
187		"api-version": APIVersion,
188	}
189
190	preparer := autorest.CreatePreparer(
191		autorest.AsContentType("application/json; charset=utf-8"),
192		autorest.AsPatch(),
193		autorest.WithBaseURL(client.BaseURI),
194		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/quotas/{quotaCounterKey}", pathParameters),
195		autorest.WithJSON(parameters),
196		autorest.WithQueryParameters(queryParameters))
197	return preparer.Prepare((&http.Request{}).WithContext(ctx))
198}
199
200// UpdateSender sends the Update request. The method will close the
201// http.Response Body if it receives an error.
202func (client QuotaByCounterKeysClient) UpdateSender(req *http.Request) (*http.Response, error) {
203	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
204}
205
206// UpdateResponder handles the response to the Update request. The method always
207// closes the http.Response Body.
208func (client QuotaByCounterKeysClient) UpdateResponder(resp *http.Response) (result QuotaCounterCollection, err error) {
209	err = autorest.Respond(
210		resp,
211		azure.WithErrorUnlessStatusCode(http.StatusOK),
212		autorest.ByUnmarshallingJSON(&result),
213		autorest.ByClosing())
214	result.Response = autorest.Response{Response: resp}
215	return
216}
217