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// APIIssueCommentClient contains the methods for the APIIssueComment group.
23// Don't use this type directly, use NewAPIIssueCommentClient() instead.
24type APIIssueCommentClient struct {
25	con            *armcore.Connection
26	subscriptionID string
27}
28
29// NewAPIIssueCommentClient creates a new instance of APIIssueCommentClient with the specified values.
30func NewAPIIssueCommentClient(con *armcore.Connection, subscriptionID string) *APIIssueCommentClient {
31	return &APIIssueCommentClient{con: con, subscriptionID: subscriptionID}
32}
33
34// CreateOrUpdate - Creates a new Comment for the Issue in an API or updates an existing one.
35// If the operation fails it returns the *ErrorResponse error type.
36func (client *APIIssueCommentClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serviceName string, apiID string, issueID string, commentID string, parameters IssueCommentContract, options *APIIssueCommentCreateOrUpdateOptions) (APIIssueCommentCreateOrUpdateResponse, error) {
37	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, serviceName, apiID, issueID, commentID, parameters, options)
38	if err != nil {
39		return APIIssueCommentCreateOrUpdateResponse{}, err
40	}
41	resp, err := client.con.Pipeline().Do(req)
42	if err != nil {
43		return APIIssueCommentCreateOrUpdateResponse{}, err
44	}
45	if !resp.HasStatusCode(http.StatusOK, http.StatusCreated) {
46		return APIIssueCommentCreateOrUpdateResponse{}, client.createOrUpdateHandleError(resp)
47	}
48	return client.createOrUpdateHandleResponse(resp)
49}
50
51// createOrUpdateCreateRequest creates the CreateOrUpdate request.
52func (client *APIIssueCommentClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, apiID string, issueID string, commentID string, parameters IssueCommentContract, options *APIIssueCommentCreateOrUpdateOptions) (*azcore.Request, error) {
53	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}/issues/{issueId}/comments/{commentId}"
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 apiID == "" {
63		return nil, errors.New("parameter apiID cannot be empty")
64	}
65	urlPath = strings.ReplaceAll(urlPath, "{apiId}", url.PathEscape(apiID))
66	if issueID == "" {
67		return nil, errors.New("parameter issueID cannot be empty")
68	}
69	urlPath = strings.ReplaceAll(urlPath, "{issueId}", url.PathEscape(issueID))
70	if commentID == "" {
71		return nil, errors.New("parameter commentID cannot be empty")
72	}
73	urlPath = strings.ReplaceAll(urlPath, "{commentId}", url.PathEscape(commentID))
74	if client.subscriptionID == "" {
75		return nil, errors.New("parameter client.subscriptionID cannot be empty")
76	}
77	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
78	req, err := azcore.NewRequest(ctx, http.MethodPut, azcore.JoinPaths(client.con.Endpoint(), urlPath))
79	if err != nil {
80		return nil, err
81	}
82	req.Telemetry(telemetryInfo)
83	reqQP := req.URL.Query()
84	reqQP.Set("api-version", "2020-12-01")
85	req.URL.RawQuery = reqQP.Encode()
86	if options != nil && options.IfMatch != nil {
87		req.Header.Set("If-Match", *options.IfMatch)
88	}
89	req.Header.Set("Accept", "application/json")
90	return req, req.MarshalAsJSON(parameters)
91}
92
93// createOrUpdateHandleResponse handles the CreateOrUpdate response.
94func (client *APIIssueCommentClient) createOrUpdateHandleResponse(resp *azcore.Response) (APIIssueCommentCreateOrUpdateResponse, error) {
95	result := APIIssueCommentCreateOrUpdateResponse{RawResponse: resp.Response}
96	if val := resp.Header.Get("ETag"); val != "" {
97		result.ETag = &val
98	}
99	if err := resp.UnmarshalAsJSON(&result.IssueCommentContract); err != nil {
100		return APIIssueCommentCreateOrUpdateResponse{}, err
101	}
102	return result, nil
103}
104
105// createOrUpdateHandleError handles the CreateOrUpdate error response.
106func (client *APIIssueCommentClient) createOrUpdateHandleError(resp *azcore.Response) error {
107	body, err := resp.Payload()
108	if err != nil {
109		return azcore.NewResponseError(err, resp.Response)
110	}
111	errType := ErrorResponse{raw: string(body)}
112	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
113		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
114	}
115	return azcore.NewResponseError(&errType, resp.Response)
116}
117
118// Delete - Deletes the specified comment from an Issue.
119// If the operation fails it returns the *ErrorResponse error type.
120func (client *APIIssueCommentClient) Delete(ctx context.Context, resourceGroupName string, serviceName string, apiID string, issueID string, commentID string, ifMatch string, options *APIIssueCommentDeleteOptions) (APIIssueCommentDeleteResponse, error) {
121	req, err := client.deleteCreateRequest(ctx, resourceGroupName, serviceName, apiID, issueID, commentID, ifMatch, options)
122	if err != nil {
123		return APIIssueCommentDeleteResponse{}, err
124	}
125	resp, err := client.con.Pipeline().Do(req)
126	if err != nil {
127		return APIIssueCommentDeleteResponse{}, err
128	}
129	if !resp.HasStatusCode(http.StatusOK, http.StatusNoContent) {
130		return APIIssueCommentDeleteResponse{}, client.deleteHandleError(resp)
131	}
132	return APIIssueCommentDeleteResponse{RawResponse: resp.Response}, nil
133}
134
135// deleteCreateRequest creates the Delete request.
136func (client *APIIssueCommentClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, apiID string, issueID string, commentID string, ifMatch string, options *APIIssueCommentDeleteOptions) (*azcore.Request, error) {
137	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}/issues/{issueId}/comments/{commentId}"
138	if resourceGroupName == "" {
139		return nil, errors.New("parameter resourceGroupName cannot be empty")
140	}
141	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
142	if serviceName == "" {
143		return nil, errors.New("parameter serviceName cannot be empty")
144	}
145	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
146	if apiID == "" {
147		return nil, errors.New("parameter apiID cannot be empty")
148	}
149	urlPath = strings.ReplaceAll(urlPath, "{apiId}", url.PathEscape(apiID))
150	if issueID == "" {
151		return nil, errors.New("parameter issueID cannot be empty")
152	}
153	urlPath = strings.ReplaceAll(urlPath, "{issueId}", url.PathEscape(issueID))
154	if commentID == "" {
155		return nil, errors.New("parameter commentID cannot be empty")
156	}
157	urlPath = strings.ReplaceAll(urlPath, "{commentId}", url.PathEscape(commentID))
158	if client.subscriptionID == "" {
159		return nil, errors.New("parameter client.subscriptionID cannot be empty")
160	}
161	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
162	req, err := azcore.NewRequest(ctx, http.MethodDelete, azcore.JoinPaths(client.con.Endpoint(), urlPath))
163	if err != nil {
164		return nil, err
165	}
166	req.Telemetry(telemetryInfo)
167	reqQP := req.URL.Query()
168	reqQP.Set("api-version", "2020-12-01")
169	req.URL.RawQuery = reqQP.Encode()
170	req.Header.Set("If-Match", ifMatch)
171	req.Header.Set("Accept", "application/json")
172	return req, nil
173}
174
175// deleteHandleError handles the Delete error response.
176func (client *APIIssueCommentClient) deleteHandleError(resp *azcore.Response) error {
177	body, err := resp.Payload()
178	if err != nil {
179		return azcore.NewResponseError(err, resp.Response)
180	}
181	errType := ErrorResponse{raw: string(body)}
182	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
183		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
184	}
185	return azcore.NewResponseError(&errType, resp.Response)
186}
187
188// Get - Gets the details of the issue Comment for an API specified by its identifier.
189// If the operation fails it returns the *ErrorResponse error type.
190func (client *APIIssueCommentClient) Get(ctx context.Context, resourceGroupName string, serviceName string, apiID string, issueID string, commentID string, options *APIIssueCommentGetOptions) (APIIssueCommentGetResponse, error) {
191	req, err := client.getCreateRequest(ctx, resourceGroupName, serviceName, apiID, issueID, commentID, options)
192	if err != nil {
193		return APIIssueCommentGetResponse{}, err
194	}
195	resp, err := client.con.Pipeline().Do(req)
196	if err != nil {
197		return APIIssueCommentGetResponse{}, err
198	}
199	if !resp.HasStatusCode(http.StatusOK) {
200		return APIIssueCommentGetResponse{}, client.getHandleError(resp)
201	}
202	return client.getHandleResponse(resp)
203}
204
205// getCreateRequest creates the Get request.
206func (client *APIIssueCommentClient) getCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, apiID string, issueID string, commentID string, options *APIIssueCommentGetOptions) (*azcore.Request, error) {
207	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}/issues/{issueId}/comments/{commentId}"
208	if resourceGroupName == "" {
209		return nil, errors.New("parameter resourceGroupName cannot be empty")
210	}
211	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
212	if serviceName == "" {
213		return nil, errors.New("parameter serviceName cannot be empty")
214	}
215	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
216	if apiID == "" {
217		return nil, errors.New("parameter apiID cannot be empty")
218	}
219	urlPath = strings.ReplaceAll(urlPath, "{apiId}", url.PathEscape(apiID))
220	if issueID == "" {
221		return nil, errors.New("parameter issueID cannot be empty")
222	}
223	urlPath = strings.ReplaceAll(urlPath, "{issueId}", url.PathEscape(issueID))
224	if commentID == "" {
225		return nil, errors.New("parameter commentID cannot be empty")
226	}
227	urlPath = strings.ReplaceAll(urlPath, "{commentId}", url.PathEscape(commentID))
228	if client.subscriptionID == "" {
229		return nil, errors.New("parameter client.subscriptionID cannot be empty")
230	}
231	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
232	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
233	if err != nil {
234		return nil, err
235	}
236	req.Telemetry(telemetryInfo)
237	reqQP := req.URL.Query()
238	reqQP.Set("api-version", "2020-12-01")
239	req.URL.RawQuery = reqQP.Encode()
240	req.Header.Set("Accept", "application/json")
241	return req, nil
242}
243
244// getHandleResponse handles the Get response.
245func (client *APIIssueCommentClient) getHandleResponse(resp *azcore.Response) (APIIssueCommentGetResponse, error) {
246	result := APIIssueCommentGetResponse{RawResponse: resp.Response}
247	if val := resp.Header.Get("ETag"); val != "" {
248		result.ETag = &val
249	}
250	if err := resp.UnmarshalAsJSON(&result.IssueCommentContract); err != nil {
251		return APIIssueCommentGetResponse{}, err
252	}
253	return result, nil
254}
255
256// getHandleError handles the Get error response.
257func (client *APIIssueCommentClient) getHandleError(resp *azcore.Response) error {
258	body, err := resp.Payload()
259	if err != nil {
260		return azcore.NewResponseError(err, resp.Response)
261	}
262	errType := ErrorResponse{raw: string(body)}
263	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
264		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
265	}
266	return azcore.NewResponseError(&errType, resp.Response)
267}
268
269// GetEntityTag - Gets the entity state (Etag) version of the issue Comment for an API specified by its identifier.
270// If the operation fails it returns the *ErrorResponse error type.
271func (client *APIIssueCommentClient) GetEntityTag(ctx context.Context, resourceGroupName string, serviceName string, apiID string, issueID string, commentID string, options *APIIssueCommentGetEntityTagOptions) (APIIssueCommentGetEntityTagResponse, error) {
272	req, err := client.getEntityTagCreateRequest(ctx, resourceGroupName, serviceName, apiID, issueID, commentID, options)
273	if err != nil {
274		return APIIssueCommentGetEntityTagResponse{}, err
275	}
276	resp, err := client.con.Pipeline().Do(req)
277	if err != nil {
278		return APIIssueCommentGetEntityTagResponse{}, err
279	}
280	return client.getEntityTagHandleResponse(resp)
281}
282
283// getEntityTagCreateRequest creates the GetEntityTag request.
284func (client *APIIssueCommentClient) getEntityTagCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, apiID string, issueID string, commentID string, options *APIIssueCommentGetEntityTagOptions) (*azcore.Request, error) {
285	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}/issues/{issueId}/comments/{commentId}"
286	if resourceGroupName == "" {
287		return nil, errors.New("parameter resourceGroupName cannot be empty")
288	}
289	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
290	if serviceName == "" {
291		return nil, errors.New("parameter serviceName cannot be empty")
292	}
293	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
294	if apiID == "" {
295		return nil, errors.New("parameter apiID cannot be empty")
296	}
297	urlPath = strings.ReplaceAll(urlPath, "{apiId}", url.PathEscape(apiID))
298	if issueID == "" {
299		return nil, errors.New("parameter issueID cannot be empty")
300	}
301	urlPath = strings.ReplaceAll(urlPath, "{issueId}", url.PathEscape(issueID))
302	if commentID == "" {
303		return nil, errors.New("parameter commentID cannot be empty")
304	}
305	urlPath = strings.ReplaceAll(urlPath, "{commentId}", url.PathEscape(commentID))
306	if client.subscriptionID == "" {
307		return nil, errors.New("parameter client.subscriptionID cannot be empty")
308	}
309	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
310	req, err := azcore.NewRequest(ctx, http.MethodHead, azcore.JoinPaths(client.con.Endpoint(), urlPath))
311	if err != nil {
312		return nil, err
313	}
314	req.Telemetry(telemetryInfo)
315	reqQP := req.URL.Query()
316	reqQP.Set("api-version", "2020-12-01")
317	req.URL.RawQuery = reqQP.Encode()
318	req.Header.Set("Accept", "application/json")
319	return req, nil
320}
321
322// getEntityTagHandleResponse handles the GetEntityTag response.
323func (client *APIIssueCommentClient) getEntityTagHandleResponse(resp *azcore.Response) (APIIssueCommentGetEntityTagResponse, error) {
324	result := APIIssueCommentGetEntityTagResponse{RawResponse: resp.Response}
325	if val := resp.Header.Get("ETag"); val != "" {
326		result.ETag = &val
327	}
328	if resp.StatusCode >= 200 && resp.StatusCode < 300 {
329		result.Success = true
330	}
331	return result, nil
332}
333
334// ListByService - Lists all comments for the Issue associated with the specified API.
335// If the operation fails it returns the *ErrorResponse error type.
336func (client *APIIssueCommentClient) ListByService(resourceGroupName string, serviceName string, apiID string, issueID string, options *APIIssueCommentListByServiceOptions) APIIssueCommentListByServicePager {
337	return &apiIssueCommentListByServicePager{
338		client: client,
339		requester: func(ctx context.Context) (*azcore.Request, error) {
340			return client.listByServiceCreateRequest(ctx, resourceGroupName, serviceName, apiID, issueID, options)
341		},
342		advancer: func(ctx context.Context, resp APIIssueCommentListByServiceResponse) (*azcore.Request, error) {
343			return azcore.NewRequest(ctx, http.MethodGet, *resp.IssueCommentCollection.NextLink)
344		},
345	}
346}
347
348// listByServiceCreateRequest creates the ListByService request.
349func (client *APIIssueCommentClient) listByServiceCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, apiID string, issueID string, options *APIIssueCommentListByServiceOptions) (*azcore.Request, error) {
350	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}/issues/{issueId}/comments"
351	if resourceGroupName == "" {
352		return nil, errors.New("parameter resourceGroupName cannot be empty")
353	}
354	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
355	if serviceName == "" {
356		return nil, errors.New("parameter serviceName cannot be empty")
357	}
358	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
359	if apiID == "" {
360		return nil, errors.New("parameter apiID cannot be empty")
361	}
362	urlPath = strings.ReplaceAll(urlPath, "{apiId}", url.PathEscape(apiID))
363	if issueID == "" {
364		return nil, errors.New("parameter issueID cannot be empty")
365	}
366	urlPath = strings.ReplaceAll(urlPath, "{issueId}", url.PathEscape(issueID))
367	if client.subscriptionID == "" {
368		return nil, errors.New("parameter client.subscriptionID cannot be empty")
369	}
370	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
371	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
372	if err != nil {
373		return nil, err
374	}
375	req.Telemetry(telemetryInfo)
376	reqQP := req.URL.Query()
377	if options != nil && options.Filter != nil {
378		reqQP.Set("$filter", *options.Filter)
379	}
380	if options != nil && options.Top != nil {
381		reqQP.Set("$top", strconv.FormatInt(int64(*options.Top), 10))
382	}
383	if options != nil && options.Skip != nil {
384		reqQP.Set("$skip", strconv.FormatInt(int64(*options.Skip), 10))
385	}
386	reqQP.Set("api-version", "2020-12-01")
387	req.URL.RawQuery = reqQP.Encode()
388	req.Header.Set("Accept", "application/json")
389	return req, nil
390}
391
392// listByServiceHandleResponse handles the ListByService response.
393func (client *APIIssueCommentClient) listByServiceHandleResponse(resp *azcore.Response) (APIIssueCommentListByServiceResponse, error) {
394	result := APIIssueCommentListByServiceResponse{RawResponse: resp.Response}
395	if err := resp.UnmarshalAsJSON(&result.IssueCommentCollection); err != nil {
396		return APIIssueCommentListByServiceResponse{}, err
397	}
398	return result, nil
399}
400
401// listByServiceHandleError handles the ListByService error response.
402func (client *APIIssueCommentClient) listByServiceHandleError(resp *azcore.Response) error {
403	body, err := resp.Payload()
404	if err != nil {
405		return azcore.NewResponseError(err, resp.Response)
406	}
407	errType := ErrorResponse{raw: string(body)}
408	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
409		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
410	}
411	return azcore.NewResponseError(&errType, resp.Response)
412}
413