-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.cs
120 lines (101 loc) · 5.7 KB
/
program.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using AFASSB.Models;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
class Program
{
private static readonly HttpClient httpClient = new HttpClient();
private static string accessToken = string.Empty;
private static string ClientUrl = "https://app-center-demo.afasfocus.nl/demo";
private static string ClientId = "your client id";
private static string ClientSecret = "your client secret";
private static string RedirectUri = "https://try.afassb.nl/callback";
private static string refreshToken = string.Empty;
static async Task Main(string[] args)
{
AfasAuthClient afasAuthClient = new AfasAuthClient(ClientUrl, ClientId, ClientSecret, RedirectUri);
string state = Guid.NewGuid().ToString();
string codeVerifier = afasAuthClient.GenerateCodeVerifier();
string codeChallenge = afasAuthClient.GenerateCodeChallenge(codeVerifier);
string authorizationCode = await afasAuthClient.AuthorizeWithPKCE(state, codeChallenge);
await afasAuthClient.GetAccessTokenAsync(authorizationCode, codeVerifier, state);
// Get the access token from the AfasAuthClient
string accessToken = afasAuthClient.AccessToken;
// Create an instance of the "AfasApiClient" class
AfasApiClient afasApiClient = new AfasApiClient(ClientUrl, accessToken);
// Example usage
List<Organisation> organizations = await afasApiClient.GetOrganisationsAsync(take: 5);
List<Person> persons = await afasApiClient.GetPersonssAsync(take: 5);
List<PaymentCondition> paymentCondition = await afasApiClient.GetPaymentConditionsAsync(take: 5);
List<Administration> administrations = await afasApiClient.GetAdministrationsAsync(take: 5);
List<LedgerAccount> ledgerAccounts = await afasApiClient.GetLedgerAccountsAsync(take: 5);
// Send sample sales journal entry
var salesJournal = new SalesJournalEntry
{
AdministrationId = administrations[0].Id,
InvoiceDate = "2024-04-01",
InvoiceNumber = "INV-001",
RelationType = "organisation",
RelationId = organizations[0].Id,
InvoiceLine = new List<InvoiceLine>
{
new InvoiceLine
{
LedgerAccountId = ledgerAccounts[0].Id,
AmountExcludingVat = 100,
VatType = "high"
}
}.ToArray()
};
await afasApiClient.SendSalesJournalAsync(salesJournal);
Console.WriteLine("Sales journal entry sent successfully.");
}
private static async Task<List<Organisation>> GetOrganizations(int take = 100, int skip = 0, string filter = "")
{
var url = $"{ClientUrl}/api/organisations?take={take}&skip={skip}&filter={Uri.EscapeDataString(filter)}";
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
var result = await JsonSerializer.DeserializeAsync<JsonElement>(await response.Content.ReadAsStreamAsync());
return result.GetProperty("Result").Deserialize<List<Organisation>>();
}
private static async Task<List<Person>> GetPersons(int take = 100, int skip = 0, string filter = "")
{
var url = $"{ClientUrl}/api/persons?take={take}&skip={skip}&filter={Uri.EscapeDataString(filter)}";
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
var result = await JsonSerializer.DeserializeAsync<JsonElement>(await response.Content.ReadAsStreamAsync());
return result.GetProperty("Result").Deserialize<List<Person>>();
}
private static async Task<List<Administration>> GetAdministrations(int take = 100, int skip = 0, string filter = "")
{
var url = $"{ClientUrl}/api/administration?take={take}&skip={skip}&filter={Uri.EscapeDataString(filter)}";
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
var result = await JsonSerializer.DeserializeAsync<JsonElement>(await response.Content.ReadAsStreamAsync());
return result.Deserialize<List<Administration>>();
}
private static async Task<List<LedgerAccount>> GetLedgerAccounts(int take = 100, int skip = 0, string filter = "")
{
var url = $"{ClientUrl}/api/ledgeraccount?take={take}&skip={skip}&filter={Uri.EscapeDataString(filter)}";
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
var result = await JsonSerializer.DeserializeAsync<JsonElement>(await response.Content.ReadAsStreamAsync());
return result.Deserialize<List<LedgerAccount>>();
}
private static async Task SendSalesJournal(SalesJournalEntry salesJournal)
{
var url = $"{ClientUrl}/api/salesjournalentry";
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var response = await httpClient.PostAsJsonAsync(url, salesJournal);
response.EnsureSuccessStatusCode();
}
}