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

SDK updates #14

Merged
merged 1 commit into from
Jan 12, 2024
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
53 changes: 14 additions & 39 deletions AdvancedBilling.Standard/AdvancedBillingClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public sealed class AdvancedBillingClient : IConfiguration

private readonly GlobalConfiguration globalConfiguration;
private const string userAgent = "AB SDK DotNet:0.0.4 on OS {os-info}";
private readonly BasicAuthManager basicAuthManager;
private readonly Lazy<APIExportsController> aPIExports;
private readonly Lazy<AdvanceInvoiceController> advanceInvoice;
private readonly Lazy<BillingPortalController> billingPortal;
Expand Down Expand Up @@ -76,17 +77,18 @@ private AdvancedBillingClient(
Environment environment,
string subdomain,
string domain,
BasicAuthManager basicAuthManager,
string basicAuthUserName,
string basicAuthPassword,
IHttpClientConfiguration httpClientConfiguration)
{
this.Environment = environment;
this.Subdomain = subdomain;
this.Domain = domain;
this.HttpClientConfiguration = httpClientConfiguration;

basicAuthManager = new BasicAuthManager(basicAuthUserName, basicAuthPassword);
globalConfiguration = new GlobalConfiguration.Builder()
.AuthManagers(new Dictionary<string, AuthManager> {
{"BasicAuth", basicAuthManager},
{"global", basicAuthManager}
})
.HttpConfiguration(httpClientConfiguration)
.ServerUrls(EnvironmentsMap[environment], Server.Default)
Expand All @@ -96,7 +98,6 @@ private AdvancedBillingClient(
.UserAgent(userAgent)
.Build();

BasicAuthCredentials = basicAuthManager;

this.aPIExports = new Lazy<APIExportsController>(
() => new APIExportsController(globalConfiguration));
Expand Down Expand Up @@ -344,7 +345,7 @@ private AdvancedBillingClient(
/// <summary>
/// Gets the credentials to use with BasicAuth.
/// </summary>
public IBasicAuthCredentials BasicAuthCredentials { get; private set; }
public IBasicAuthCredentials BasicAuthCredentials => this.basicAuthManager;

/// <summary>
/// Gets the URL for a particular alias in the current environment and appends
Expand All @@ -367,16 +368,9 @@ public Builder ToBuilder()
.Environment(this.Environment)
.Subdomain(this.Subdomain)
.Domain(this.Domain)
.BasicAuthCredentials(basicAuthManager.BasicAuthUserName, basicAuthManager.BasicAuthPassword)
.HttpClientConfig(config => config.Build());

if (BasicAuthCredentials.BasicAuthUserName != null && BasicAuthCredentials.BasicAuthPassword != null)
{
builder.BasicAuthCredentials(auth => auth
.Username(BasicAuthCredentials.BasicAuthUserName)
.Password(BasicAuthCredentials.BasicAuthPassword)
);
}

return builder;
}

Expand Down Expand Up @@ -421,10 +415,7 @@ internal static AdvancedBillingClient CreateFromEnvironment()

if (basicAuthUserName != null && basicAuthPassword != null)
{
builder.BasicAuthCredentials(auth => auth
.Username(basicAuthUserName)
.Password(basicAuthPassword)
);
builder.BasicAuthCredentials(basicAuthUserName, basicAuthPassword);
}

return builder.Build();
Expand All @@ -438,7 +429,8 @@ public class Builder
private Environment environment = AdvancedBilling.Standard.Environment.Production;
private string subdomain = "subdomain";
private string domain = "chargify.com";
private readonly BasicAuthModel.Builder basicAuth = new BasicAuthModel.Builder();
private string basicAuthUserName = "";
private string basicAuthPassword = "";
private HttpClientConfiguration.Builder httpClientConfig = new HttpClientConfiguration.Builder();

/// <summary>
Expand All @@ -447,28 +439,10 @@ public class Builder
/// <param name="basicAuthUserName">BasicAuthUserName.</param>
/// <param name="basicAuthPassword">BasicAuthPassword.</param>
/// <returns>Builder.</returns>
[Obsolete("This method is deprecated. Use BasicAuthCredentials(action) instead.")]
public Builder BasicAuthCredentials(string basicAuthUserName, string basicAuthPassword)
{
basicAuth
.Username(basicAuthUserName)
.Password(basicAuthPassword);
return this;
}

/// <summary>
/// Sets credentials for BasicAuth.
/// </summary>
/// <param name="action">Action.</param>
/// <returns>Builder.</returns>
public Builder BasicAuthCredentials(Action<BasicAuthModel.Builder> action)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}

action(this.basicAuth);
this.basicAuthUserName = basicAuthUserName ?? throw new ArgumentNullException(nameof(basicAuthUserName));
this.basicAuthPassword = basicAuthPassword ?? throw new ArgumentNullException(nameof(basicAuthPassword));
return this;
}

