github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/usages_client.go (about)

     1  //go:build go1.18
     2  // +build go1.18
     3  
     4  // Copyright (c) Microsoft Corporation. All rights reserved.
     5  // Licensed under the MIT License. See License.txt in the project root for license information.
     6  // Code generated by Microsoft (R) AutoRest Code Generator. DO NOT EDIT.
     7  // Changes may cause incorrect behavior and will be lost if the code is regenerated.
     8  
     9  package armnetwork
    10  
    11  import (
    12  	"context"
    13  	"errors"
    14  	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
    15  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
    16  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
    17  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
    18  	"net/http"
    19  	"net/url"
    20  	"strings"
    21  )
    22  
    23  // UsagesClient contains the methods for the Usages group.
    24  // Don't use this type directly, use NewUsagesClient() instead.
    25  type UsagesClient struct {
    26  	internal       *arm.Client
    27  	subscriptionID string
    28  }
    29  
    30  // NewUsagesClient creates a new instance of UsagesClient with the specified values.
    31  //   - subscriptionID - The subscription credentials which uniquely identify the Microsoft Azure subscription. The subscription
    32  //     ID forms part of the URI for every service call.
    33  //   - credential - used to authorize requests. Usually a credential from azidentity.
    34  //   - options - pass nil to accept the default values.
    35  func NewUsagesClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*UsagesClient, error) {
    36  	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	client := &UsagesClient{
    41  		subscriptionID: subscriptionID,
    42  		internal:       cl,
    43  	}
    44  	return client, nil
    45  }
    46  
    47  // NewListPager - List network usages for a subscription.
    48  //
    49  // Generated from API version 2024-05-01
    50  //   - location - The location where resource usage is queried.
    51  //   - options - UsagesClientListOptions contains the optional parameters for the UsagesClient.NewListPager method.
    52  func (client *UsagesClient) NewListPager(location string, options *UsagesClientListOptions) *runtime.Pager[UsagesClientListResponse] {
    53  	return runtime.NewPager(runtime.PagingHandler[UsagesClientListResponse]{
    54  		More: func(page UsagesClientListResponse) bool {
    55  			return page.NextLink != nil && len(*page.NextLink) > 0
    56  		},
    57  		Fetcher: func(ctx context.Context, page *UsagesClientListResponse) (UsagesClientListResponse, error) {
    58  			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "UsagesClient.NewListPager")
    59  			nextLink := ""
    60  			if page != nil {
    61  				nextLink = *page.NextLink
    62  			}
    63  			resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
    64  				return client.listCreateRequest(ctx, location, options)
    65  			}, nil)
    66  			if err != nil {
    67  				return UsagesClientListResponse{}, err
    68  			}
    69  			return client.listHandleResponse(resp)
    70  		},
    71  		Tracer: client.internal.Tracer(),
    72  	})
    73  }
    74  
    75  // listCreateRequest creates the List request.
    76  func (client *UsagesClient) listCreateRequest(ctx context.Context, location string, options *UsagesClientListOptions) (*policy.Request, error) {
    77  	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Network/locations/{location}/usages"
    78  	if location == "" {
    79  		return nil, errors.New("parameter location cannot be empty")
    80  	}
    81  	urlPath = strings.ReplaceAll(urlPath, "{location}", url.PathEscape(location))
    82  	if client.subscriptionID == "" {
    83  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
    84  	}
    85  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
    86  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	reqQP := req.Raw().URL.Query()
    91  	reqQP.Set("api-version", "2024-05-01")
    92  	req.Raw().URL.RawQuery = reqQP.Encode()
    93  	req.Raw().Header["Accept"] = []string{"application/json"}
    94  	return req, nil
    95  }
    96  
    97  // listHandleResponse handles the List response.
    98  func (client *UsagesClient) listHandleResponse(resp *http.Response) (UsagesClientListResponse, error) {
    99  	result := UsagesClientListResponse{}
   100  	if err := runtime.UnmarshalAsJSON(resp, &result.UsagesListResult); err != nil {
   101  		return UsagesClientListResponse{}, err
   102  	}
   103  	return result, nil
   104  }