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

Adding an Interface for DI and CancellationToken for async methods #48

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
25 changes: 25 additions & 0 deletions src/Taxjar/DependencyInjection/TaxjarOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#if NETSTANDARD2_0
using System;

namespace Taxjar.Options
{
public class TaxjarOptions
{

/// <summary>
/// Taxjar token.
/// </summary>
public string ApiToken { get; set; }

/// <summary>
/// Is Sandbox?
/// </summary>
public bool IsSandBox { get; set; }
kdcllc marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Timeout for the HttpClient.
/// </summary>
public TimeSpan Timeout { get; set; }
kdcllc marked this conversation as resolved.
Show resolved Hide resolved
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#if NETSTANDARD2_0

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

using System;

using Taxjar.Options;

namespace Taxjar.DependencyInjection
{
/// <summary>
/// Add TaxjarApi to Dependency Injection.
/// </summary>
public static class TaxjarServiceCollectionExtensions
{
public static IServiceCollection AddTaxjar(this IServiceCollection services,
Action<TaxjarOptions> configure = default)
{

services.AddChangeTokenOptions(nameof(TaxjarOptions), string.Empty, configure);
kdcllc marked this conversation as resolved.
Show resolved Hide resolved

services.AddTransient<ITaxjarApi, TaxjarApi>(sp =>
{
var options = sp.GetRequiredService<IOptions<TaxjarOptions>>().Value;

var baseUrl = options.IsSandBox ? "https://api.sandbox.taxjar.com" : "https://api.taxjar.com";
return new TaxjarApi(
options.ApiToken,
new { apiUrl = baseUrl, timeout = (int)options.Timeout.TotalMilliseconds });
});

return services;
}
}
}
#endif
113 changes: 113 additions & 0 deletions src/Taxjar/ITaxjarApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Taxjar
{
public interface ITaxjarApi
{
string apiToken { get; set; }


string apiUrl { get; set; }

IDictionary<string, string> headers { get; set; }

/// <summary>
/// Milliseconds.
/// </summary>
int timeout { get; set; }

List<Category> Categories();

Task<List<Category>> CategoriesAsync(CancellationToken cancellationToken = default);

CustomerResponseAttributes CreateCustomer(object parameters);

Task<CustomerResponseAttributes> CreateCustomerAsync(object parameters, CancellationToken cancellationToken = default);

OrderResponseAttributes CreateOrder(object parameters);

Task<OrderResponseAttributes> CreateOrderAsync(object parameters, CancellationToken cancellationToken = default);

RefundResponseAttributes CreateRefund(object parameters);

Task<RefundResponseAttributes> CreateRefundAsync(object parameters, CancellationToken cancellationToken = default);

CustomerResponseAttributes DeleteCustomer(string customerId);

Task<CustomerResponseAttributes> DeleteCustomerAsync(string customerId, CancellationToken cancellationToken = default);

OrderResponseAttributes DeleteOrder(string transactionId, object parameters = null);

Task<OrderResponseAttributes> DeleteOrderAsync(string transactionId, object parameters = null, CancellationToken cancellationToken = default);

RefundResponseAttributes DeleteRefund(string transactionId, object parameters = null);

Task<RefundResponseAttributes> DeleteRefundAsync(string transactionId, object parameters = null, CancellationToken cancellationToken = default);

object GetApiConfig(string key);

List<string> ListCustomers(object parameters = null);

Task<List<string>> ListCustomersAsync(object parameters = null, CancellationToken cancellationToken = default);

List<string> ListOrders(object parameters = null);

Task<List<string>> ListOrdersAsync(object parameters = null, CancellationToken cancellationToken = default);

List<string> ListRefunds(object parameters);

Task<List<string>> ListRefundsAsync(object parameters, CancellationToken cancellationToken = default);

List<NexusRegion> NexusRegions();

Task<List<NexusRegion>> NexusRegionsAsync(CancellationToken cancellationToken = default);

RateResponseAttributes RatesForLocation(string zip, object parameters = null);

Task<RateResponseAttributes> RatesForLocationAsync(string zip, object parameters = null, CancellationToken cancellationToken = default);

void SetApiConfig(string key, object value);

CustomerResponseAttributes ShowCustomer(string customerId);

Task<CustomerResponseAttributes> ShowCustomerAsync(string customerId, CancellationToken cancellationToken = default);

OrderResponseAttributes ShowOrder(string transactionId, object parameters = null);

Task<OrderResponseAttributes> ShowOrderAsync(string transactionId, object parameters = null, CancellationToken cancellationToken = default);

RefundResponseAttributes ShowRefund(string transactionId, object parameters = null);

Task<RefundResponseAttributes> ShowRefundAsync(string transactionId, object parameters = null, CancellationToken cancellationToken = default);

List<SummaryRate> SummaryRates();

Task<List<SummaryRate>> SummaryRatesAsync(CancellationToken cancellationToken = default);

TaxResponseAttributes TaxForOrder(object parameters);

Task<TaxResponseAttributes> TaxForOrderAsync(object parameters, CancellationToken cancellationToken = default);

CustomerResponseAttributes UpdateCustomer(object parameters);

Task<CustomerResponseAttributes> UpdateCustomerAsync(object parameters, CancellationToken cancellationToken = default);

OrderResponseAttributes UpdateOrder(object parameters);

Task<OrderResponseAttributes> UpdateOrderAsync(object parameters, CancellationToken cancellationToken = default);

RefundResponseAttributes UpdateRefund(object parameters);

Task<RefundResponseAttributes> UpdateRefundAsync(object parameters, CancellationToken cancellationToken = default);

List<Address> ValidateAddress(object parameters);

Task<List<Address>> ValidateAddressAsync(object parameters, CancellationToken cancellationToken = default);

ValidationResponseAttributes ValidateVat(object parameters);

Task<ValidationResponseAttributes> ValidateVatAsync(object parameters, CancellationToken cancellationToken = default);
}
}
22 changes: 21 additions & 1 deletion src/Taxjar/Taxjar.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net452</TargetFrameworks>
Expand Down Expand Up @@ -31,4 +31,24 @@
<PackageReference Include="RestSharp" Version="106.10.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.10"/>
<PackageReference Include="Bet.Extensions.Options" Version="3.1.10"/>
kdcllc marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

<ItemGroup Label="SourceLink">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

<PropertyGroup Label="SourceLink Settings">
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>

<!-- https://github.com/kdcllc/Bet.AspNetCore/issues/103 -->
<DebugType>Embedded</DebugType>
<EmbedAllSources>True</EmbedAllSources>
</PropertyGroup>

</Project>
Loading