-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathApi.php
189 lines (154 loc) · 4.51 KB
/
Api.php
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
declare(strict_types=1);
namespace Sysix\LexOffice;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
use SensitiveParameter;
use Sysix\LexOffice\Clients\Article;
use Sysix\LexOffice\Clients\Contact;
use Sysix\LexOffice\Clients\Country;
use Sysix\LexOffice\Clients\CreditNote;
use Sysix\LexOffice\Clients\DeliveryNote;
use Sysix\LexOffice\Clients\DownPaymentInvoice;
use Sysix\LexOffice\Clients\Dunning;
use Sysix\LexOffice\Clients\Event;
use Sysix\LexOffice\Clients\File;
use Sysix\LexOffice\Clients\Invoice;
use Sysix\LexOffice\Clients\OrderConfirmation;
use Sysix\LexOffice\Clients\Payment;
use Sysix\LexOffice\Clients\PaymentCondition;
use Sysix\LexOffice\Clients\PostingCategory;
use Sysix\LexOffice\Clients\PrintLayout;
use Sysix\LexOffice\Clients\Profile;
use Sysix\LexOffice\Clients\Quotation;
use Sysix\LexOffice\Clients\RecurringTemplate;
use Sysix\LexOffice\Clients\Voucher;
use Sysix\LexOffice\Clients\VoucherList;
use Sysix\LexOffice\Interfaces\ApiInterface;
class Api implements ApiInterface
{
public string $apiUrl = 'https://api.lexoffice.io';
protected string $apiVersion = 'v1';
protected RequestInterface $request;
public function __construct(
#[SensitiveParameter] protected string $apiKey,
protected ClientInterface $client
) {
}
public function newRequest(string $method, string $resource, array $headers = []): self
{
return $this->setRequest(
new Request($method, $this->createApiUri($resource), $headers)
);
}
public function setRequest(RequestInterface $request): self
{
if (!$request->hasHeader('Authorization')) {
$request = $request->withHeader('Authorization', 'Bearer ' . $this->apiKey);
}
if (!$request->hasHeader('Accept')) {
$request = $request->withHeader('Accept', 'application/json');
}
if (!$request->hasHeader('Content-Type') && in_array($request->getMethod(), ['POST', 'PUT'], true)) {
$request = $request->withHeader('Content-Type', 'application/json');
}
$this->request = $request;
return $this;
}
public function getRequest(): RequestInterface
{
return $this->request;
}
public function getResponse(): ResponseInterface
{
return $this->client->sendRequest($this->request);
}
protected function createApiUri(string $resource): UriInterface
{
return new Uri($this->apiUrl . '/' . $this->apiVersion . '/' . $resource);
}
public function article(): Article
{
return new Article($this);
}
public function contact(): Contact
{
return new Contact($this);
}
public function country(): Country
{
return new Country($this);
}
public function creditNote(): CreditNote
{
return new CreditNote($this);
}
public function deliveryNote(): DeliveryNote
{
return new DeliveryNote($this);
}
public function downPaymentInvoice(): DownPaymentInvoice
{
return new DownPaymentInvoice($this);
}
public function dunning(): Dunning
{
return new Dunning($this);
}
public function event(): Event
{
return new Event($this);
}
public function file(): File
{
return new File($this);
}
public function invoice(): Invoice
{
return new Invoice($this);
}
public function orderConfirmation(): OrderConfirmation
{
return new OrderConfirmation($this);
}
public function payment(): Payment
{
return new Payment($this);
}
public function paymentCondition(): PaymentCondition
{
return new PaymentCondition($this);
}
public function postingCategory(): PostingCategory
{
return new PostingCategory($this);
}
public function printLayout(): PrintLayout
{
return new PrintLayout($this);
}
public function profile(): Profile
{
return new Profile($this);
}
public function quotation(): Quotation
{
return new Quotation($this);
}
public function recurringTemplate(): RecurringTemplate
{
return new RecurringTemplate($this);
}
public function voucher(): Voucher
{
return new Voucher($this);
}
public function voucherlist(): VoucherList
{
return new VoucherList($this);
}
}