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// NotificationRecipientUserClient contains the methods for the NotificationRecipientUser group.
22// Don't use this type directly, use NewNotificationRecipientUserClient() instead.
23type NotificationRecipientUserClient struct {
24	con            *armcore.Connection
25	subscriptionID string
26}
27
28// NewNotificationRecipientUserClient creates a new instance of NotificationRecipientUserClient with the specified values.
29func NewNotificationRecipientUserClient(con *armcore.Connection, subscriptionID string) *NotificationRecipientUserClient {
30	return &NotificationRecipientUserClient{con: con, subscriptionID: subscriptionID}
31}
32
33// CheckEntityExists - Determine if the Notification Recipient User is subscribed to the notification.
34// If the operation fails it returns the *ErrorResponse error type.
35func (client *NotificationRecipientUserClient) CheckEntityExists(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName, userID string, options *NotificationRecipientUserCheckEntityExistsOptions) (NotificationRecipientUserCheckEntityExistsResponse, error) {
36	req, err := client.checkEntityExistsCreateRequest(ctx, resourceGroupName, serviceName, notificationName, userID, options)
37	if err != nil {
38		return NotificationRecipientUserCheckEntityExistsResponse{}, err
39	}
40	resp, err := client.con.Pipeline().Do(req)
41	if err != nil {
42		return NotificationRecipientUserCheckEntityExistsResponse{}, err
43	}
44	result := NotificationRecipientUserCheckEntityExistsResponse{RawResponse: resp.Response}
45	if resp.StatusCode >= 200 && resp.StatusCode < 300 {
46		result.Success = true
47	}
48	return result, nil
49}
50
51// checkEntityExistsCreateRequest creates the CheckEntityExists request.
52func (client *NotificationRecipientUserClient) checkEntityExistsCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName, userID string, options *NotificationRecipientUserCheckEntityExistsOptions) (*azcore.Request, error) {
53	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications/{notificationName}/recipientUsers/{userId}"
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 userID == "" {
67		return nil, errors.New("parameter userID cannot be empty")
68	}
69	urlPath = strings.ReplaceAll(urlPath, "{userId}", url.PathEscape(userID))
70	if client.subscriptionID == "" {
71		return nil, errors.New("parameter client.subscriptionID cannot be empty")
72	}
73	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
74	req, err := azcore.NewRequest(ctx, http.MethodHead, azcore.JoinPaths(client.con.Endpoint(), urlPath))
75	if err != nil {
76		return nil, err
77	}
78	req.Telemetry(telemetryInfo)
79	reqQP := req.URL.Query()
80	reqQP.Set("api-version", "2020-12-01")
81	req.URL.RawQuery = reqQP.Encode()
82	req.Header.Set("Accept", "application/json")
83	return req, nil
84}
85
86// CreateOrUpdate - Adds the API Management User to the list of Recipients for the Notification.
87// If the operation fails it returns the *ErrorResponse error type.
88func (client *NotificationRecipientUserClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName, userID string, options *NotificationRecipientUserCreateOrUpdateOptions) (NotificationRecipientUserCreateOrUpdateResponse, error) {
89	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, serviceName, notificationName, userID, options)
90	if err != nil {
91		return NotificationRecipientUserCreateOrUpdateResponse{}, err
92	}
93	resp, err := client.con.Pipeline().Do(req)
94	if err != nil {
95		return NotificationRecipientUserCreateOrUpdateResponse{}, err
96	}
97	if !resp.HasStatusCode(http.StatusOK, http.StatusCreated) {
98		return NotificationRecipientUserCreateOrUpdateResponse{}, client.createOrUpdateHandleError(resp)
99	}
100	return client.createOrUpdateHandleResponse(resp)
101}
102
103// createOrUpdateCreateRequest creates the CreateOrUpdate request.
104func (client *NotificationRecipientUserClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName, userID string, options *NotificationRecipientUserCreateOrUpdateOptions) (*azcore.Request, error) {
105	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications/{notificationName}/recipientUsers/{userId}"
106	if resourceGroupName == "" {
107		return nil, errors.New("parameter resourceGroupName cannot be empty")
108	}
109	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
110	if serviceName == "" {
111		return nil, errors.New("parameter serviceName cannot be empty")
112	}
113	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
114	if notificationName == "" {
115		return nil, errors.New("parameter notificationName cannot be empty")
116	}
117	urlPath = strings.ReplaceAll(urlPath, "{notificationName}", url.PathEscape(string(notificationName)))
118	if userID == "" {
119		return nil, errors.New("parameter userID cannot be empty")
120	}
121	urlPath = strings.ReplaceAll(urlPath, "{userId}", url.PathEscape(userID))
122	if client.subscriptionID == "" {
123		return nil, errors.New("parameter client.subscriptionID cannot be empty")
124	}
125	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
126	req, err := azcore.NewRequest(ctx, http.MethodPut, azcore.JoinPaths(client.con.Endpoint(), urlPath))
127	if err != nil {
128		return nil, err
129	}
130	req.Telemetry(telemetryInfo)
131	reqQP := req.URL.Query()
132	reqQP.Set("api-version", "2020-12-01")
133	req.URL.RawQuery = reqQP.Encode()
134	req.Header.Set("Accept", "application/json")
135	return req, nil
136}
137
138// createOrUpdateHandleResponse handles the CreateOrUpdate response.
139func (client *NotificationRecipientUserClient) createOrUpdateHandleResponse(resp *azcore.Response) (NotificationRecipientUserCreateOrUpdateResponse, error) {
140	result := NotificationRecipientUserCreateOrUpdateResponse{RawResponse: resp.Response}
141	if err := resp.UnmarshalAsJSON(&result.RecipientUserContract); err != nil {
142		return NotificationRecipientUserCreateOrUpdateResponse{}, err
143	}
144	return result, nil
145}
146
147// createOrUpdateHandleError handles the CreateOrUpdate error response.
148func (client *NotificationRecipientUserClient) createOrUpdateHandleError(resp *azcore.Response) error {
149	body, err := resp.Payload()
150	if err != nil {
151		return azcore.NewResponseError(err, resp.Response)
152	}
153	errType := ErrorResponse{raw: string(body)}
154	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
155		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
156	}
157	return azcore.NewResponseError(&errType, resp.Response)
158}
159
160// Delete - Removes the API Management user from the list of Notification.
161// If the operation fails it returns the *ErrorResponse error type.
162func (client *NotificationRecipientUserClient) Delete(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName, userID string, options *NotificationRecipientUserDeleteOptions) (NotificationRecipientUserDeleteResponse, error) {
163	req, err := client.deleteCreateRequest(ctx, resourceGroupName, serviceName, notificationName, userID, options)
164	if err != nil {
165		return NotificationRecipientUserDeleteResponse{}, err
166	}
167	resp, err := client.con.Pipeline().Do(req)
168	if err != nil {
169		return NotificationRecipientUserDeleteResponse{}, err
170	}
171	if !resp.HasStatusCode(http.StatusOK, http.StatusNoContent) {
172		return NotificationRecipientUserDeleteResponse{}, client.deleteHandleError(resp)
173	}
174	return NotificationRecipientUserDeleteResponse{RawResponse: resp.Response}, nil
175}
176
177// deleteCreateRequest creates the Delete request.
178func (client *NotificationRecipientUserClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName, userID string, options *NotificationRecipientUserDeleteOptions) (*azcore.Request, error) {
179	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications/{notificationName}/recipientUsers/{userId}"
180	if resourceGroupName == "" {
181		return nil, errors.New("parameter resourceGroupName cannot be empty")
182	}
183	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
184	if serviceName == "" {
185		return nil, errors.New("parameter serviceName cannot be empty")
186	}
187	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
188	if notificationName == "" {
189		return nil, errors.New("parameter notificationName cannot be empty")
190	}
191	urlPath = strings.ReplaceAll(urlPath, "{notificationName}", url.PathEscape(string(notificationName)))
192	if userID == "" {
193		return nil, errors.New("parameter userID cannot be empty")
194	}
195	urlPath = strings.ReplaceAll(urlPath, "{userId}", url.PathEscape(userID))
196	if client.subscriptionID == "" {
197		return nil, errors.New("parameter client.subscriptionID cannot be empty")
198	}
199	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
200	req, err := azcore.NewRequest(ctx, http.MethodDelete, azcore.JoinPaths(client.con.Endpoint(), urlPath))
201	if err != nil {
202		return nil, err
203	}
204	req.Telemetry(telemetryInfo)
205	reqQP := req.URL.Query()
206	reqQP.Set("api-version", "2020-12-01")
207	req.URL.RawQuery = reqQP.Encode()
208	req.Header.Set("Accept", "application/json")
209	return req, nil
210}
211
212// deleteHandleError handles the Delete error response.
213func (client *NotificationRecipientUserClient) deleteHandleError(resp *azcore.Response) error {
214	body, err := resp.Payload()
215	if err != nil {
216		return azcore.NewResponseError(err, resp.Response)
217	}
218	errType := ErrorResponse{raw: string(body)}
219	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
220		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
221	}
222	return azcore.NewResponseError(&errType, resp.Response)
223}
224
225// ListByNotification - Gets the list of the Notification Recipient User subscribed to the notification.
226// If the operation fails it returns the *ErrorResponse error type.
227func (client *NotificationRecipientUserClient) ListByNotification(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName, options *NotificationRecipientUserListByNotificationOptions) (NotificationRecipientUserListByNotificationResponse, error) {
228	req, err := client.listByNotificationCreateRequest(ctx, resourceGroupName, serviceName, notificationName, options)
229	if err != nil {
230		return NotificationRecipientUserListByNotificationResponse{}, err
231	}
232	resp, err := client.con.Pipeline().Do(req)
233	if err != nil {
234		return NotificationRecipientUserListByNotificationResponse{}, err
235	}
236	if !resp.HasStatusCode(http.StatusOK) {
237		return NotificationRecipientUserListByNotificationResponse{}, client.listByNotificationHandleError(resp)
238	}
239	return client.listByNotificationHandleResponse(resp)
240}
241
242// listByNotificationCreateRequest creates the ListByNotification request.
243func (client *NotificationRecipientUserClient) listByNotificationCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName, options *NotificationRecipientUserListByNotificationOptions) (*azcore.Request, error) {
244	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications/{notificationName}/recipientUsers"
245	if resourceGroupName == "" {
246		return nil, errors.New("parameter resourceGroupName cannot be empty")
247	}
248	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
249	if serviceName == "" {
250		return nil, errors.New("parameter serviceName cannot be empty")
251	}
252	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
253	if notificationName == "" {
254		return nil, errors.New("parameter notificationName cannot be empty")
255	}
256	urlPath = strings.ReplaceAll(urlPath, "{notificationName}", url.PathEscape(string(notificationName)))
257	if client.subscriptionID == "" {
258		return nil, errors.New("parameter client.subscriptionID cannot be empty")
259	}
260	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
261	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
262	if err != nil {
263		return nil, err
264	}
265	req.Telemetry(telemetryInfo)
266	reqQP := req.URL.Query()
267	reqQP.Set("api-version", "2020-12-01")
268	req.URL.RawQuery = reqQP.Encode()
269	req.Header.Set("Accept", "application/json")
270	return req, nil
271}
272
273// listByNotificationHandleResponse handles the ListByNotification response.
274func (client *NotificationRecipientUserClient) listByNotificationHandleResponse(resp *azcore.Response) (NotificationRecipientUserListByNotificationResponse, error) {
275	result := NotificationRecipientUserListByNotificationResponse{RawResponse: resp.Response}
276	if err := resp.UnmarshalAsJSON(&result.RecipientUserCollection); err != nil {
277		return NotificationRecipientUserListByNotificationResponse{}, err
278	}
279	return result, nil
280}
281
282// listByNotificationHandleError handles the ListByNotification error response.
283func (client *NotificationRecipientUserClient) listByNotificationHandleError(resp *azcore.Response) error {
284	body, err := resp.Payload()
285	if err != nil {
286		return azcore.NewResponseError(err, resp.Response)
287	}
288	errType := ErrorResponse{raw: string(body)}
289	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
290		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
291	}
292	return azcore.NewResponseError(&errType, resp.Response)
293}
294