Skip to content

Commit 3e43fd4

Browse files
committed
download-file-accept-header
1 parent 3a5dd29 commit 3e43fd4

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ $response = $api->invoice()->get($entityId);
8686
$response = $api->invoice()->create($data);
8787
$response = $api->invoice()->document($entityId); // get document ID
8888
$response = $api->invoice()->document($entityId, true); // get file content
89+
$response = $api->invoice()->document($entityId, true, 'image/*'); // accept only images
90+
$response = $api->invoice()->document($entityId, true, 'application/xml'); // get XRechung XML File (if possible)
8991
```
9092

9193
### Down Payment Invoices Endpoint
@@ -95,6 +97,8 @@ $response = $api->downPaymentInvoice()->get($entityId);
9597
$response = $api->downPaymentInvoice()->create($data);
9698
$response = $api->downPaymentInvoice()->document($entityId); // get document ID
9799
$response = $api->downPaymentInvoice()->document($entityId, true); // get file content
100+
$response = $api->downPaymentInvoice()->document($entityId, true, 'image/*'); // accept only images
101+
$response = $api->downPaymentInvoice()->document($entityId, true, 'application/xml'); // get XRechung XML File (if possible)
98102
```
99103

100104
### Order Confirmation Endpoint
@@ -104,6 +108,8 @@ $response = $api->orderConfirmation()->get($entityId);
104108
$response = $api->orderConfirmation()->create($data);
105109
$response = $api->orderConfirmation()->document($entityId); // get document ID
106110
$response = $api->orderConfirmation()->document($entityId, true); // get file content
111+
$response = $api->orderConfirmation()->document($entityId, true, 'image/*'); // accept only images
112+
$response = $api->orderConfirmation()->document($entityId, true, 'application/xml'); // get XRechung XML File (if possible)
107113
```
108114

109115
### Quotation Endpoint
@@ -113,6 +119,8 @@ $response = $api->quotation()->get($entityId);
113119
$response = $api->quotation()->create($data);
114120
$response = $api->quotation()->document($entityId); // get document ID
115121
$response = $api->quotation()->document($entityId, true); // get file content
122+
$response = $api->quotation()->document($entityId, true, 'image/*'); // accept only images
123+
$response = $api->quotation()->document($entityId, true, 'application/xml'); // get XRechung XML File (if possible)
116124
```
117125

118126
### Voucher Endpoint
@@ -122,6 +130,8 @@ $response = $api->voucher()->create($data);
122130
$response = $api->voucher()->update($entityId, $data);
123131
$response = $api->voucher()->document($entityId); // get document ID
124132
$response = $api->voucher()->document($entityId, true); // get file content
133+
$response = $api->voucher()->document($entityId, true, 'image/*'); // accept only images
134+
$response = $api->voucher()->document($entityId, true, 'application/xml'); // get XRechung XML File (if possible)
125135
$response = $api->voucher()->upload($entitiyId, $filepath);
126136
```
127137

@@ -133,6 +143,8 @@ $response = $api->creditNote()->get($entityId);
133143
$response = $api->creditNote()->create($data);
134144
$response = $api->creditNote()->document($entityId); // get document ID
135145
$response = $api->creditNote()->document($entityId, true); // get file content
146+
$response = $api->creditNote()->document($entityId, true, 'image/*'); // accept only images
147+
$response = $api->creditNote()->document($entityId, true, 'application/xml'); // get XRechung XML File (if possible)
136148
```
137149

138150
### Payment Endpoint
@@ -221,7 +233,9 @@ $response = $client->getPage(0);
221233
### File Endpoint
222234
```php
223235
$response = $api->file()->upload($filePath, $voucherType);
224-
$response = $api->file()->get($entityId);
236+
$response = $api->file()->get($entityId); // accept every file
237+
$response = $api->file()->get($entityId, 'image/*'); // accept only images
238+
$response = $api->file()->get($entityId, 'application/xml'); // get XRechung XML File (if possible)
225239
```
226240

227241

src/Api.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ public function newRequest(string $method, string $resource, array $headers = []
5656

5757
public function setRequest(RequestInterface $request): self
5858
{
59-
$request = $request
60-
->withHeader('Authorization', 'Bearer ' . $this->apiKey)
61-
->withHeader('Accept', 'application/json');
59+
$request = $request->withHeader('Authorization', 'Bearer ' . $this->apiKey);
60+
61+
if (!$request->hasHeader('Accept')) {
62+
$request = $request->withHeader('Accept', 'application/json');
63+
}
6264

6365
if (!$request->hasHeader('Content-Type') && in_array($request->getMethod(), ['POST', 'PUT'], true)) {
6466
$request = $request->withHeader('Content-Type', 'application/json');

src/Clients/File.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,24 @@
55
namespace Sysix\LexOffice\Clients;
66

77
use Sysix\LexOffice\BaseClient;
8-
use Sysix\LexOffice\Clients\Traits\GetTrait;
98
use Sysix\LexOffice\Exceptions\LexOfficeApiException;
109
use Psr\Http\Message\ResponseInterface;
1110
use Sysix\LexOffice\Config\FileClientConfig;
1211
use Sysix\LexOffice\Utils;
1312

1413
class File extends BaseClient
1514
{
16-
use GetTrait;
17-
1815
protected string $resource = 'files';
1916

17+
public function get(string $id, string $acceptHeader = '*/*'): ResponseInterface
18+
{
19+
$this->api->newRequest('GET', $this->resource . '/' . rawurlencode($id));
20+
21+
$this->api->request = $this->api->request->withHeader('Accept', $acceptHeader);
22+
23+
return $this->api->getResponse();
24+
}
25+
2026
/**
2127
* @throws LexOfficeApiException
2228
*/

src/Clients/Traits/DocumentClientTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
trait DocumentClientTrait
1313
{
14-
public function document(string $id, bool $asContent = false): ResponseInterface
14+
public function document(string $id, bool $asContent = false, string $acceptHeader = '*/*'): ResponseInterface
1515
{
1616
$response = $this->api
1717
->newRequest('GET', $this->resource . '/' . rawurlencode($id) . '/document')
@@ -34,6 +34,6 @@ public function document(string $id, bool $asContent = false): ResponseInterface
3434

3535
$fileClient = new File($this->api);
3636

37-
return $fileClient->get($content->documentFileId);
37+
return $fileClient->get($content->documentFileId, $acceptHeader);
3838
}
3939
}

tests/Clients/FileTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,22 @@ public function testGet(): void
2121
$this->assertInstanceOf(ResponseInterface::class, $response);
2222

2323
$this->assertEquals('GET', $api->request->getMethod());
24+
$this->assertEquals('*/*', $api->request->getHeaderLine('Accept'));
2425
$this->assertEquals(
2526
$api->apiUrl . '/v1/files/resource-id',
2627
$api->request->getUri()->__toString()
2728
);
2829
}
2930

31+
public function testGetWithAccceptHeader(): void
32+
{
33+
[$api, $stub] = $this->createClientMockObject(File::class);
34+
35+
$stub->get('resource-id', 'application/xml');
36+
37+
$this->assertEquals('application/xml', $api->request->getHeaderLine('Accept'));
38+
}
39+
3040
public function testUploadNotSupportedExtension(): void
3141
{
3242
$this->expectException(LexOfficeApiException::class);

0 commit comments

Comments
 (0)