Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for end point overrides #41

Merged
merged 1 commit into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}