-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8fa0421
commit 878949e
Showing
4 changed files
with
109 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace ShippingRates.Models.FedEx | ||
{ | ||
class FedExErrorResponse | ||
{ | ||
[JsonPropertyName("errors")] | ||
public FedExErrorItem[] Errors { get; set; } | ||
} | ||
|
||
class FedExErrorItem | ||
{ | ||
[JsonPropertyName("code")] | ||
public string Code { get; set; } | ||
[JsonPropertyName("message")] | ||
public string Message { get; set; } | ||
} | ||
} |
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,77 @@ | ||
using ShippingRates.ShippingProviders; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
|
||
namespace ShippingRates.Services.FedEx | ||
{ | ||
internal class FedExOAuthService | ||
{ | ||
static string GetOAuthRequestUri(bool isProduction) | ||
=> $"https://{(isProduction ? "apis" : "apis-sandbox")}.fedex.com/oauth/token"; | ||
|
||
public static async Task<string> GetTokenAsync(FedExProviderConfiguration configuration, HttpClient httpClient, Action<Error> reportError) | ||
{ | ||
var token = TokenCacheService.GetToken(configuration.ClientId); | ||
if (!string.IsNullOrEmpty(token)) | ||
return token; | ||
|
||
var requestMessage = new HttpRequestMessage(HttpMethod.Post, GetOAuthRequestUri(configuration.UseProduction)); | ||
var postData = new List<KeyValuePair<string, string>> | ||
{ | ||
new KeyValuePair<string, string>("grant_type", "client_credentials"), | ||
new KeyValuePair<string, string>("client_id", configuration.ClientId), | ||
new KeyValuePair<string, string>("client_secret", configuration.ClientSecret) | ||
}; | ||
requestMessage.Content = new FormUrlEncodedContent(postData); | ||
|
||
var responseMessage = await httpClient.SendAsync(requestMessage); | ||
var response = await responseMessage.Content.ReadAsStringAsync(); | ||
|
||
if (responseMessage.IsSuccessStatusCode) | ||
{ | ||
var result = JsonSerializer.Deserialize<FedExOAuthResponse>(response); | ||
|
||
TokenCacheService.AddToken(configuration.ClientId, result.AccessToken, result.ExpiresIn); | ||
|
||
return result.AccessToken; | ||
} | ||
else | ||
{ | ||
var errorResponse = JsonSerializer.Deserialize<Models.FedEx.FedExErrorResponse>(response); | ||
if ((errorResponse?.Errors?.Length ?? 0) > 0) | ||
{ | ||
foreach (var error in errorResponse.Errors) | ||
{ | ||
reportError(new Error() | ||
{ | ||
Number = error.Code, | ||
Description = error.Message | ||
}); | ||
} | ||
} | ||
else | ||
{ | ||
reportError(new Error() { Description = $"Unknown error while fetching FedEx OAuth token: {responseMessage.StatusCode} {response}" }); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
||
class FedExOAuthResponse | ||
{ | ||
[JsonPropertyName("token_type")] | ||
public string TokenType { get; set; } | ||
[JsonPropertyName("access_token")] | ||
public string AccessToken { get; set; } | ||
[JsonPropertyName("expires_in")] | ||
public int ExpiresIn { get; set; } | ||
[JsonPropertyName("scope")] | ||
public string Scope { get; set; } | ||
} | ||
} | ||
} |
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