-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFacturapiClient.cs
More file actions
85 lines (75 loc) · 3.56 KB
/
FacturapiClient.cs
File metadata and controls
85 lines (75 loc) · 3.56 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using Facturapi.Wrappers;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace Facturapi
{
public sealed class FacturapiClient : IFacturapiClient
{
public ICustomerWrapper Customer { get; private set; }
public IProductWrapper Product { get; private set; }
public IInvoiceWrapper Invoice { get; private set; }
public IOrganizationWrapper Organization { get; private set; }
public IReceiptWrapper Receipt { get; private set; }
public IRetentionWrapper Retention { get; private set; }
public ICatalogWrapper Catalog { get; private set; }
public ICartaporteCatalogWrapper CartaporteCatalog { get; private set; }
public IToolWrapper Tool { get; private set; }
public IWebhookWrapper Webhook { get; private set; }
private readonly HttpClient httpClient;
private readonly bool ownsHttpClient;
private bool disposed;
public FacturapiClient(string apiKey, string apiVersion = "v2")
: this(apiKey, apiVersion, CreateDefaultHttpClient(apiKey, apiVersion), ownsHttpClient: true)
{
}
private FacturapiClient(string apiKey, string apiVersion, HttpClient httpClient, bool ownsHttpClient)
{
this.httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
this.ownsHttpClient = ownsHttpClient;
ConfigureHttpClient(this.httpClient, apiKey, apiVersion);
this.Customer = new CustomerWrapper(apiKey, apiVersion, this.httpClient);
this.Product = new ProductWrapper(apiKey, apiVersion, this.httpClient);
this.Invoice = new InvoiceWrapper(apiKey, apiVersion, this.httpClient);
this.Organization = new OrganizationWrapper(apiKey, apiVersion, this.httpClient);
this.Receipt = new ReceiptWrapper(apiKey, apiVersion, this.httpClient);
this.Retention = new RetentionWrapper(apiKey, apiVersion, this.httpClient);
this.Catalog = new CatalogWrapper(apiKey, apiVersion, this.httpClient);
this.CartaporteCatalog = new CartaporteCatalogWrapper(apiKey, apiVersion, this.httpClient);
this.Tool = new ToolWrapper(apiKey, apiVersion, this.httpClient);
this.Webhook = new WebhookWrapper(apiKey, apiVersion, this.httpClient);
}
public static FacturapiClient CreateWithCustomHttpClient(string apiKey, HttpClient httpClient, string apiVersion = "v2")
{
if (httpClient == null)
{
throw new ArgumentNullException(nameof(httpClient));
}
return new FacturapiClient(apiKey, apiVersion, httpClient, ownsHttpClient: false);
}
public void Dispose()
{
if (this.disposed)
{
return;
}
if (this.ownsHttpClient)
{
this.httpClient.Dispose();
}
this.disposed = true;
GC.SuppressFinalize(this);
}
private static HttpClient CreateDefaultHttpClient(string apiKey, string apiVersion)
{
return new HttpClient();
}
private static void ConfigureHttpClient(HttpClient client, string apiKey, string apiVersion)
{
var apiKeyBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(apiKey + ":"));
client.BaseAddress = new Uri($"https://www.facturapi.io/{apiVersion}/");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", apiKeyBase64);
}
}
}