-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathAvalaraClient.cs
56 lines (51 loc) · 2.04 KB
/
AvalaraClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using Flurl.Http;
using OrderCloud.Catalyst;
using System;
using System.Collections.Generic;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace OrderCloud.Integrations.Tax.Avalara
{
public class AvalaraClient
{
// provided by the Avalara team to indentify this specific integration with OrderCloud
protected static string AppName = "a0o33000003vpfvAAA";
protected static string AvataxClientHeaderName = "X-Avalara-Client";
protected static IFlurlRequest BuildClient(AvalaraConfig config) => config.BaseUrl.WithBasicAuth(config.AccountID, config.LicenseKey);
/// <summary>
/// https://developer.avalara.com/api-reference/avatax/rest/v2/methods/Definitions/ListTaxCodes/
/// </summary>
public static async Task<List<AvalaraTaxCode>> ListTaxCodesAsync(string filterParam, AvalaraConfig config)
{
var tax = await BuildClient(config)
.AppendPathSegments("definitions", "taxcodes")
.SetQueryParam("$filter", filterParam)
.WithHeader(AvataxClientHeaderName, GetAvataxHeader())
.GetJsonWithErrorHandlingAsync<AvalaraFetchResult<AvalaraTaxCode>, AvalaraError>(config);
return tax.value;
}
/// <summary>
/// https://developer.avalara.com/api-reference/avatax/rest/v2/methods/Transactions/CreateTransaction/
/// </summary>
public static async Task<AvalaraTransactionModel> CreateTransaction(AvalaraCreateTransactionModel transaction, AvalaraConfig config)
{
var tax = await BuildClient(config)
.AppendPathSegments("transactions", "create")
.WithHeader(AvataxClientHeaderName, GetAvataxHeader())
.PostJsonWithErrorHandlingAsync<AvalaraError>(transaction, config)
.ReceiveJson<AvalaraTransactionModel>();
return tax;
}
// See https://developer.avalara.com/avatax/client-headers/
private static string GetAvataxHeader()
{
return $"{AppName};{GetNugetPackageVersion()};";
}
private static string GetNugetPackageVersion()
{
return Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
}
}
}