Expand Down Expand Up @@ -534,7 +508,8 @@ public AdvancedBillingClient Build()
environment,
subdomain,
domain,
new BasicAuthManager(basicAuth.Build()),
basicAuthUserName,
basicAuthPassword,
httpClientConfig.Build());
}
}
Expand Down
74 changes: 7 additions & 67 deletions AdvancedBilling.Standard/Authentication/BasicAuthManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ namespace AdvancedBilling.Standard.Authentication
internal class BasicAuthManager : AuthManager, IBasicAuthCredentials
{
/// <summary>
/// Initializes a new instance of the <see cref="BasicAuthManager"/> class.
/// Initializes a new instance of the <see cref="IrisBasicAuthManager"/> class.
/// </summary>
/// <param name="basicAuth"> BasicAuthModel.</param>
internal BasicAuthManager(BasicAuthModel basicAuth)
/// <param name="username"> Username.</param>
/// <param name="password"> Password.</param>
public BasicAuthManager(string username, string password)
{
BasicAuthUserName = basicAuth.Username;
BasicAuthPassword = basicAuth.Password;
this.BasicAuthUserName = username;
this.BasicAuthPassword = password;
Parameters(paramBuilder => paramBuilder
.Header(header => header.Setup("Authorization", GetBasicAuthHeader()).Required()));
.Header(header => header.Setup("Authorization", GetBasicAuthHeader())));
}

/// <summary>
Expand All @@ -51,70 +52,9 @@ public bool Equals(string basicAuthUserName, string basicAuthPassword)

private string GetBasicAuthHeader()
{
if (this.BasicAuthUserName == null || this.BasicAuthPassword == null)
return null;

string authCredentials = this.BasicAuthUserName + ":" + this.BasicAuthPassword;
byte[] data = Encoding.ASCII.GetBytes(authCredentials);
return "Basic " + Convert.ToBase64String(data);
}
}

public sealed class BasicAuthModel
{
internal BasicAuthModel(string username, string password)
{
this.Username = username;
this.Password = password;
}

internal string Username { get; set; }

internal string Password { get; set; }

/// <summary>
/// Builder class for BasicAuthModel.
/// </summary>
public class Builder
{
private string username;
private string password;

/// <summary>
/// Sets Username.
/// </summary>
/// <param name="username">Username.</param>
/// <returns>Builder.</returns>
public Builder Username(string username)
{
this.username = username ?? throw new ArgumentNullException(nameof(username));
return this;
}


/// <summary>
/// Sets Password.
/// </summary>
/// <param name="password">Password.</param>
/// <returns>Builder.</returns>
public Builder Password(string password)
{
this.password = password ?? throw new ArgumentNullException(nameof(password));
return this;
}


/// <summary>
/// Creates an object of the BasicAuthModel using the values provided for the builder.
/// </summary>
/// <returns>BasicAuthModel.</returns>
public BasicAuthModel Build()
{
return new BasicAuthModel(
username,
password
);
}
}
}
}
41 changes: 21 additions & 20 deletions AdvancedBilling.Standard/Controllers/APIExportsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace AdvancedBilling.Standard.Controllers
using System.Threading;
using System.Threading.Tasks;
using AdvancedBilling.Standard;
using AdvancedBilling.Standard.Authentication;
using AdvancedBilling.Standard.Exceptions;
using AdvancedBilling.Standard.Http.Client;
using AdvancedBilling.Standard.Utilities;
Expand Down Expand Up @@ -56,13 +57,13 @@ internal APIExportsController(GlobalConfiguration globalConfiguration) : base(gl
=> await CreateApiCall<List<Models.ProformaInvoice>>()
.RequestBuilder(_requestBuilder => _requestBuilder
.Setup(HttpMethod.Get, "/api_exports/proforma_invoices/{batch_id}/rows.json")
.WithAuth("BasicAuth")
.WithAuth("global")
.Parameters(_parameters => _parameters
.Template(_template => _template.Setup("batch_id", input.BatchId).Required())
.Query(_query => _query.Setup("per_page", input.PerPage))
.Query(_query => _query.Setup("page", input.Page))))
.ResponseHandler(_responseHandler => _responseHandler
.ErrorCase("404", CreateErrorCase("Not Found", (_reason, _context) => new ApiException(_reason, _context))))
.ErrorCase("404", CreateErrorCase("Not Found:'{$response.body}'", (_reason, _context) => new ApiException(_reason, _context), true)))
.ExecuteAsync(cancellationToken).ConfigureAwait(false);

