1
0

Add your own HTTP client

This commit is contained in:
2026-03-13 19:03:08 +01:00
parent ec959f1500
commit 762e3df704
6 changed files with 66 additions and 24 deletions

View File

@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- Added optional parameter to inject your own `HttpClient`
### Changed
- Migrated main repository from Gitlab to Gitea

View File

@@ -40,6 +40,6 @@ namespace AMWD.Net.Api.LinkMobility
/// <summary>
/// Gets or sets the proxy information.
/// </summary>
public virtual IWebProxy Proxy { get; set; }
public virtual IWebProxy? Proxy { get; set; }
}
}

View File

@@ -56,6 +56,7 @@ namespace AMWD.Net.Api.LinkMobility
return PostAsync<SendMessageResponse, SendBinaryMessageRequest>("/smsmessaging/binary", request, cancellationToken: cancellationToken);
}
// https://en.wikipedia.org/wiki/MSISDN
private static bool IsValidMSISDN(string msisdn)
{
if (string.IsNullOrWhiteSpace(msisdn))

View File

@@ -26,8 +26,9 @@ namespace AMWD.Net.Api.LinkMobility
/// <param name="username">The username 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>
public LinkMobilityClient(string username, string password, ClientOptions? clientOptions = null)
: this(new BasicAuthentication(username, password), clientOptions)
/// <param name="httpClient">Optional <see cref="HttpClient"/> instance if you want a custom <see cref="HttpMessageHandler"/> implemented.</param>
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>
/// <param name="token">The bearer token used for authentication.</param>
/// <param name="clientOptions">Optional configuration settings for the client.</param>
public LinkMobilityClient(string token, ClientOptions? clientOptions = null)
: this(new AccessTokenAuthentication(token), clientOptions)
/// <param name="httpClient">Optional <see cref="HttpClient"/> instance if you want a custom <see cref="HttpMessageHandler"/> implemented.</param>
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>
/// <param name="authentication">The authentication mechanism used to authorize requests.</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)
throw new ArgumentNullException(nameof(authentication));
@@ -55,7 +58,9 @@ namespace AMWD.Net.Api.LinkMobility
_clientOptions = clientOptions ?? new ClientOptions();
ValidateClientOptions();
_httpClient = CreateHttpClient();
_httpClient = httpClient ?? CreateHttpClient();
ConfigureHttpClient(_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('/');
var client = new HttpClient(handler, disposeHandler: true)
{
BaseAddress = new Uri($"{baseUrl}/"),
Timeout = _clientOptions.Timeout
};
httpClient.BaseAddress = new Uri($"{baseUrl}/");
httpClient.Timeout = _clientOptions.Timeout;
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(nameof(LinkMobilityClient), version));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (_clientOptions.DefaultHeaders.Count > 0)
{
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)
@@ -146,8 +156,16 @@ namespace AMWD.Net.Api.LinkMobility
string requestUrl = BuildRequestUrl(requestPath, queryParams);
var httpContent = ConvertRequest(request);
var response = await _httpClient.PostAsync(requestUrl, httpContent, cancellationToken).ConfigureAwait(false);
return await GetResponse<TResponse>(response, cancellationToken).ConfigureAwait(false);
var httpRequest = new HttpRequestMessage
{
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)

View File

@@ -1,20 +1,38 @@
namespace AMWD.Net.Api.LinkMobility
{
/// <summary>
/// A generic response of the LinkMobility API.
/// </summary>
public class LinkMobilityResponse
{
/// <summary>
/// Contains the message id defined in the request.
/// </summary>
[JsonProperty("clientMessageId")]
public string ClientMessageId { get; set; }
public string? ClientMessageId { get; set; }
/// <summary>
/// The actual number of generated SMS.
/// </summary>
[JsonProperty("smsCount")]
public int SmsCount { get; set; }
public int? SmsCount { get; set; }
/// <summary>
/// Status code
/// </summary>
[JsonProperty("statusCode")]
public StatusCodes StatusCode { get; set; }
public StatusCodes? StatusCode { get; set; }
/// <summary>
/// Description of the response status code.
/// </summary>
[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")]
public string TransferId { get; set; }
public string? TransferId { get; set; }
}
}

View File

@@ -7,6 +7,7 @@
{
/// <summary>
/// Retrieves the query parameters.
/// </summary>
IReadOnlyDictionary<string, string> GetQueryParameters();
}
}