Adsk.Platform.HttpClient 0.0.9

dotnet add package Adsk.Platform.HttpClient --version 0.0.9
NuGet\Install-Package Adsk.Platform.HttpClient -Version 0.0.9
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.9" />
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.9
#r "nuget: Adsk.Platform.HttpClient, 0.0.9"
#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.9

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

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 (8)

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

Package Downloads
Adsk.Platform.ACC.AccountAdmin

Package Description

Adsk.Platform.ACC.CostManagement

Package Description

Adsk.Platform.ACC.ModelProperties

Autodesk Platform: Model Properties Service SDK

Adsk.Platform.DataManagement

Autodesk Platform: Data Management Service SDK

Adsk.Platform.Authentication

Autodesk Platform: Authentication Service SDK

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.9 82 5/31/2024
0.0.8 102 5/22/2024
0.0.7 72 5/14/2024
0.0.6 124 5/4/2024
0.0.5 96 5/3/2024
0.0.4 112 4/30/2024
0.0.3 118 4/30/2024