// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// Code generated by Microsoft (R) Go Code Generator. DO NOT EDIT.

package armdevopsinfrastructure

import (
	"context"
	"errors"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
	"net/http"
	"net/url"
	"strings"
)

// SubscriptionUsagesClient contains the methods for the SubscriptionUsages group.
// Don't use this type directly, use NewSubscriptionUsagesClient() instead.
type SubscriptionUsagesClient struct {
	internal       *arm.Client
	subscriptionID string
}

// NewSubscriptionUsagesClient creates a new instance of SubscriptionUsagesClient with the specified values.
//   - subscriptionID - The ID of the target subscription. The value must be an UUID.
//   - credential - used to authorize requests. Usually a credential from azidentity.
//   - options - pass nil to accept the default values.
func NewSubscriptionUsagesClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*SubscriptionUsagesClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &SubscriptionUsagesClient{
		subscriptionID: subscriptionID,
		internal:       cl,
	}
	return client, nil
}

// NewUsagesPager - List Quota resources by subscription ID
//
// Generated from API version 2024-10-19
//   - location - The name of the Azure region.
//   - options - SubscriptionUsagesClientUsagesOptions contains the optional parameters for the SubscriptionUsagesClient.NewUsagesPager
//     method.
func (client *SubscriptionUsagesClient) NewUsagesPager(location string, options *SubscriptionUsagesClientUsagesOptions) *runtime.Pager[SubscriptionUsagesClientUsagesResponse] {
	return runtime.NewPager(runtime.PagingHandler[SubscriptionUsagesClientUsagesResponse]{
		More: func(page SubscriptionUsagesClientUsagesResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *SubscriptionUsagesClientUsagesResponse) (SubscriptionUsagesClientUsagesResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "SubscriptionUsagesClient.NewUsagesPager")
			nextLink := ""
			if page != nil {
				nextLink = *page.NextLink
			}
			resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
				return client.usagesCreateRequest(ctx, location, options)
			}, nil)
			if err != nil {
				return SubscriptionUsagesClientUsagesResponse{}, err
			}
			return client.usagesHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// usagesCreateRequest creates the Usages request.
func (client *SubscriptionUsagesClient) usagesCreateRequest(ctx context.Context, location string, _ *SubscriptionUsagesClientUsagesOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.DevOpsInfrastructure/locations/{location}/usages"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if location == "" {
		return nil, errors.New("parameter location cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{location}", url.PathEscape(location))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2024-10-19")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// usagesHandleResponse handles the Usages response.
func (client *SubscriptionUsagesClient) usagesHandleResponse(resp *http.Response) (SubscriptionUsagesClientUsagesResponse, error) {
	result := SubscriptionUsagesClientUsagesResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.PagedQuota); err != nil {
		return SubscriptionUsagesClientUsagesResponse{}, err
	}
	return result, nil
}