/// <summary>
Expand All @@ -88,13 +89,13 @@ internal APIExportsController(GlobalConfiguration globalConfiguration) : base(gl
=> await CreateApiCall<List<Models.Invoice>>()
.RequestBuilder(_requestBuilder => _requestBuilder
.Setup(HttpMethod.Get, "/api_exports/invoices/{batch_id}/rows.json")
.WithAuth("BasicAuth")
.WithAuth("global")
.Parameters(_parameters => _parameters
.Template(_template => _template.Setup("batch_id", input.BatchId).Required())
.Query(_query => _query.Setup("per_page", input.PerPage))
.Query(_query => _query.Setup("page", input.Page))))
.ResponseHandler(_responseHandler => _responseHandler
.ErrorCase("404", CreateErrorCase("Not Found", (_reason, _context) => new ApiException(_reason, _context))))
.ErrorCase("404", CreateErrorCase("Not Found:'{$response.body}'", (_reason, _context) => new ApiException(_reason, _context), true)))
.ExecuteAsync(cancellationToken).ConfigureAwait(false);

/// <summary>
Expand All @@ -120,13 +121,13 @@ internal APIExportsController(GlobalConfiguration globalConfiguration) : base(gl
=> await CreateApiCall<List<Models.Subscription>>()
.RequestBuilder(_requestBuilder => _requestBuilder
.Setup(HttpMethod.Get, "/api_exports/subscriptions/{batch_id}/rows.json")
.WithAuth("BasicAuth")
.WithAuth("global")
.Parameters(_parameters => _parameters
.Template(_template => _template.Setup("batch_id", input.BatchId).Required())
.Query(_query => _query.Setup("per_page", input.PerPage))
.Query(_query => _query.Setup("page", input.Page))))
.ResponseHandler(_responseHandler => _responseHandler
.ErrorCase("404", CreateErrorCase("Not Found", (_reason, _context) => new ApiException(_reason, _context))))
.ErrorCase("404", CreateErrorCase("Not Found:'{$response.body}'", (_reason, _context) => new ApiException(_reason, _context), true)))
.ExecuteAsync(cancellationToken).ConfigureAwait(false);

/// <summary>
Expand All @@ -147,10 +148,10 @@ public Models.BatchJobResponse ExportProformaInvoices()
=> await CreateApiCall<Models.BatchJobResponse>()
.RequestBuilder(_requestBuilder => _requestBuilder
.Setup(HttpMethod.Post, "/api_exports/proforma_invoices.json")
.WithAuth("BasicAuth"))
.WithAuth("global"))
.ResponseHandler(_responseHandler => _responseHandler
.ErrorCase("404", CreateErrorCase("Not Found", (_reason, _context) => new ApiException(_reason, _context)))
.ErrorCase("409", CreateErrorCase("Conflict", (_reason, _context) => new SingleErrorResponseException(_reason, _context))))
.ErrorCase("404", CreateErrorCase("Not Found:'{$response.body}'", (_reason, _context) => new ApiException(_reason, _context), true))
.ErrorCase("409", CreateErrorCase("HTTP Response Not OK. Status code: {$statusCode}. Response: '{$response.body}'.", (_reason, _context) => new SingleErrorResponseException(_reason, _context), true)))
.ExecuteAsync(cancellationToken).ConfigureAwait(false);

