Adsk.Platform.HttpClient 0.0.11

There is a newer version of this package available.
See the version list below for details.
dotnet add package Adsk.Platform.HttpClient --version 0.0.11                
NuGet\Install-Package Adsk.Platform.HttpClient -Version 0.0.11                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Adsk.Platform.HttpClient" Version="0.0.11" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Adsk.Platform.HttpClient --version 0.0.11                
#r "nuget: Adsk.Platform.HttpClient, 0.0.11"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Adsk.Platform.HttpClient as a Cake Addin
#addin nuget:?package=Adsk.Platform.HttpClient&version=0.0.11

// Install Adsk.Platform.HttpClient as a Cake Tool
#tool nuget:?package=Adsk.Platform.HttpClient&version=0.0.11                

Adsk.Platform.HttpClient

Adsk.Platform.HttpClient is a dependency of the APS toolkit libraries, and it's used by default to make HTTP requests to the Autodesk Platform (APS) API endpoints.

Adsk.Platform.HttpClient is a implementation of System.Net.Http.HttpClient.

Compare to the standard System.Net.Http.HttpClient, this implementation has the following features:

Installation

dotnet add package Adsk.Platform.DataManagement

This package is a dependency of the other packagesAdsk.Platform.*.

Usage

In the following example, we create a new instance of DataManagementClient is based on the HttpClient implementation.

using Autodesk.DataManagement;

public async Task<Hubs> GetHub()
{

    async Task<string> getAccessToken()
    {
        //return access token with your logic
    }

    var DMclient = new DataManagementClient(getAccessToken);

    var hubs = await DMclient.DataMgtApi.Project.V1.Hubs.GetAsync();

    return hubs;
}

Concurrency rate limit

The concurrency rate limit can be enabled with the HttpClient constructor. The rate limit is defined by the maximum number of concurrent requests and the time window. This limit is per endpoint.

public async Task GetUsersConcurrently()
{

    //Limit to 10 requests per second
    var requestTimeWindow = TimeSpan.FromMilliseconds(1000);
    var httpClient = Autodesk.Common.HttpClientLibrary.HttpClient.Create((10, requestTimeWindow));

    //Just run all tasks in parallel. The HttpClient will handle the concurrency rate limit
    var tasks = new List<Task>();
    for (int i = 0; i < 20; i++)
    {
        var resp = httpClient.GetAsync("https://randomuser.me/api/");
        tasks.Add(resp);

    }

    await Task.WhenAll(tasks);

    }

By default, the concurrency rate limit is disabled.

Note: The concurrency rate limit is per endpoint, not global.

Error handling

The error handler throw an HttpRequestException exception if:

The exception message contains the response status code and the endpoint response content as a string.

using Autodesk.DataManagement;

public async Task<Hubs> GetHub()
{

    async Task<string> getAccessToken()
    {
        //return access token with your logic
    }

    var DMclient = new DataManagementClient(getAccessToken);

    try {

        var hubs = await DMclient.DataMgtApi.Project.V1.Hubs.GetAsync();
        return hubs;

    } catch (HttpRequestException ex) {

        Console.WriteLine(ex.StatusCode);
        Console.WriteLine(ex.Message); //Response content returned by the API endpoint

    }
}

Retry logic

The client uses the default retry logic provided with Kiota. The code below shows which status codes are considered as 'retry-able'.

private static bool ShouldRetry(HttpStatusCode? statusCode)
{
    return statusCode switch
    {
        HttpStatusCode.ServiceUnavailable => true,
        HttpStatusCode.GatewayTimeout => true,
        (HttpStatusCode)429 => true,
        _ => false
    };
}

Url Redirection

The client uses the default redirection logic provided with Kiota. The code below shows which status codes are considered as redirection.

private static bool IsRedirect(HttpStatusCode statusCode)
{
    return statusCode switch
    {
        HttpStatusCode.MovedPermanently => true,
        HttpStatusCode.Found => true,
        HttpStatusCode.SeeOther => true,
        HttpStatusCode.TemporaryRedirect => true,
        (HttpStatusCode)308 => true,
        _ => false
    };
}

Bring your own HttpClient

This default httpClient can be replaced by your own implementation.

For example, the Polly library could be used to implement a retry policy.

using System;
using System.Net.Http;
using Microsoft.Extensions.Http;
using Microsoft.Extensions.Http.Resilience;
using Polly;
using Autodesk.DataManagement;

public async Task<Hubs> GetHub()
{

    async Task<string> getAccessToken()
    {
        //return access token with your logic
    }

    // Create a custom HttpClient
    var customHttpClient = CreateHttpClient();

    //Pass it to the DataManagementClient
    var DMclient = new DataManagementClient(getAccessToken, customHttpClient);

    var hubs = await DMclient.DataMgtApi.Project.V1.Hubs.GetAsync();

    return hubs;
}

// Create a custom HttpClient with a Polly retry policy
private HttpClient CreateHttpClient() {

    var retryPipeline = new ResiliencePipelineBuilder<HttpResponseMessage>()
        .AddRetry(new HttpRetryStrategyOptions
        {
            BackoffType = DelayBackoffType.Exponential,
            MaxRetryAttempts = 3
        })
        .Build();
    
    var socketHandler = new SocketsHttpHandler
    {
        PooledConnectionLifetime = TimeSpan.FromMinutes(15)
    };

    var resilienceHandler = new ResilienceHandler(retryPipeline)
    {
        InnerHandler = socketHandler,
    };
    
    return new HttpClient(resilienceHandler);
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (9)

Showing the top 5 NuGet packages that depend on Adsk.Platform.HttpClient:

Package Downloads
Adsk.Platform.ACC.AccountAdmin

Autodesk Platform: ACC Account service SDK and tools

Adsk.Platform.DataManagement

Autodesk Platform: Data Management Service SDK and tools

Adsk.Platform.ACC.CostManagement

Autodesk Platform: ACC Cost Service SDK and tools

Adsk.Platform.ACC.ModelProperties

Autodesk Platform: ACC Model Properties Service SDK and tools

Adsk.Platform.Authentication

Autodesk Platform: Authentication Service SDK and tools

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.3 150 10/17/2024
0.1.2 138 10/16/2024
0.1.1 138 10/16/2024
0.1.0 142 10/16/2024
0.0.16 138 10/14/2024
0.0.15 127 10/14/2024
0.0.14 143 10/14/2024
0.0.13 212 9/18/2024
0.0.12 202 7/30/2024
0.0.11 151 7/16/2024
0.0.10 150 7/16/2024
0.0.9 148 5/31/2024
0.0.8 161 5/22/2024
0.0.7 121 5/14/2024
0.0.6 175 5/4/2024
0.0.5 143 5/3/2024
0.0.4 166 4/30/2024
0.0.3 174 4/30/2024