Add your own HTTP client
This commit is contained in:
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Added optional parameter to inject your own `HttpClient`
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- Migrated main repository from Gitlab to Gitea
|
- Migrated main repository from Gitlab to Gitea
|
||||||
|
|||||||
@@ -40,6 +40,6 @@ namespace AMWD.Net.Api.LinkMobility
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the proxy information.
|
/// Gets or sets the proxy information.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual IWebProxy Proxy { get; set; }
|
public virtual IWebProxy? Proxy { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ namespace AMWD.Net.Api.LinkMobility
|
|||||||
return PostAsync<SendMessageResponse, SendBinaryMessageRequest>("/smsmessaging/binary", request, cancellationToken: cancellationToken);
|
return PostAsync<SendMessageResponse, SendBinaryMessageRequest>("/smsmessaging/binary", request, cancellationToken: cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://en.wikipedia.org/wiki/MSISDN
|
||||||
private static bool IsValidMSISDN(string msisdn)
|
private static bool IsValidMSISDN(string msisdn)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(msisdn))
|
if (string.IsNullOrWhiteSpace(msisdn))
|
||||||
|
|||||||
@@ -26,8 +26,9 @@ namespace AMWD.Net.Api.LinkMobility
|
|||||||
/// <param name="username">The username used for basic authentication.</param>
|
/// <param name="username">The username used for basic authentication.</param>
|
||||||
/// <param name="password">The password used for basic authentication.</param>
|
/// <param name="password">The password used for basic authentication.</param>
|
||||||
/// <param name="clientOptions">Optional configuration settings for the client.</param>
|
/// <param name="clientOptions">Optional configuration settings for the client.</param>
|
||||||
public LinkMobilityClient(string username, string password, ClientOptions? clientOptions = null)
|
/// <param name="httpClient">Optional <see cref="HttpClient"/> instance if you want a custom <see cref="HttpMessageHandler"/> implemented.</param>
|
||||||
: this(new BasicAuthentication(username, password), clientOptions)
|
public LinkMobilityClient(string username, string password, ClientOptions? clientOptions = null, HttpClient? httpClient = null)
|
||||||
|
: this(new BasicAuthentication(username, password), clientOptions, httpClient)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,8 +37,9 @@ namespace AMWD.Net.Api.LinkMobility
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="token">The bearer token used for authentication.</param>
|
/// <param name="token">The bearer token used for authentication.</param>
|
||||||
/// <param name="clientOptions">Optional configuration settings for the client.</param>
|
/// <param name="clientOptions">Optional configuration settings for the client.</param>
|
||||||
public LinkMobilityClient(string token, ClientOptions? clientOptions = null)
|
/// <param name="httpClient">Optional <see cref="HttpClient"/> instance if you want a custom <see cref="HttpMessageHandler"/> implemented.</param>
|
||||||
: this(new AccessTokenAuthentication(token), clientOptions)
|
public LinkMobilityClient(string token, ClientOptions? clientOptions = null, HttpClient? httpClient = null)
|
||||||
|
: this(new AccessTokenAuthentication(token), clientOptions, httpClient)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,7 +49,8 @@ namespace AMWD.Net.Api.LinkMobility
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="authentication">The authentication mechanism used to authorize requests.</param>
|
/// <param name="authentication">The authentication mechanism used to authorize requests.</param>
|
||||||
/// <param name="clientOptions">Optional client configuration settings.</param>
|
/// <param name="clientOptions">Optional client configuration settings.</param>
|
||||||
public LinkMobilityClient(IAuthentication authentication, ClientOptions? clientOptions = null)
|
/// <param name="httpClient">Optional <see cref="HttpClient"/> instance if you want a custom <see cref="HttpMessageHandler"/> implemented.</param>
|
||||||
|
public LinkMobilityClient(IAuthentication authentication, ClientOptions? clientOptions = null, HttpClient? httpClient = null)
|
||||||
{
|
{
|
||||||
if (authentication == null)
|
if (authentication == null)
|
||||||
throw new ArgumentNullException(nameof(authentication));
|
throw new ArgumentNullException(nameof(authentication));
|
||||||
@@ -55,7 +58,9 @@ namespace AMWD.Net.Api.LinkMobility
|
|||||||
_clientOptions = clientOptions ?? new ClientOptions();
|
_clientOptions = clientOptions ?? new ClientOptions();
|
||||||
ValidateClientOptions();
|
ValidateClientOptions();
|
||||||
|
|
||||||
_httpClient = CreateHttpClient();
|
_httpClient = httpClient ?? CreateHttpClient();
|
||||||
|
ConfigureHttpClient(_httpClient);
|
||||||
|
|
||||||
authentication.AddHeader(_httpClient);
|
authentication.AddHeader(_httpClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,24 +123,29 @@ namespace AMWD.Net.Api.LinkMobility
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var httpClient = new HttpClient(handler, disposeHandler: true);
|
||||||
|
|
||||||
|
httpClient.DefaultRequestHeaders.UserAgent.Clear();
|
||||||
|
httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(nameof(LinkMobilityClient), version));
|
||||||
|
|
||||||
|
return httpClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ConfigureHttpClient(HttpClient httpClient)
|
||||||
|
{
|
||||||
string baseUrl = _clientOptions.BaseUrl.Trim().TrimEnd('/');
|
string baseUrl = _clientOptions.BaseUrl.Trim().TrimEnd('/');
|
||||||
|
|
||||||
var client = new HttpClient(handler, disposeHandler: true)
|
httpClient.BaseAddress = new Uri($"{baseUrl}/");
|
||||||
{
|
httpClient.Timeout = _clientOptions.Timeout;
|
||||||
BaseAddress = new Uri($"{baseUrl}/"),
|
|
||||||
Timeout = _clientOptions.Timeout
|
|
||||||
};
|
|
||||||
|
|
||||||
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(nameof(LinkMobilityClient), version));
|
httpClient.DefaultRequestHeaders.Accept.Clear();
|
||||||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||||
|
|
||||||
if (_clientOptions.DefaultHeaders.Count > 0)
|
if (_clientOptions.DefaultHeaders.Count > 0)
|
||||||
{
|
{
|
||||||
foreach (var headerKvp in _clientOptions.DefaultHeaders)
|
foreach (var headerKvp in _clientOptions.DefaultHeaders)
|
||||||
client.DefaultRequestHeaders.Add(headerKvp.Key, headerKvp.Value);
|
httpClient.DefaultRequestHeaders.Add(headerKvp.Key, headerKvp.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return client;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<TResponse> PostAsync<TResponse, TRequest>(string requestPath, TRequest? request, IQueryParameter? queryParams = null, CancellationToken cancellationToken = default)
|
private async Task<TResponse> PostAsync<TResponse, TRequest>(string requestPath, TRequest? request, IQueryParameter? queryParams = null, CancellationToken cancellationToken = default)
|
||||||
@@ -146,8 +156,16 @@ namespace AMWD.Net.Api.LinkMobility
|
|||||||
string requestUrl = BuildRequestUrl(requestPath, queryParams);
|
string requestUrl = BuildRequestUrl(requestPath, queryParams);
|
||||||
var httpContent = ConvertRequest(request);
|
var httpContent = ConvertRequest(request);
|
||||||
|
|
||||||
var response = await _httpClient.PostAsync(requestUrl, httpContent, cancellationToken).ConfigureAwait(false);
|
var httpRequest = new HttpRequestMessage
|
||||||
return await GetResponse<TResponse>(response, cancellationToken).ConfigureAwait(false);
|
{
|
||||||
|
Method = HttpMethod.Post,
|
||||||
|
RequestUri = new Uri(requestUrl, UriKind.Relative),
|
||||||
|
Content = httpContent,
|
||||||
|
};
|
||||||
|
|
||||||
|
var httpResponse = await _httpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
|
||||||
|
var response = await GetResponse<TResponse>(httpResponse, cancellationToken).ConfigureAwait(false);
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string BuildRequestUrl(string requestPath, IQueryParameter? queryParams = null)
|
private string BuildRequestUrl(string requestPath, IQueryParameter? queryParams = null)
|
||||||
|
|||||||
@@ -1,20 +1,38 @@
|
|||||||
namespace AMWD.Net.Api.LinkMobility
|
namespace AMWD.Net.Api.LinkMobility
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A generic response of the LinkMobility API.
|
||||||
|
/// </summary>
|
||||||
public class LinkMobilityResponse
|
public class LinkMobilityResponse
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Contains the message id defined in the request.
|
||||||
|
/// </summary>
|
||||||
[JsonProperty("clientMessageId")]
|
[JsonProperty("clientMessageId")]
|
||||||
public string ClientMessageId { get; set; }
|
public string? ClientMessageId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The actual number of generated SMS.
|
||||||
|
/// </summary>
|
||||||
[JsonProperty("smsCount")]
|
[JsonProperty("smsCount")]
|
||||||
public int SmsCount { get; set; }
|
public int? SmsCount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Status code
|
||||||
|
/// </summary>
|
||||||
[JsonProperty("statusCode")]
|
[JsonProperty("statusCode")]
|
||||||
public StatusCodes StatusCode { get; set; }
|
public StatusCodes? StatusCode { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Description of the response status code.
|
||||||
|
/// </summary>
|
||||||
[JsonProperty("statusMessage")]
|
[JsonProperty("statusMessage")]
|
||||||
public string StatusMessage { get; set; }
|
public string? StatusMessage { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Unique identifier that is set after successful processing of the request.
|
||||||
|
/// </summary>
|
||||||
[JsonProperty("transferId")]
|
[JsonProperty("transferId")]
|
||||||
public string TransferId { get; set; }
|
public string? TransferId { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Retrieves the query parameters.
|
/// Retrieves the query parameters.
|
||||||
|
/// </summary>
|
||||||
IReadOnlyDictionary<string, string> GetQueryParameters();
|
IReadOnlyDictionary<string, string> GetQueryParameters();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user