Skip to content

Commit

Permalink
Merge pull request #41 from joepb/add-endpointoverride
Browse files Browse the repository at this point in the history
Add support for end point overrides
  • Loading branch information
EricSmekens authored Jan 31, 2022
2 parents ccf63fe + 434cd4a commit e0b5598
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
- Add support for a custom end point URI

## [2.3.0] - 2021-10-27
- Add TextClientFactory

Expand Down
22 changes: 18 additions & 4 deletions CM.Text/TextClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class TextClient : ITextClient
{
private static readonly Lazy<HttpClient> ClientSingletonLazy = new Lazy<HttpClient>();
private readonly Guid _apiKey;
[CanBeNull] private readonly Uri _endPointOverride;
private readonly HttpClient _httpClient;

/// <summary>
Expand All @@ -88,10 +89,23 @@ public TextClient(Guid apiKey) : this(apiKey, null)
/// <param name="apiKey">The API key.</param>
/// <param name="httpClient">An optional HTTP client.</param>
[PublicAPI]
public TextClient(Guid apiKey, [CanBeNull] HttpClient httpClient)
[SuppressMessage("ReSharper", "IntroduceOptionalParameters.Global", Justification = "Binary backwards compatibility")]
public TextClient(Guid apiKey, [CanBeNull] HttpClient httpClient): this(apiKey, httpClient, null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="TextClient" /> class.
/// </summary>
/// <param name="apiKey">The API key.</param>
/// <param name="httpClient">An optional HTTP client.</param>
/// <param name="endPointOverride">(Optional) The end point to use, instead of the default "https://gw.cmtelecom.com/v1.0/message".</param>
[PublicAPI]
public TextClient(Guid apiKey, [CanBeNull] HttpClient httpClient, [CanBeNull] Uri endPointOverride)
{
this._apiKey = apiKey;
this._httpClient = httpClient ?? ClientSingletonLazy.Value;
this._endPointOverride = endPointOverride;
}

/// <inheritdoc />
Expand All @@ -104,9 +118,9 @@ public async Task<TextClientResult> SendMessageAsync(
CancellationToken cancellationToken = default(CancellationToken))
{
using (var request = new HttpRequestMessage(
HttpMethod.Post,
new Uri(BusinessMessagingApi.Constant.BusinessMessagingGatewayJsonEndpoint)
))
HttpMethod.Post,
this._endPointOverride ?? new Uri(BusinessMessagingApi.Constant.BusinessMessagingGatewayJsonEndpoint)
))
{
request.Content = new StringContent(
BusinessMessagingApi.GetHttpPostBody(this._apiKey, messageText, from, to, reference),
Expand Down
7 changes: 5 additions & 2 deletions CM.Text/TextClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,23 @@ public interface ITextClientFactory
public class TextClientFactory : ITextClientFactory
{
private readonly HttpClient _httpClient;
[CanBeNull] private readonly Uri _endPointOverride;

/// <summary>
/// Initializes a new instance of the <see cref="TextClientFactory"/> class.
/// </summary>
/// <param name="httpClient">The HTTP client.</param>
public TextClientFactory(HttpClient httpClient)
/// <param name="endPointOverride">(Optional) The end point to use, instead of the default "https://gw.cmtelecom.com/v1.0/message".</param>
public TextClientFactory(HttpClient httpClient, Uri endPointOverride = null)
{
this._httpClient = httpClient;
this._endPointOverride = endPointOverride;
}

/// <inheritdoc />
public ITextClient GetClient(Guid productToken)
{
return new TextClient(productToken, this._httpClient);
return new TextClient(productToken, this._httpClient, this._endPointOverride);
}
}
}

0 comments on commit e0b5598

Please sign in to comment.