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 armnetwork 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// UsagesClient contains the methods for the Usages group. 22// Don't use this type directly, use NewUsagesClient() instead. 23type UsagesClient struct { 24 con *armcore.Connection 25 subscriptionID string 26} 27 28// NewUsagesClient creates a new instance of UsagesClient with the specified values. 29func NewUsagesClient(con *armcore.Connection, subscriptionID string) *UsagesClient { 30 return &UsagesClient{con: con, subscriptionID: subscriptionID} 31} 32 33// List - List network usages for a subscription. 34// If the operation fails it returns the *CloudError error type. 35func (client *UsagesClient) List(location string, options *UsagesListOptions) UsagesListResultPager { 36 return &usagesListResultPager{ 37 pipeline: client.con.Pipeline(), 38 requester: func(ctx context.Context) (*azcore.Request, error) { 39 return client.listCreateRequest(ctx, location, options) 40 }, 41 responder: client.listHandleResponse, 42 errorer: client.listHandleError, 43 advancer: func(ctx context.Context, resp UsagesListResultResponse) (*azcore.Request, error) { 44 return azcore.NewRequest(ctx, http.MethodGet, *resp.UsagesListResult.NextLink) 45 }, 46 statusCodes: []int{http.StatusOK}, 47 } 48} 49 50// listCreateRequest creates the List request. 51func (client *UsagesClient) listCreateRequest(ctx context.Context, location string, options *UsagesListOptions) (*azcore.Request, error) { 52 urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Network/locations/{location}/usages" 53 if location == "" { 54 return nil, errors.New("parameter location cannot be empty") 55 } 56 urlPath = strings.ReplaceAll(urlPath, "{location}", url.PathEscape(location)) 57 if client.subscriptionID == "" { 58 return nil, errors.New("parameter client.subscriptionID cannot be empty") 59 } 60 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 61 req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 62 if err != nil { 63 return nil, err 64 } 65 req.Telemetry(telemetryInfo) 66 reqQP := req.URL.Query() 67 reqQP.Set("api-version", "2021-02-01") 68 req.URL.RawQuery = reqQP.Encode() 69 req.Header.Set("Accept", "application/json") 70 return req, nil 71} 72 73// listHandleResponse handles the List response. 74func (client *UsagesClient) listHandleResponse(resp *azcore.Response) (UsagesListResultResponse, error) { 75 var val *UsagesListResult 76 if err := resp.UnmarshalAsJSON(&val); err != nil { 77 return UsagesListResultResponse{}, err 78 } 79 return UsagesListResultResponse{RawResponse: resp.Response, UsagesListResult: val}, nil 80} 81 82// listHandleError handles the List error response. 83func (client *UsagesClient) listHandleError(resp *azcore.Response) error { 84 body, err := resp.Payload() 85 if err != nil { 86 return azcore.NewResponseError(err, resp.Response) 87 } 88 errType := CloudError{raw: string(body)} 89 if err := resp.UnmarshalAsJSON(&errType); err != nil { 90 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 91 } 92 return azcore.NewResponseError(&errType, resp.Response) 93} 94