/// <summary>
Expand All @@ -169,10 +170,10 @@ public Models.BatchJobResponse ExportInvoices()
=> await CreateApiCall<Models.BatchJobResponse>()
.RequestBuilder(_requestBuilder => _requestBuilder
.Setup(HttpMethod.Post, "/api_exports/invoices.json")
.WithAuth("BasicAuth"))
.WithAuth("global"))
.ResponseHandler(_responseHandler => _responseHandler
.ErrorCase("404", CreateErrorCase("Not Found", (_reason, _context) => new ApiException(_reason, _context)))
.ErrorCase("409", CreateErrorCase("Conflict", (_reason, _context) => new SingleErrorResponseException(_reason, _context))))
.ErrorCase("404", CreateErrorCase("Not Found:'{$response.body}'", (_reason, _context) => new ApiException(_reason, _context), true))
.ErrorCase("409", CreateErrorCase("HTTP Response Not OK. Status code: {$statusCode}. Response: '{$response.body}'.", (_reason, _context) => new SingleErrorResponseException(_reason, _context), true)))
.ExecuteAsync(cancellationToken).ConfigureAwait(false);

/// <summary>
Expand All @@ -191,9 +192,9 @@ public Models.BatchJobResponse ExportSubscriptions()
=> await CreateApiCall<Models.BatchJobResponse>()
.RequestBuilder(_requestBuilder => _requestBuilder
.Setup(HttpMethod.Post, "/api_exports/subscriptions.json")
.WithAuth("BasicAuth"))
.WithAuth("global"))
.ResponseHandler(_responseHandler => _responseHandler
.ErrorCase("409", CreateErrorCase("Conflict", (_reason, _context) => new SingleErrorResponseException(_reason, _context))))
.ErrorCase("409", CreateErrorCase("HTTP Response Not OK. Status code: {$statusCode}. Response: '{$response.body}'.", (_reason, _context) => new SingleErrorResponseException(_reason, _context), true)))
.ExecuteAsync(cancellationToken).ConfigureAwait(false);

/// <summary>
Expand All @@ -217,11 +218,11 @@ public Models.BatchJobResponse ReadProformaInvoicesExport(
=> await CreateApiCall<Models.BatchJobResponse>()
.RequestBuilder(_requestBuilder => _requestBuilder
.Setup(HttpMethod.Get, "/api_exports/proforma_invoices/{batch_id}.json")
.WithAuth("BasicAuth")
.WithAuth("global")
.Parameters(_parameters => _parameters
.Template(_template => _template.Setup("batch_id", batchId).Required())))
.ResponseHandler(_responseHandler => _responseHandler
.ErrorCase("404", CreateErrorCase("Not Found", (_reason, _context) => new ApiException(_reason, _context))))
.ErrorCase("404", CreateErrorCase("Not Found:'{$response.body}'", (_reason, _context) => new ApiException(_reason, _context), true)))
.ExecuteAsync(cancellationToken).ConfigureAwait(false);

/// <summary>
Expand All @@ -245,11 +246,11 @@ public Models.BatchJobResponse ReadInvoicesExport(
=> await CreateApiCall<Models.BatchJobResponse>()
.RequestBuilder(_requestBuilder => _requestBuilder
.Setup(HttpMethod.Get, "/api_exports/invoices/{batch_id}.json")
.WithAuth("BasicAuth")
.WithAuth("global")
.Parameters(_parameters => _parameters
.Template(_template => _template.Setup("batch_id", batchId).Required())))
.ResponseHandler(_responseHandler => _responseHandler
.ErrorCase("404", CreateErrorCase("Not Found", (_reason, _context) => new ApiException(_reason, _context))))
.ErrorCase("404", CreateErrorCase("Not Found:'{$response.body}'", (_reason, _context) => new ApiException(_reason, _context), true)))
.ExecuteAsync(cancellationToken).ConfigureAwait(false);

/// <summary>
Expand All @@ -273,11 +274,11 @@ public Models.BatchJobResponse ReadSubscriptionsExport(
=> await CreateApiCall<Models.BatchJobResponse>()
.RequestBuilder(_requestBuilder => _requestBuilder
.Setup(HttpMethod.Get, "/api_exports/subscriptions/{batch_id}.json")
.WithAuth("BasicAuth")
.WithAuth("global")
.Parameters(_parameters => _parameters
.Template(_template => _template.Setup("batch_id", batchId).Required())))
.ResponseHandler(_responseHandler => _responseHandler
.ErrorCase("404", CreateErrorCase("Not Found", (_reason, _context) => new ApiException(_reason, _context))))
.ErrorCase("404", CreateErrorCase("Not Found:'{$response.body}'", (_reason, _context) => new ApiException(_reason, _context), true)))
.ExecuteAsync(cancellationToken).ConfigureAwait(false);
}
}
Loading
Loading