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// PolicyDescriptionClient is the apiManagement Client
19type PolicyDescriptionClient struct {
20	BaseClient
21}
22
23// NewPolicyDescriptionClient creates an instance of the PolicyDescriptionClient client.
24func NewPolicyDescriptionClient(subscriptionID string) PolicyDescriptionClient {
25	return NewPolicyDescriptionClientWithBaseURI(DefaultBaseURI, subscriptionID)
26}
27
28// NewPolicyDescriptionClientWithBaseURI creates an instance of the PolicyDescriptionClient 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 NewPolicyDescriptionClientWithBaseURI(baseURI string, subscriptionID string) PolicyDescriptionClient {
32	return PolicyDescriptionClient{NewWithBaseURI(baseURI, subscriptionID)}
33}
34
35// ListByService lists all policy descriptions.
36// Parameters:
37// resourceGroupName - the name of the resource group.
38// serviceName - the name of the API Management service.
39// scope - policy scope.
40func (client PolicyDescriptionClient) ListByService(ctx context.Context, resourceGroupName string, serviceName string, scope PolicyScopeContract) (result PolicyDescriptionCollection, err error) {
41	if tracing.IsEnabled() {
42		ctx = tracing.StartSpan(ctx, fqdn+"/PolicyDescriptionClient.ListByService")
43		defer func() {
44			sc := -1
45			if result.Response.Response != nil {
46				sc = result.Response.Response.StatusCode
47			}
48			tracing.EndSpan(ctx, sc, err)
49		}()
50	}
51	if err := validation.Validate([]validation.Validation{
52		{TargetValue: serviceName,
53			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
54				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
55				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
56		return result, validation.NewError("apimanagement.PolicyDescriptionClient", "ListByService", err.Error())
57	}
58
59	req, err := client.ListByServicePreparer(ctx, resourceGroupName, serviceName, scope)
60	if err != nil {
61		err = autorest.NewErrorWithError(err, "apimanagement.PolicyDescriptionClient", "ListByService", nil, "Failure preparing request")
62		return
63	}
64
65	resp, err := client.ListByServiceSender(req)
66	if err != nil {
67		result.Response = autorest.Response{Response: resp}
68		err = autorest.NewErrorWithError(err, "apimanagement.PolicyDescriptionClient", "ListByService", resp, "Failure sending request")
69		return
70	}
71
72	result, err = client.ListByServiceResponder(resp)
73	if err != nil {
74		err = autorest.NewErrorWithError(err, "apimanagement.PolicyDescriptionClient", "ListByService", resp, "Failure responding to request")
75		return
76	}
77
78	return
79}
80
81// ListByServicePreparer prepares the ListByService request.
82func (client PolicyDescriptionClient) ListByServicePreparer(ctx context.Context, resourceGroupName string, serviceName string, scope PolicyScopeContract) (*http.Request, error) {
83	pathParameters := map[string]interface{}{
84		"resourceGroupName": autorest.Encode("path", resourceGroupName),
85		"serviceName":       autorest.Encode("path", serviceName),
86		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
87	}
88
89	const APIVersion = "2019-12-01-preview"
90	queryParameters := map[string]interface{}{
91		"api-version": APIVersion,
92	}
93	if len(string(scope)) > 0 {
94		queryParameters["scope"] = autorest.Encode("query", scope)
95	}
96
97	preparer := autorest.CreatePreparer(
98		autorest.AsGet(),
99		autorest.WithBaseURL(client.BaseURI),
100		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/policyDescriptions", pathParameters),
101		autorest.WithQueryParameters(queryParameters))
102	return preparer.Prepare((&http.Request{}).WithContext(ctx))
103}
104
105// ListByServiceSender sends the ListByService request. The method will close the
106// http.Response Body if it receives an error.
107func (client PolicyDescriptionClient) ListByServiceSender(req *http.Request) (*http.Response, error) {
108	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
109}
110
111// ListByServiceResponder handles the response to the ListByService request. The method always
112// closes the http.Response Body.
113func (client PolicyDescriptionClient) ListByServiceResponder(resp *http.Response) (result PolicyDescriptionCollection, 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