1// +build go1.13
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// Code generated by Microsoft (R) AutoRest Code Generator.
6// Changes may cause incorrect behavior and will be lost if the code is regenerated.
7
8package armapimanagement
9
10import (
11	"context"
12	"errors"
13	"fmt"
14	"github.com/Azure/azure-sdk-for-go/sdk/armcore"
15	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
16	"net/http"
17	"net/url"
18	"strings"
19)
20
21// PortalSettingsClient contains the methods for the PortalSettings group.
22// Don't use this type directly, use NewPortalSettingsClient() instead.
23type PortalSettingsClient struct {
24	con            *armcore.Connection
25	subscriptionID string
26}
27
28// NewPortalSettingsClient creates a new instance of PortalSettingsClient with the specified values.
29func NewPortalSettingsClient(con *armcore.Connection, subscriptionID string) *PortalSettingsClient {
30	return &PortalSettingsClient{con: con, subscriptionID: subscriptionID}
31}
32
33// ListByService - Lists a collection of portalsettings defined within a service instance..
34// If the operation fails it returns the *ErrorResponse error type.
35func (client *PortalSettingsClient) ListByService(ctx context.Context, resourceGroupName string, serviceName string, options *PortalSettingsListByServiceOptions) (PortalSettingsListByServiceResponse, error) {
36	req, err := client.listByServiceCreateRequest(ctx, resourceGroupName, serviceName, options)
37	if err != nil {
38		return PortalSettingsListByServiceResponse{}, err
39	}
40	resp, err := client.con.Pipeline().Do(req)
41	if err != nil {
42		return PortalSettingsListByServiceResponse{}, err
43	}
44	if !resp.HasStatusCode(http.StatusOK) {
45		return PortalSettingsListByServiceResponse{}, client.listByServiceHandleError(resp)
46	}
47	return client.listByServiceHandleResponse(resp)
48}
49
50// listByServiceCreateRequest creates the ListByService request.
51func (client *PortalSettingsClient) listByServiceCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, options *PortalSettingsListByServiceOptions) (*azcore.Request, error) {
52	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/portalsettings"
53	if resourceGroupName == "" {
54		return nil, errors.New("parameter resourceGroupName cannot be empty")
55	}
56	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
57	if serviceName == "" {
58		return nil, errors.New("parameter serviceName cannot be empty")
59	}
60	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
61	if client.subscriptionID == "" {
62		return nil, errors.New("parameter client.subscriptionID cannot be empty")
63	}
64	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
65	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
66	if err != nil {
67		return nil, err
68	}
69	req.Telemetry(telemetryInfo)
70	reqQP := req.URL.Query()
71	reqQP.Set("api-version", "2020-12-01")
72	req.URL.RawQuery = reqQP.Encode()
73	req.Header.Set("Accept", "application/json")
74	return req, nil
75}
76
77// listByServiceHandleResponse handles the ListByService response.
78func (client *PortalSettingsClient) listByServiceHandleResponse(resp *azcore.Response) (PortalSettingsListByServiceResponse, error) {
79	result := PortalSettingsListByServiceResponse{RawResponse: resp.Response}
80	if err := resp.UnmarshalAsJSON(&result.PortalSettingsCollection); err != nil {
81		return PortalSettingsListByServiceResponse{}, err
82	}
83	return result, nil
84}
85
86// listByServiceHandleError handles the ListByService error response.
87func (client *PortalSettingsClient) listByServiceHandleError(resp *azcore.Response) error {
88	body, err := resp.Payload()
89	if err != nil {
90		return azcore.NewResponseError(err, resp.Response)
91	}
92	errType := ErrorResponse{raw: string(body)}
93	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
94		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
95	}
96	return azcore.NewResponseError(&errType, resp.Response)
97}
98