|
1 | | -namespace Facturapi |
| 1 | +using Facturapi.Wrappers; |
| 2 | +using System; |
| 3 | +using System.Net.Http; |
| 4 | +using System.Net.Http.Headers; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace Facturapi |
2 | 8 | { |
3 | | - public class FacturapiClient |
| 9 | + public sealed class FacturapiClient : IFacturapiClient |
4 | 10 | { |
5 | | - public Wrappers.CustomerWrapper Customer { get; private set; } |
6 | | - public Wrappers.ProductWrapper Product { get; private set; } |
7 | | - public Wrappers.InvoiceWrapper Invoice { get; private set; } |
8 | | - public Wrappers.OrganizationWrapper Organization { get; private set; } |
9 | | - public Wrappers.ReceiptWrapper Receipt { get; private set; } |
10 | | - public Wrappers.RetentionWrapper Retention { get; private set; } |
11 | | - public Wrappers.CatalogWrapper Catalog { get; private set; } |
12 | | - public Wrappers.CatalogWrapper CartaporteCatalog { get; private set; } |
13 | | - public Wrappers.ToolWrapper Tool { get; private set; } |
| 11 | + public CustomerWrapper Customer { get; private set; } |
| 12 | + public ProductWrapper Product { get; private set; } |
| 13 | + public InvoiceWrapper Invoice { get; private set; } |
| 14 | + public OrganizationWrapper Organization { get; private set; } |
| 15 | + public ReceiptWrapper Receipt { get; private set; } |
| 16 | + public RetentionWrapper Retention { get; private set; } |
| 17 | + public CatalogWrapper Catalog { get; private set; } |
| 18 | + public CartaporteCatalogWrapper CartaporteCatalog { get; private set; } |
| 19 | + public ToolWrapper Tool { get; private set; } |
| 20 | + public WebhookWrapper Webhook { get; private set; } |
| 21 | + private readonly HttpClient httpClient; |
| 22 | + private bool disposed; |
14 | 23 |
|
15 | 24 | public FacturapiClient(string apiKey, string apiVersion = "v2") |
16 | 25 | { |
17 | | - this.Customer = new Wrappers.CustomerWrapper(apiKey, apiVersion); |
18 | | - this.Product = new Wrappers.ProductWrapper(apiKey, apiVersion); |
19 | | - this.Invoice = new Wrappers.InvoiceWrapper(apiKey, apiVersion); |
20 | | - this.Organization = new Wrappers.OrganizationWrapper(apiKey, apiVersion); |
21 | | - this.Receipt = new Wrappers.ReceiptWrapper(apiKey, apiVersion); |
22 | | - this.Retention = new Wrappers.RetentionWrapper(apiKey, apiVersion); |
23 | | - this.Catalog = new Wrappers.CatalogWrapper(apiKey, apiVersion); |
24 | | - this.CartaporteCatalog = new Wrappers.CatalogWrapper(apiKey, apiVersion); |
25 | | - this.Tool = new Wrappers.ToolWrapper(apiKey, apiVersion); |
| 26 | + var apiKeyBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(apiKey + ":")); |
| 27 | + this.httpClient = new HttpClient |
| 28 | + { |
| 29 | + BaseAddress = new Uri($"https://www.facturapi.io/{apiVersion}/") |
| 30 | + }; |
| 31 | + this.httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", apiKeyBase64); |
| 32 | + |
| 33 | + this.Customer = new CustomerWrapper(apiKey, apiVersion, this.httpClient); |
| 34 | + this.Product = new ProductWrapper(apiKey, apiVersion, this.httpClient); |
| 35 | + this.Invoice = new InvoiceWrapper(apiKey, apiVersion, this.httpClient); |
| 36 | + this.Organization = new OrganizationWrapper(apiKey, apiVersion, this.httpClient); |
| 37 | + this.Receipt = new ReceiptWrapper(apiKey, apiVersion, this.httpClient); |
| 38 | + this.Retention = new RetentionWrapper(apiKey, apiVersion, this.httpClient); |
| 39 | + this.Catalog = new CatalogWrapper(apiKey, apiVersion, this.httpClient); |
| 40 | + this.CartaporteCatalog = new CartaporteCatalogWrapper(apiKey, apiVersion, this.httpClient); |
| 41 | + this.Tool = new ToolWrapper(apiKey, apiVersion, this.httpClient); |
| 42 | + this.Webhook = new WebhookWrapper(apiKey, apiVersion, this.httpClient); |
| 43 | + } |
| 44 | + |
| 45 | + public void Dispose() |
| 46 | + { |
| 47 | + if (this.disposed) |
| 48 | + { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + this.httpClient?.Dispose(); |
| 53 | + this.disposed = true; |
| 54 | + GC.SuppressFinalize(this); |
26 | 55 | } |
27 | 56 | } |
28 | 57 | } |
0 commit comments