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

Non-static client factory #99

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions src/Postmark/PostmarkAdminClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public class PostmarkAdminClient : PostmarkDotNet.PostmarkClientBase
/// </summary>
/// <param name="accountToken">The "accountToken" can be found by logging into your Postmark and navigating to https://postmarkapp.com/account/edit - Keep this token secret and safe.</param>
/// <param name="apiBaseUri">Optionally override the base url to the API. For example, you may fallback to HTTP (non-SSL) if your app requires it, though, this is not recommended.</param>
public PostmarkAdminClient(string accountToken, string apiBaseUri = "https://api.postmarkapp.com")
: base(apiBaseUri)
/// <param name="client"><see cref="ISimpleHttpClient"/> to processes HTTP interactions.</param>
public PostmarkAdminClient(string accountToken, string apiBaseUri = "https://api.postmarkapp.com", ISimpleHttpClient client = null)
: base(apiBaseUri, client)
{
_authToken = accountToken;
}
Expand Down Expand Up @@ -112,7 +113,7 @@ public async Task<PostmarkServer> CreateServerAsync(String name, string color =
public async Task<PostmarkServer> EditServerAsync(int serverId, String name = null, string color = null,
bool? rawEmailEnabled = null, bool? smtpApiActivated = null, string inboundHookUrl = null,
string bounceHookUrl = null, string openHookUrl = null, bool? postFirstOpenOnly = null,
bool? trackOpens = null, string inboundDomain = null, int? inboundSpamThreshold = null,
bool? trackOpens = null, string inboundDomain = null, int? inboundSpamThreshold = null,
LinkTrackingOptions? trackLinks = null, string clickHookUrl = null, string deliveryHookUrl = null, bool? enableSmtpApiErrorHooks = null)
{

Expand Down
5 changes: 3 additions & 2 deletions src/Postmark/PostmarkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ protected override string AuthHeaderName
/// </summary>
/// <param name="apiBaseUri">The base uri to use when connecting to Postmark. You should rarely need to modify this, except if you want to disable TLS (not recommended), or you are using a proxy of some sort to connect to the API.</param>
/// <param name="serverToken">Used for requests that require server level privileges. This token can be found on the Credentials tab under your Postmark server.</param>
public PostmarkClient(string serverToken, string apiBaseUri = "https://api.postmarkapp.com")
: base(apiBaseUri)
/// <param name="client"><see cref="ISimpleHttpClient"/> to processes HTTP interactions.</param>
public PostmarkClient(string serverToken, string apiBaseUri = "https://api.postmarkapp.com", ISimpleHttpClient client = null)
: base(apiBaseUri, client)
{
_authToken = serverToken;
}
Expand Down
21 changes: 4 additions & 17 deletions src/Postmark/PostmarkClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@ namespace PostmarkDotNet
/// </summary>
public abstract class PostmarkClientBase
{
private static Lazy<ISimpleHttpClient> _staticClient =
new Lazy<ISimpleHttpClient>(()=>new SimpleHttpClient()) ;

/// <summary>
/// Configure a global connection factory to to process HTTP interactions.
/// </summary>
/// <remarks>
/// In most cases, you should not need to modify this property, but it's useful
/// in cases where you want to use another http client, or to mock the http processing
/// (for tests).
/// </remarks>
public static Func<ISimpleHttpClient> ClientFactory {get;set;} = () => _staticClient.Value;

protected static readonly string DATE_FORMAT = "yyyy-MM-dd";

/// <summary>
Expand All @@ -38,15 +25,17 @@ public abstract class PostmarkClientBase
typeof(PostmarkClient).AssemblyQualifiedName + ")";

private Uri baseUri;
private readonly ISimpleHttpClient client;

/// <summary>
/// Provides a base implementation of core request/response interactions.
/// </summary>
/// <param name="apiBaseUri"></param>
/// <param name="requestTimeoutInSeconds"></param>
public PostmarkClientBase(string apiBaseUri = "https://api.postmarkapp.com")
/// <param name="client"><see cref="ISimpleHttpClient"/> to processes HTTP interactions.</param>
public PostmarkClientBase(string apiBaseUri = "https://api.postmarkapp.com", ISimpleHttpClient client = null)
{
baseUri = new Uri(apiBaseUri);
this.client = client ?? new SimpleHttpClient();
}

protected abstract string AuthHeaderName { get; }
Expand All @@ -68,8 +57,6 @@ protected async Task<TResponse> ProcessRequestAsync<TRequestBody, TResponse>(str
{
TResponse retval = default(TResponse);

var client = ClientFactory();

var request = new HttpRequestMessage(verb, baseUri + apiPath.TrimStart('/'));

//if the message is not a string, or the message is a non-empty string,
Expand Down