-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from easykeys/feature/fedex/api
Feature/fedex/api
- Loading branch information
Showing
173 changed files
with
3,379 additions
and
756 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
mode: Mainline | ||
next-version: 3.11.0 | ||
next-version: 4.0.0 | ||
branches: | ||
feature: | ||
tag: preview | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 10 additions & 14 deletions
24
src/EasyKeys.Shipping.Abstractions/EasyKeys.Shipping.Abstractions.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net7.0</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Label="Nuget Package Settings"> | ||
<Description>DotNetCore Abstractions of Shipment Library.</Description> | ||
<PackageTags>DotNetCore, Shipment Abstractions</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Humanizer" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net8.0</TargetFrameworks> | ||
</PropertyGroup> | ||
<PropertyGroup Label="Nuget Package Settings"> | ||
<Description>DotNetCore Abstractions of Shipment Library.</Description> | ||
<PackageTags>DotNetCore, Shipment Abstractions</PackageTags> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Humanizer" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/EasyKeys.Shipping.FedEx.Abstractions/Client/V1/Auth/IFedexAuthClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace EasyKeys.Shipping.FedEx.Abstractions.Api.V1.Auth; | ||
|
||
/// <summary> | ||
/// Implmentation of the FedEx Rates and Transit Times client. Can be used to get rates and transit times. | ||
/// <see href="https://developer.fedex.com/api/en-us/catalog/authorization/v1/docs.html"/>. | ||
/// </summary> | ||
public interface IFedExAuthClient | ||
{ | ||
/// <summary> | ||
/// Gets the authentication token asynchronously. | ||
/// </summary> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
/// <returns>The authentication token.</returns> | ||
Task<string> GetTokenAsync(CancellationToken cancellationToken = default); | ||
} |
75 changes: 75 additions & 0 deletions
75
src/EasyKeys.Shipping.FedEx.Abstractions/Client/V1/Auth/Impl/FedExAuthClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System.Collections.Concurrent; | ||
using System.Net.Http.Json; | ||
|
||
using EasyKeys.Shipping.FedEx.Abstractions.Api.V1.Auth.Models; | ||
using EasyKeys.Shipping.FedEx.Abstractions.Options; | ||
|
||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace EasyKeys.Shipping.FedEx.Abstractions.Api.V1.Auth.Impl; | ||
|
||
public class FedExAuthClient : IFedExAuthClient | ||
{ | ||
private readonly HttpClient _httpClient; | ||
private readonly FedExApiOptions _options; | ||
private readonly ILogger<FedExAuthClient> _logger; | ||
private readonly ConcurrentDictionary<string, string> _token = new(); | ||
private readonly ConcurrentDictionary<string, DateTimeOffset> _expirationClock = new(); | ||
|
||
public FedExAuthClient( | ||
IHttpClientFactory clientFactory, | ||
IOptionsMonitor<FedExApiOptions> optionsMonitor, | ||
ILogger<FedExAuthClient> logger) | ||
{ | ||
_options = optionsMonitor.CurrentValue; | ||
_httpClient = clientFactory.CreateClient(nameof(IFedExAuthClient)); | ||
_logger = logger; | ||
} | ||
|
||
public async Task<string> GetTokenAsync(CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
if (DateTimeOffset.Now < _expirationClock.GetValueOrDefault(nameof(_expirationClock))) | ||
{ | ||
_logger.LogDebug("Token is returned"); | ||
_token.TryGetValue(nameof(TokenResponse), out var existingToken); | ||
ArgumentNullException.ThrowIfNull(existingToken, nameof(TokenResponse)); | ||
return existingToken; | ||
} | ||
|
||
_logger.LogDebug("Token being requested."); | ||
|
||
// Send a POST request to FedEx's token endpoint | ||
var response = await _httpClient.PostAsync( | ||
$"{_options.Url}oauth/token", | ||
new FormUrlEncodedContent(new Dictionary<string, string> | ||
{ | ||
{ "client_id", _options.ClientId }, | ||
{ "client_secret", _options.ClientSecret }, | ||
{ "grant_type", "client_credentials" } | ||
}), | ||
cancellationToken); | ||
|
||
response.EnsureSuccessStatusCode(); | ||
|
||
var tokenObj = await response.Content.ReadFromJsonAsync<TokenResponse>(cancellationToken); | ||
|
||
ArgumentNullException.ThrowIfNull(tokenObj, nameof(TokenResponse)); | ||
|
||
_logger.LogInformation("Token being updated"); | ||
|
||
_token.AddOrUpdate(nameof(TokenResponse), tokenObj.Token, (x, y) => tokenObj.Token); | ||
|
||
_expirationClock.AddOrUpdate(nameof(_expirationClock), (x) => DateTimeOffset.Now.AddSeconds(tokenObj.ExpiresIn - 5), (x, y) => y.AddSeconds(tokenObj.ExpiresIn - 5)); | ||
|
||
return tokenObj.Token; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex.Message); | ||
return string.Empty; | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/EasyKeys.Shipping.FedEx.Abstractions/Client/V1/Auth/Models/TokenResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace EasyKeys.Shipping.FedEx.Abstractions.Api.V1.Auth.Models; | ||
|
||
public record TokenResponse | ||
{ | ||
[JsonPropertyName("token_type")] | ||
public string TokenType { get; init; } = string.Empty; | ||
|
||
[JsonPropertyName("scope")] | ||
public string Scope { get; init; } = string.Empty; | ||
|
||
[JsonPropertyName("access_token")] | ||
public string AccessToken { get; init; } = string.Empty; | ||
|
||
[JsonPropertyName("expires_in")] | ||
public long ExpiresIn { get; init; } | ||
|
||
public string Token => $"{TokenType} {AccessToken}"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 22 additions & 29 deletions
51
src/EasyKeys.Shipping.FedEx.Abstractions/EasyKeys.Shipping.FedEx.Abstractions.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net7.0</TargetFrameworks> | ||
<NoWarn>$(NoWarn);CS0108</NoWarn> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Label="Nuget Package Settings"> | ||
<Description>DotNetCore Implementation of FedEx Web Services 2020 of WCF.</Description> | ||
<PackageTags>DotNetCore, FedEx 2020 WCF</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Ardalis.SmartEnum" /> | ||
<PackageReference Include="System.ServiceModel.Http" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Bet.Extensions.Options" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference | ||
Include="..\EasyKeys.Shipping.Abstractions\EasyKeys.Shipping.Abstractions.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<WCFMetadata Include="Connected Services" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net8.0</TargetFrameworks> | ||
<NoWarn>$(NoWarn);CS0108</NoWarn> | ||
</PropertyGroup> | ||
<PropertyGroup Label="Nuget Package Settings"> | ||
<Description>DotNetCore Implementation of FedEx Authorization Api and Web Services 2020 of WCF.</Description> | ||
<PackageTags>DotNetCore, FedEx 2020 WCF, FedEx v1 API, FedEx Authorization Api</PackageTags> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Ardalis.SmartEnum" /> | ||
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" /> | ||
<PackageReference Include="System.ServiceModel.Http" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Bet.Extensions.Options" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\EasyKeys.Shipping.Abstractions\EasyKeys.Shipping.Abstractions.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<WCFMetadata Include="Connected Services" /> | ||
</ItemGroup> | ||
</Project> |
Oops, something went wrong.