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 "time" 21) 22 23// APIClient contains the methods for the API group. 24// Don't use this type directly, use NewAPIClient() instead. 25type APIClient struct { 26 con *armcore.Connection 27 subscriptionID string 28} 29 30// NewAPIClient creates a new instance of APIClient with the specified values. 31func NewAPIClient(con *armcore.Connection, subscriptionID string) *APIClient { 32 return &APIClient{con: con, subscriptionID: subscriptionID} 33} 34 35// BeginCreateOrUpdate - Creates new or updates existing specified API of the API Management service instance. 36// If the operation fails it returns the *ErrorResponse error type. 37func (client *APIClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, serviceName string, apiID string, parameters APICreateOrUpdateParameter, options *APIBeginCreateOrUpdateOptions) (APICreateOrUpdatePollerResponse, error) { 38 resp, err := client.createOrUpdate(ctx, resourceGroupName, serviceName, apiID, parameters, options) 39 if err != nil { 40 return APICreateOrUpdatePollerResponse{}, err 41 } 42 result := APICreateOrUpdatePollerResponse{ 43 RawResponse: resp.Response, 44 } 45 pt, err := armcore.NewLROPoller("APIClient.CreateOrUpdate", "location", resp, client.con.Pipeline(), client.createOrUpdateHandleError) 46 if err != nil { 47 return APICreateOrUpdatePollerResponse{}, err 48 } 49 poller := &apiCreateOrUpdatePoller{ 50 pt: pt, 51 } 52 result.Poller = poller 53 result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (APICreateOrUpdateResponse, error) { 54 return poller.pollUntilDone(ctx, frequency) 55 } 56 return result, nil 57} 58 59// ResumeCreateOrUpdate creates a new APICreateOrUpdatePoller from the specified resume token. 60// token - The value must come from a previous call to APICreateOrUpdatePoller.ResumeToken(). 61func (client *APIClient) ResumeCreateOrUpdate(ctx context.Context, token string) (APICreateOrUpdatePollerResponse, error) { 62 pt, err := armcore.NewLROPollerFromResumeToken("APIClient.CreateOrUpdate", token, client.con.Pipeline(), client.createOrUpdateHandleError) 63 if err != nil { 64 return APICreateOrUpdatePollerResponse{}, err 65 } 66 poller := &apiCreateOrUpdatePoller{ 67 pt: pt, 68 } 69 resp, err := poller.Poll(ctx) 70 if err != nil { 71 return APICreateOrUpdatePollerResponse{}, err 72 } 73 result := APICreateOrUpdatePollerResponse{ 74 RawResponse: resp, 75 } 76 result.Poller = poller 77 result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (APICreateOrUpdateResponse, error) { 78 return poller.pollUntilDone(ctx, frequency) 79 } 80 return result, nil 81} 82 83// CreateOrUpdate - Creates new or updates existing specified API of the API Management service instance. 84// If the operation fails it returns the *ErrorResponse error type. 85func (client *APIClient) createOrUpdate(ctx context.Context, resourceGroupName string, serviceName string, apiID string, parameters APICreateOrUpdateParameter, options *APIBeginCreateOrUpdateOptions) (*azcore.Response, error) { 86 req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, serviceName, apiID, parameters, options) 87 if err != nil { 88 return nil, err 89 } 90 resp, err := client.con.Pipeline().Do(req) 91 if err != nil { 92 return nil, err 93 } 94 if !resp.HasStatusCode(http.StatusOK, http.StatusCreated, http.StatusAccepted) { 95 return nil, client.createOrUpdateHandleError(resp) 96 } 97 return resp, nil 98} 99 100// createOrUpdateCreateRequest creates the CreateOrUpdate request. 101func (client *APIClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, apiID string, parameters APICreateOrUpdateParameter, options *APIBeginCreateOrUpdateOptions) (*azcore.Request, error) { 102 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}" 103 if resourceGroupName == "" { 104 return nil, errors.New("parameter resourceGroupName cannot be empty") 105 } 106 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 107 if serviceName == "" { 108 return nil, errors.New("parameter serviceName cannot be empty") 109 } 110 urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName)) 111 if apiID == "" { 112 return nil, errors.New("parameter apiID cannot be empty") 113 } 114 urlPath = strings.ReplaceAll(urlPath, "{apiId}", url.PathEscape(apiID)) 115 if client.subscriptionID == "" { 116 return nil, errors.New("parameter client.subscriptionID cannot be empty") 117 } 118 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 119 req, err := azcore.NewRequest(ctx, http.MethodPut, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 120 if err != nil { 121 return nil, err 122 } 123 req.Telemetry(telemetryInfo) 124 reqQP := req.URL.Query() 125 reqQP.Set("api-version", "2020-12-01") 126 req.URL.RawQuery = reqQP.Encode() 127 if options != nil && options.IfMatch != nil { 128 req.Header.Set("If-Match", *options.IfMatch) 129 } 130 req.Header.Set("Accept", "application/json") 131 return req, req.MarshalAsJSON(parameters) 132} 133 134// createOrUpdateHandleError handles the CreateOrUpdate error response. 135func (client *APIClient) createOrUpdateHandleError(resp *azcore.Response) error { 136 body, err := resp.Payload() 137 if err != nil { 138 return azcore.NewResponseError(err, resp.Response) 139 } 140 errType := ErrorResponse{raw: string(body)} 141 if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil { 142 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 143 } 144 return azcore.NewResponseError(&errType, resp.Response) 145} 146 147// Delete - Deletes the specified API of the API Management service instance. 148// If the operation fails it returns the *ErrorResponse error type. 149func (client *APIClient) Delete(ctx context.Context, resourceGroupName string, serviceName string, apiID string, ifMatch string, options *APIDeleteOptions) (APIDeleteResponse, error) { 150 req, err := client.deleteCreateRequest(ctx, resourceGroupName, serviceName, apiID, ifMatch, options) 151 if err != nil { 152 return APIDeleteResponse{}, err 153 } 154 resp, err := client.con.Pipeline().Do(req) 155 if err != nil { 156 return APIDeleteResponse{}, err 157 } 158 if !resp.HasStatusCode(http.StatusOK, http.StatusNoContent) { 159 return APIDeleteResponse{}, client.deleteHandleError(resp) 160 } 161 return APIDeleteResponse{RawResponse: resp.Response}, nil 162} 163 164// deleteCreateRequest creates the Delete request. 165func (client *APIClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, apiID string, ifMatch string, options *APIDeleteOptions) (*azcore.Request, error) { 166 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}" 167 if resourceGroupName == "" { 168 return nil, errors.New("parameter resourceGroupName cannot be empty") 169 } 170 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 171 if serviceName == "" { 172 return nil, errors.New("parameter serviceName cannot be empty") 173 } 174 urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName)) 175 if apiID == "" { 176 return nil, errors.New("parameter apiID cannot be empty") 177 } 178 urlPath = strings.ReplaceAll(urlPath, "{apiId}", url.PathEscape(apiID)) 179 if client.subscriptionID == "" { 180 return nil, errors.New("parameter client.subscriptionID cannot be empty") 181 } 182 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 183 req, err := azcore.NewRequest(ctx, http.MethodDelete, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 184 if err != nil { 185 return nil, err 186 } 187 req.Telemetry(telemetryInfo) 188 reqQP := req.URL.Query() 189 if options != nil && options.DeleteRevisions != nil { 190 reqQP.Set("deleteRevisions", strconv.FormatBool(*options.DeleteRevisions)) 191 } 192 reqQP.Set("api-version", "2020-12-01") 193 req.URL.RawQuery = reqQP.Encode() 194 req.Header.Set("If-Match", ifMatch) 195 req.Header.Set("Accept", "application/json") 196 return req, nil 197} 198 199// deleteHandleError handles the Delete error response. 200func (client *APIClient) deleteHandleError(resp *azcore.Response) error { 201 body, err := resp.Payload() 202 if err != nil { 203 return azcore.NewResponseError(err, resp.Response) 204 } 205 errType := ErrorResponse{raw: string(body)} 206 if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil { 207 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 208 } 209 return azcore.NewResponseError(&errType, resp.Response) 210} 211 212// Get - Gets the details of the API specified by its identifier. 213// If the operation fails it returns one of the following error types. 214// - *ErrorResponse, *ErrorResponse 215func (client *APIClient) Get(ctx context.Context, resourceGroupName string, serviceName string, apiID string, options *APIGetOptions) (APIGetResponse, error) { 216 req, err := client.getCreateRequest(ctx, resourceGroupName, serviceName, apiID, options) 217 if err != nil { 218 return APIGetResponse{}, err 219 } 220 resp, err := client.con.Pipeline().Do(req) 221 if err != nil { 222 return APIGetResponse{}, err 223 } 224 if !resp.HasStatusCode(http.StatusOK, http.StatusOK) { 225 return APIGetResponse{}, client.getHandleError(resp) 226 } 227 return client.getHandleResponse(resp) 228} 229 230// getCreateRequest creates the Get request. 231func (client *APIClient) getCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, apiID string, options *APIGetOptions) (*azcore.Request, error) { 232 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}" 233 if resourceGroupName == "" { 234 return nil, errors.New("parameter resourceGroupName cannot be empty") 235 } 236 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 237 if serviceName == "" { 238 return nil, errors.New("parameter serviceName cannot be empty") 239 } 240 urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName)) 241 if apiID == "" { 242 return nil, errors.New("parameter apiID cannot be empty") 243 } 244 urlPath = strings.ReplaceAll(urlPath, "{apiId}", url.PathEscape(apiID)) 245 if client.subscriptionID == "" { 246 return nil, errors.New("parameter client.subscriptionID cannot be empty") 247 } 248 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 249 req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 250 if err != nil { 251 return nil, err 252 } 253 req.Telemetry(telemetryInfo) 254 reqQP := req.URL.Query() 255 reqQP.Set("api-version", "2020-12-01") 256 req.URL.RawQuery = reqQP.Encode() 257 req.Header.Set("Accept", "application/json, application/vnd.swagger.doc+json, application/vnd.oai.openapi+json, application/vnd.sun.wadl+xml, application/wsdl+xml") 258 return req, nil 259} 260 261// getHandleResponse handles the Get response. 262func (client *APIClient) getHandleResponse(resp *azcore.Response) (APIGetResponse, error) { 263 result := APIGetResponse{RawResponse: resp.Response} 264 if val := resp.Header.Get("ETag"); val != "" { 265 result.ETag = &val 266 } 267 if err := resp.UnmarshalAsJSON(&result.APIContract); err != nil { 268 return APIGetResponse{}, err 269 } 270 return result, nil 271} 272 273// getHandleError handles the Get error response. 274func (client *APIClient) getHandleError(resp *azcore.Response) error { 275 body, err := resp.Payload() 276 if err != nil { 277 return azcore.NewResponseError(err, resp.Response) 278 } 279 errType := ErrorResponse{raw: string(body)} 280 if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil { 281 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 282 } 283 return azcore.NewResponseError(&errType, resp.Response) 284} 285 286// GetEntityTag - Gets the entity state (Etag) version of the API specified by its identifier. 287// If the operation fails it returns the *ErrorResponse error type. 288func (client *APIClient) GetEntityTag(ctx context.Context, resourceGroupName string, serviceName string, apiID string, options *APIGetEntityTagOptions) (APIGetEntityTagResponse, error) { 289 req, err := client.getEntityTagCreateRequest(ctx, resourceGroupName, serviceName, apiID, options) 290 if err != nil { 291 return APIGetEntityTagResponse{}, err 292 } 293 resp, err := client.con.Pipeline().Do(req) 294 if err != nil { 295 return APIGetEntityTagResponse{}, err 296 } 297 return client.getEntityTagHandleResponse(resp) 298} 299 300// getEntityTagCreateRequest creates the GetEntityTag request. 301func (client *APIClient) getEntityTagCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, apiID string, options *APIGetEntityTagOptions) (*azcore.Request, error) { 302 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}" 303 if resourceGroupName == "" { 304 return nil, errors.New("parameter resourceGroupName cannot be empty") 305 } 306 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 307 if serviceName == "" { 308 return nil, errors.New("parameter serviceName cannot be empty") 309 } 310 urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName)) 311 if apiID == "" { 312 return nil, errors.New("parameter apiID cannot be empty") 313 } 314 urlPath = strings.ReplaceAll(urlPath, "{apiId}", url.PathEscape(apiID)) 315 if client.subscriptionID == "" { 316 return nil, errors.New("parameter client.subscriptionID cannot be empty") 317 } 318 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 319 req, err := azcore.NewRequest(ctx, http.MethodHead, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 320 if err != nil { 321 return nil, err 322 } 323 req.Telemetry(telemetryInfo) 324 reqQP := req.URL.Query() 325 reqQP.Set("api-version", "2020-12-01") 326 req.URL.RawQuery = reqQP.Encode() 327 req.Header.Set("Accept", "application/json") 328 return req, nil 329} 330 331// getEntityTagHandleResponse handles the GetEntityTag response. 332func (client *APIClient) getEntityTagHandleResponse(resp *azcore.Response) (APIGetEntityTagResponse, error) { 333 result := APIGetEntityTagResponse{RawResponse: resp.Response} 334 if val := resp.Header.Get("ETag"); val != "" { 335 result.ETag = &val 336 } 337 if resp.StatusCode >= 200 && resp.StatusCode < 300 { 338 result.Success = true 339 } 340 return result, nil 341} 342 343// ListByService - Lists all APIs of the API Management service instance. 344// If the operation fails it returns the *ErrorResponse error type. 345func (client *APIClient) ListByService(resourceGroupName string, serviceName string, options *APIListByServiceOptions) APIListByServicePager { 346 return &apiListByServicePager{ 347 client: client, 348 requester: func(ctx context.Context) (*azcore.Request, error) { 349 return client.listByServiceCreateRequest(ctx, resourceGroupName, serviceName, options) 350 }, 351 advancer: func(ctx context.Context, resp APIListByServiceResponse) (*azcore.Request, error) { 352 return azcore.NewRequest(ctx, http.MethodGet, *resp.APICollection.NextLink) 353 }, 354 } 355} 356 357// listByServiceCreateRequest creates the ListByService request. 358func (client *APIClient) listByServiceCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, options *APIListByServiceOptions) (*azcore.Request, error) { 359 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis" 360 if resourceGroupName == "" { 361 return nil, errors.New("parameter resourceGroupName cannot be empty") 362 } 363 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 364 if serviceName == "" { 365 return nil, errors.New("parameter serviceName cannot be empty") 366 } 367 urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName)) 368 if client.subscriptionID == "" { 369 return nil, errors.New("parameter client.subscriptionID cannot be empty") 370 } 371 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 372 req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 373 if err != nil { 374 return nil, err 375 } 376 req.Telemetry(telemetryInfo) 377 reqQP := req.URL.Query() 378 if options != nil && options.Filter != nil { 379 reqQP.Set("$filter", *options.Filter) 380 } 381 if options != nil && options.Top != nil { 382 reqQP.Set("$top", strconv.FormatInt(int64(*options.Top), 10)) 383 } 384 if options != nil && options.Skip != nil { 385 reqQP.Set("$skip", strconv.FormatInt(int64(*options.Skip), 10)) 386 } 387 if options != nil && options.Tags != nil { 388 reqQP.Set("tags", *options.Tags) 389 } 390 if options != nil && options.ExpandAPIVersionSet != nil { 391 reqQP.Set("expandApiVersionSet", strconv.FormatBool(*options.ExpandAPIVersionSet)) 392 } 393 reqQP.Set("api-version", "2020-12-01") 394 req.URL.RawQuery = reqQP.Encode() 395 req.Header.Set("Accept", "application/json") 396 return req, nil 397} 398 399// listByServiceHandleResponse handles the ListByService response. 400func (client *APIClient) listByServiceHandleResponse(resp *azcore.Response) (APIListByServiceResponse, error) { 401 result := APIListByServiceResponse{RawResponse: resp.Response} 402 if err := resp.UnmarshalAsJSON(&result.APICollection); err != nil { 403 return APIListByServiceResponse{}, err 404 } 405 return result, nil 406} 407 408// listByServiceHandleError handles the ListByService error response. 409func (client *APIClient) listByServiceHandleError(resp *azcore.Response) error { 410 body, err := resp.Payload() 411 if err != nil { 412 return azcore.NewResponseError(err, resp.Response) 413 } 414 errType := ErrorResponse{raw: string(body)} 415 if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil { 416 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 417 } 418 return azcore.NewResponseError(&errType, resp.Response) 419} 420 421// ListByTags - Lists a collection of apis associated with tags. 422// If the operation fails it returns the *ErrorResponse error type. 423func (client *APIClient) ListByTags(resourceGroupName string, serviceName string, options *APIListByTagsOptions) APIListByTagsPager { 424 return &apiListByTagsPager{ 425 client: client, 426 requester: func(ctx context.Context) (*azcore.Request, error) { 427 return client.listByTagsCreateRequest(ctx, resourceGroupName, serviceName, options) 428 }, 429 advancer: func(ctx context.Context, resp APIListByTagsResponse) (*azcore.Request, error) { 430 return azcore.NewRequest(ctx, http.MethodGet, *resp.TagResourceCollection.NextLink) 431 }, 432 } 433} 434 435// listByTagsCreateRequest creates the ListByTags request. 436func (client *APIClient) listByTagsCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, options *APIListByTagsOptions) (*azcore.Request, error) { 437 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apisByTags" 438 if resourceGroupName == "" { 439 return nil, errors.New("parameter resourceGroupName cannot be empty") 440 } 441 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 442 if serviceName == "" { 443 return nil, errors.New("parameter serviceName cannot be empty") 444 } 445 urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName)) 446 if client.subscriptionID == "" { 447 return nil, errors.New("parameter client.subscriptionID cannot be empty") 448 } 449 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 450 req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 451 if err != nil { 452 return nil, err 453 } 454 req.Telemetry(telemetryInfo) 455 reqQP := req.URL.Query() 456 if options != nil && options.Filter != nil { 457 reqQP.Set("$filter", *options.Filter) 458 } 459 if options != nil && options.Top != nil { 460 reqQP.Set("$top", strconv.FormatInt(int64(*options.Top), 10)) 461 } 462 if options != nil && options.Skip != nil { 463 reqQP.Set("$skip", strconv.FormatInt(int64(*options.Skip), 10)) 464 } 465 if options != nil && options.IncludeNotTaggedApis != nil { 466 reqQP.Set("includeNotTaggedApis", strconv.FormatBool(*options.IncludeNotTaggedApis)) 467 } 468 reqQP.Set("api-version", "2020-12-01") 469 req.URL.RawQuery = reqQP.Encode() 470 req.Header.Set("Accept", "application/json") 471 return req, nil 472} 473 474// listByTagsHandleResponse handles the ListByTags response. 475func (client *APIClient) listByTagsHandleResponse(resp *azcore.Response) (APIListByTagsResponse, error) { 476 result := APIListByTagsResponse{RawResponse: resp.Response} 477 if err := resp.UnmarshalAsJSON(&result.TagResourceCollection); err != nil { 478 return APIListByTagsResponse{}, err 479 } 480 return result, nil 481} 482 483// listByTagsHandleError handles the ListByTags error response. 484func (client *APIClient) listByTagsHandleError(resp *azcore.Response) error { 485 body, err := resp.Payload() 486 if err != nil { 487 return azcore.NewResponseError(err, resp.Response) 488 } 489 errType := ErrorResponse{raw: string(body)} 490 if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil { 491 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 492 } 493 return azcore.NewResponseError(&errType, resp.Response) 494} 495 496// Update - Updates the specified API of the API Management service instance. 497// If the operation fails it returns the *ErrorResponse error type. 498func (client *APIClient) Update(ctx context.Context, resourceGroupName string, serviceName string, apiID string, ifMatch string, parameters APIUpdateContract, options *APIUpdateOptions) (APIUpdateResponse, error) { 499 req, err := client.updateCreateRequest(ctx, resourceGroupName, serviceName, apiID, ifMatch, parameters, options) 500 if err != nil { 501 return APIUpdateResponse{}, err 502 } 503 resp, err := client.con.Pipeline().Do(req) 504 if err != nil { 505 return APIUpdateResponse{}, err 506 } 507 if !resp.HasStatusCode(http.StatusOK) { 508 return APIUpdateResponse{}, client.updateHandleError(resp) 509 } 510 return client.updateHandleResponse(resp) 511} 512 513// updateCreateRequest creates the Update request. 514func (client *APIClient) updateCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, apiID string, ifMatch string, parameters APIUpdateContract, options *APIUpdateOptions) (*azcore.Request, error) { 515 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}" 516 if resourceGroupName == "" { 517 return nil, errors.New("parameter resourceGroupName cannot be empty") 518 } 519 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 520 if serviceName == "" { 521 return nil, errors.New("parameter serviceName cannot be empty") 522 } 523 urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName)) 524 if apiID == "" { 525 return nil, errors.New("parameter apiID cannot be empty") 526 } 527 urlPath = strings.ReplaceAll(urlPath, "{apiId}", url.PathEscape(apiID)) 528 if client.subscriptionID == "" { 529 return nil, errors.New("parameter client.subscriptionID cannot be empty") 530 } 531 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 532 req, err := azcore.NewRequest(ctx, http.MethodPatch, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 533 if err != nil { 534 return nil, err 535 } 536 req.Telemetry(telemetryInfo) 537 reqQP := req.URL.Query() 538 reqQP.Set("api-version", "2020-12-01") 539 req.URL.RawQuery = reqQP.Encode() 540 req.Header.Set("If-Match", ifMatch) 541 req.Header.Set("Accept", "application/json") 542 return req, req.MarshalAsJSON(parameters) 543} 544 545// updateHandleResponse handles the Update response. 546func (client *APIClient) updateHandleResponse(resp *azcore.Response) (APIUpdateResponse, error) { 547 result := APIUpdateResponse{RawResponse: resp.Response} 548 if val := resp.Header.Get("ETag"); val != "" { 549 result.ETag = &val 550 } 551 if err := resp.UnmarshalAsJSON(&result.APIContract); err != nil { 552 return APIUpdateResponse{}, err 553 } 554 return result, nil 555} 556 557// updateHandleError handles the Update error response. 558func (client *APIClient) updateHandleError(resp *azcore.Response) error { 559 body, err := resp.Payload() 560 if err != nil { 561 return azcore.NewResponseError(err, resp.Response) 562 } 563 errType := ErrorResponse{raw: string(body)} 564 if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil { 565 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 566 } 567 return azcore.NewResponseError(&errType, resp.Response) 568} 569