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