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	"strconv"
19	"strings"
20)
21
22// NotificationClient contains the methods for the Notification group.
23// Don't use this type directly, use NewNotificationClient() instead.
24type NotificationClient struct {
25	con            *armcore.Connection
26	subscriptionID string
27}
28
29// NewNotificationClient creates a new instance of NotificationClient with the specified values.
30func NewNotificationClient(con *armcore.Connection, subscriptionID string) *NotificationClient {
31	return &NotificationClient{con: con, subscriptionID: subscriptionID}
32}
33
34// CreateOrUpdate - Create or Update API Management publisher notification.
35// If the operation fails it returns the *ErrorResponse error type.
36func (client *NotificationClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName, options *NotificationCreateOrUpdateOptions) (NotificationCreateOrUpdateResponse, error) {
37	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, serviceName, notificationName, options)
38	if err != nil {
39		return NotificationCreateOrUpdateResponse{}, err
40	}
41	resp, err := client.con.Pipeline().Do(req)
42	if err != nil {
43		return NotificationCreateOrUpdateResponse{}, err
44	}
45	if !resp.HasStatusCode(http.StatusOK) {
46		return NotificationCreateOrUpdateResponse{}, client.createOrUpdateHandleError(resp)
47	}
48	return client.createOrUpdateHandleResponse(resp)
49}
50
51// createOrUpdateCreateRequest creates the CreateOrUpdate request.
52func (client *NotificationClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName, options *NotificationCreateOrUpdateOptions) (*azcore.Request, error) {
53	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications/{notificationName}"
54	if resourceGroupName == "" {
55		return nil, errors.New("parameter resourceGroupName cannot be empty")
56	}
57	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
58	if serviceName == "" {
59		return nil, errors.New("parameter serviceName cannot be empty")
60	}
61	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
62	if notificationName == "" {
63		return nil, errors.New("parameter notificationName cannot be empty")
64	}
65	urlPath = strings.ReplaceAll(urlPath, "{notificationName}", url.PathEscape(string(notificationName)))
66	if client.subscriptionID == "" {
67		return nil, errors.New("parameter client.subscriptionID cannot be empty")
68	}
69	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
70	req, err := azcore.NewRequest(ctx, http.MethodPut, azcore.JoinPaths(client.con.Endpoint(), urlPath))
71	if err != nil {
72		return nil, err
73	}
74	req.Telemetry(telemetryInfo)
75	reqQP := req.URL.Query()
76	reqQP.Set("api-version", "2020-12-01")
77	req.URL.RawQuery = reqQP.Encode()
78	if options != nil && options.IfMatch != nil {
79		req.Header.Set("If-Match", *options.IfMatch)
80	}
81	req.Header.Set("Accept", "application/json")
82	return req, nil
83}
84
85// createOrUpdateHandleResponse handles the CreateOrUpdate response.
86func (client *NotificationClient) createOrUpdateHandleResponse(resp *azcore.Response) (NotificationCreateOrUpdateResponse, error) {
87	result := NotificationCreateOrUpdateResponse{RawResponse: resp.Response}
88	if err := resp.UnmarshalAsJSON(&result.NotificationContract); err != nil {
89		return NotificationCreateOrUpdateResponse{}, err
90	}
91	return result, nil
92}
93
94// createOrUpdateHandleError handles the CreateOrUpdate error response.
95func (client *NotificationClient) createOrUpdateHandleError(resp *azcore.Response) error {
96	body, err := resp.Payload()
97	if err != nil {
98		return azcore.NewResponseError(err, resp.Response)
99	}
100	errType := ErrorResponse{raw: string(body)}
101	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
102		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
103	}
104	return azcore.NewResponseError(&errType, resp.Response)
105}
106
107// Get - Gets the details of the Notification specified by its identifier.
108// If the operation fails it returns the *ErrorResponse error type.
109func (client *NotificationClient) Get(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName, options *NotificationGetOptions) (NotificationGetResponse, error) {
110	req, err := client.getCreateRequest(ctx, resourceGroupName, serviceName, notificationName, options)
111	if err != nil {
112		return NotificationGetResponse{}, err
113	}
114	resp, err := client.con.Pipeline().Do(req)
115	if err != nil {
116		return NotificationGetResponse{}, err
117	}
118	if !resp.HasStatusCode(http.StatusOK) {
119		return NotificationGetResponse{}, client.getHandleError(resp)
120	}
121	return client.getHandleResponse(resp)
122}
123
124// getCreateRequest creates the Get request.
125func (client *NotificationClient) getCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName, options *NotificationGetOptions) (*azcore.Request, error) {
126	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications/{notificationName}"
127	if resourceGroupName == "" {
128		return nil, errors.New("parameter resourceGroupName cannot be empty")
129	}
130	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
131	if serviceName == "" {
132		return nil, errors.New("parameter serviceName cannot be empty")
133	}
134	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
135	if notificationName == "" {
136		return nil, errors.New("parameter notificationName cannot be empty")
137	}
138	urlPath = strings.ReplaceAll(urlPath, "{notificationName}", url.PathEscape(string(notificationName)))
139	if client.subscriptionID == "" {
140		return nil, errors.New("parameter client.subscriptionID cannot be empty")
141	}
142	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
143	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
144	if err != nil {
145		return nil, err
146	}
147	req.Telemetry(telemetryInfo)
148	reqQP := req.URL.Query()
149	reqQP.Set("api-version", "2020-12-01")
150	req.URL.RawQuery = reqQP.Encode()
151	req.Header.Set("Accept", "application/json")
152	return req, nil
153}
154
155// getHandleResponse handles the Get response.
156func (client *NotificationClient) getHandleResponse(resp *azcore.Response) (NotificationGetResponse, error) {
157	result := NotificationGetResponse{RawResponse: resp.Response}
158	if err := resp.UnmarshalAsJSON(&result.NotificationContract); err != nil {
159		return NotificationGetResponse{}, err
160	}
161	return result, nil
162}
163
164// getHandleError handles the Get error response.
165func (client *NotificationClient) getHandleError(resp *azcore.Response) error {
166	body, err := resp.Payload()
167	if err != nil {
168		return azcore.NewResponseError(err, resp.Response)
169	}
170	errType := ErrorResponse{raw: string(body)}
171	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
172		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
173	}
174	return azcore.NewResponseError(&errType, resp.Response)
175}
176
177// ListByService - Lists a collection of properties defined within a service instance.
178// If the operation fails it returns the *ErrorResponse error type.
179func (client *NotificationClient) ListByService(resourceGroupName string, serviceName string, options *NotificationListByServiceOptions) NotificationListByServicePager {
180	return &notificationListByServicePager{
181		client: client,
182		requester: func(ctx context.Context) (*azcore.Request, error) {
183			return client.listByServiceCreateRequest(ctx, resourceGroupName, serviceName, options)
184		},
185		advancer: func(ctx context.Context, resp NotificationListByServiceResponse) (*azcore.Request, error) {
186			return azcore.NewRequest(ctx, http.MethodGet, *resp.NotificationCollection.NextLink)
187		},
188	}
189}
190
191// listByServiceCreateRequest creates the ListByService request.
192func (client *NotificationClient) listByServiceCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, options *NotificationListByServiceOptions) (*azcore.Request, error) {
193	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications"
194	if resourceGroupName == "" {
195		return nil, errors.New("parameter resourceGroupName cannot be empty")
196	}
197	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
198	if serviceName == "" {
199		return nil, errors.New("parameter serviceName cannot be empty")
200	}
201	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
202	if client.subscriptionID == "" {
203		return nil, errors.New("parameter client.subscriptionID cannot be empty")
204	}
205	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
206	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
207	if err != nil {
208		return nil, err
209	}
210	req.Telemetry(telemetryInfo)
211	reqQP := req.URL.Query()
212	if options != nil && options.Top != nil {
213		reqQP.Set("$top", strconv.FormatInt(int64(*options.Top), 10))
214	}
215	if options != nil && options.Skip != nil {
216		reqQP.Set("$skip", strconv.FormatInt(int64(*options.Skip), 10))
217	}
218	reqQP.Set("api-version", "2020-12-01")
219	req.URL.RawQuery = reqQP.Encode()
220	req.Header.Set("Accept", "application/json")
221	return req, nil
222}
223
224// listByServiceHandleResponse handles the ListByService response.
225func (client *NotificationClient) listByServiceHandleResponse(resp *azcore.Response) (NotificationListByServiceResponse, error) {
226	result := NotificationListByServiceResponse{RawResponse: resp.Response}
227	if err := resp.UnmarshalAsJSON(&result.NotificationCollection); err != nil {
228		return NotificationListByServiceResponse{}, err
229	}
230	return result, nil
231}
232
233// listByServiceHandleError handles the ListByService error response.
234func (client *NotificationClient) listByServiceHandleError(resp *azcore.Response) error {
235	body, err := resp.Payload()
236	if err != nil {
237		return azcore.NewResponseError(err, resp.Response)
238	}
239	errType := ErrorResponse{raw: string(body)}
240	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
241		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
242	}
243	return azcore.NewResponseError(&errType, resp.Response)
244}
245