Adsk.Platform.HttpClient
0.1.3
dotnet add package Adsk.Platform.HttpClient --version 0.1.3
NuGet\Install-Package Adsk.Platform.HttpClient -Version 0.1.3
<PackageReference Include="Adsk.Platform.HttpClient" Version="0.1.3" />
paket add Adsk.Platform.HttpClient --version 0.1.3
#r "nuget: Adsk.Platform.HttpClient, 0.1.3"
// Install Adsk.Platform.HttpClient as a Cake Addin #addin nuget:?package=Adsk.Platform.HttpClient&version=0.1.3 // Install Adsk.Platform.HttpClient as a Cake Tool #tool nuget:?package=Adsk.Platform.HttpClient&version=0.1.3
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:
- Retry logic
- Response decompression
- Error handling
- Url Redirection
- Concurrency rate limit
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:
- Status code is not in the range 200-299
- Status code is not handled by the retry logic or the redirection logic
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 | Versions 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Http (>= 8.0.1)
- Microsoft.Kiota.Http.HttpClientLibrary (>= 1.4.3)
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.Authentication
Autodesk Platform: Authentication Service SDK and tools |
|
Adsk.Platform.ACC.ModelProperties
Autodesk Platform: ACC Model Properties Service SDK and tools |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.1.3 | 165 | 10/17/2024 |
0.1.2 | 143 | 10/16/2024 |
0.1.1 | 147 | 10/16/2024 |
0.1.0 | 149 | 10/16/2024 |
0.0.16 | 143 | 10/14/2024 |
0.0.15 | 134 | 10/14/2024 |
0.0.14 | 148 | 10/14/2024 |
0.0.13 | 223 | 9/18/2024 |
0.0.12 | 205 | 7/30/2024 |
0.0.11 | 156 | 7/16/2024 |
0.0.10 | 157 | 7/16/2024 |
0.0.9 | 153 | 5/31/2024 |
0.0.8 | 166 | 5/22/2024 |
0.0.7 | 128 | 5/14/2024 |
0.0.6 | 185 | 5/4/2024 |
0.0.5 | 153 | 5/3/2024 |
0.0.4 | 169 | 4/30/2024 |
0.0.3 | 179 | 4/30/2024 |