Skip to content
This repository was archived by the owner on Apr 14, 2024. It is now read-only.

Commit 2229ad5

Browse files
author
Julien Neuhart
committed
improving README + custom HTTP headers
1 parent 44e0369 commit 2229ad5

11 files changed

+122
-46
lines changed

README.md

+39-42
Original file line numberDiff line numberDiff line change
@@ -19,56 +19,53 @@ $ composer require thecodingmachine/gotenberg-php-client
1919
## Usage
2020

2121
```php
22-
<?php
23-
24-
namespace YourAwesomeNamespace;
25-
2622
use TheCodingMachine\Gotenberg\Client;
2723
use TheCodingMachine\Gotenberg\ClientException;
2824
use TheCodingMachine\Gotenberg\DocumentFactory;
2925
use TheCodingMachine\Gotenberg\HTMLRequest;
3026
use TheCodingMachine\Gotenberg\Request;
3127
use TheCodingMachine\Gotenberg\RequestException;
28+
use GuzzleHttp\Psr7\LazyOpenStream;
29+
30+
# create the client.
31+
$client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client());
32+
# ... or the following if you want the client to discover automatically an installed implementation of the PSR7 `HttpClient`.
33+
$client = new Client('http://localhost:3000');
34+
35+
# prepare the files required for your conversion.
3236

33-
class YourAwesomeClass {
37+
# from a path.
38+
$index = DocumentFactory::makeFromPath('index.html', '/path/to/file');
39+
# ... or from your own stream.
40+
$stream = new LazyOpenStream('/path/to/file', 'r');
41+
$index = DocumentFactory::makeFromStream('index.html', $stream);
42+
// ... or from a string.
43+
$index = DocumentFactory::makeFromString('index.html', '<html>Foo</html>');
44+
45+
$header = DocumentFactory::makeFromPath('header.html', '/path/to/file');
46+
$footer = DocumentFactory::makeFromPath('footer.html', '/path/to/file');
47+
$assets = [
48+
DocumentFactory::makeFromPath('style.css', '/path/to/file'),
49+
DocumentFactory::makeFromPath('img.png', '/path/to/file'),
50+
];
51+
52+
try {
53+
$request = new HTMLRequest($index);
54+
$request->setHeader($header);
55+
$request->setFooter($footer);
56+
$request->setAssets($assets);
57+
$request->setPaperSize(Request::A4);
58+
$request->setMargins(Request::NO_MARGINS);
59+
60+
# store method allows you to... store the resulting PDF in a particular destination.
61+
$client->store($request, 'path/you/want/the/pdf/to/be/stored.pdf');
3462

35-
public function yourAwesomeMethod()
36-
{
37-
$client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client());
38-
# or the following if you want the client to discover automatically an installed implementation of the PSR7 `HttpClient`.
39-
$client = new Client('http://localhost:3000');
40-
41-
# HTML conversion example.
42-
$index = DocumentFactory::makeFromPath('index.html', '/path/to/file');
43-
$header = DocumentFactory::makeFromPath('header.html', '/path/to/file');
44-
$footer = DocumentFactory::makeFromPath('footer.html', '/path/to/file');
45-
$assets = [
46-
DocumentFactory::makeFromPath('style.css', '/path/to/file'),
47-
DocumentFactory::makeFromPath('img.png', '/path/to/file'),
48-
];
49-
50-
try {
51-
$request = new HTMLRequest($index);
52-
$request->setHeader($header);
53-
$request->setFooter($footer);
54-
$request->setAssets($assets);
55-
$request->setPaperSize(Request::A4);
56-
$request->setMargins(Request::NO_MARGINS);
57-
58-
# store method allows you to... store the resulting PDF in a particular destination.
59-
$client->store($request, 'path/you/want/the/pdf/to/be/stored.pdf');
60-
61-
# if you wish to redirect the response directly to the browser, you may also use:
62-
$client->post($request);
63-
64-
} catch (RequestException $e) {
65-
# this exception is thrown if given paper size or margins are not correct.
66-
} catch (ClientException $e) {
67-
# this exception is thrown by the client if the API has returned a code != 200.
68-
} catch (\Exception $e) {
69-
# some (random?) exception.
70-
}
71-
}
63+
# if you wish to redirect the response directly to the browser, you may also use:
64+
$client->post($request);
65+
} catch (RequestException $e) {
66+
# this exception is thrown if given paper size or margins are not correct.
67+
} catch (ClientException $e) {
68+
# this exception is thrown by the client if the API has returned a code != 200.
7269
}
7370
```
7471

docker-compose.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ services:
66
image: thecodingmachine/php:7.3-v2-cli
77
container_name: php
88
environment:
9-
- PHP_EXTENSION_XDEBUG=1
9+
PHP_EXTENSION_XDEBUG: 1
1010
restart: 'no'
1111
volumes:
1212
- ./:/usr/src/app:rw
1313

1414
gotenberg:
15-
image: thecodingmachine/gotenberg:6.0.1
15+
image: thecodingmachine/gotenberg:6
16+
environment:
17+
LOG_LEVEL: 'DEBUG'
18+
DEFAULT_WAIT_TIMEOUT: '30.0'
1619
container_name: gotenberg
1720
restart: 'no'

src/Client.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,15 @@ private function makeMultipartFormDataRequest(GotenbergRequestInterface $request
7979
}
8080
$body = new MultipartStream($multipartData);
8181
$messageFactory = MessageFactoryDiscovery::find();
82-
83-
return $messageFactory
82+
$message = $messageFactory
8483
->createRequest('POST', $this->apiURL . $request->getPostURL())
8584
->withHeader('Content-Type', 'multipart/form-data; boundary="' . $body->getBoundary() . '"')
8685
->withBody($body);
86+
foreach ($request->getCustomHTTPHeaders() as $key => $value) {
87+
$message = $message->withHeader($key, $value);
88+
}
89+
90+
return $message;
8791
}
8892

8993
/**

src/GotenbergRequestInterface.php

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ interface GotenbergRequestInterface
88
{
99
public function getPostURL(): string;
1010

11+
/**
12+
* @return array<string,string>
13+
*/
14+
public function getCustomHTTPHeaders(): array;
15+
1116
/**
1217
* @return array<string,mixed>
1318
*/

src/HTMLRequest.php

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class HTMLRequest extends ChromeRequest implements GotenbergRequestInterface
1414

1515
public function __construct(Document $index)
1616
{
17+
parent::__construct();
1718
$this->index = $index;
1819
$this->assets = [];
1920
}

src/MergeRequest.php

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ final class MergeRequest extends Request implements GotenbergRequestInterface
1414
*/
1515
public function __construct(array $files)
1616
{
17+
parent::__construct();
1718
$this->files = $files;
1819
}
1920

src/OfficeRequest.php

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ final class OfficeRequest extends Request implements GotenbergRequestInterface
1919
*/
2020
public function __construct(array $files)
2121
{
22+
parent::__construct();
2223
$this->files = $files;
2324
}
2425

src/Request.php

+24
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ abstract class Request
2323
private const WEBHOOK_URL = 'webhookURL';
2424
private const WEBHOOK_URL_TIMEOUT = 'webhookURLTimeout';
2525

26+
private const WEBHOOK_URL_BASE_HTTP_HEADER_KEY = 'Gotenberg-Webhookurl-';
27+
2628
/** @var string|null */
2729
private $resultFilename;
2830

@@ -35,6 +37,14 @@ abstract class Request
3537
/** @var float|null */
3638
private $webhookURLTimeout;
3739

40+
/** @var array<string,string> */
41+
protected $customHTTPHeaders;
42+
43+
public function __construct()
44+
{
45+
$this->customHTTPHeaders = [];
46+
}
47+
3848
/**
3949
* @return array<string,mixed>
4050
*/
@@ -57,6 +67,14 @@ public function getFormValues(): array
5767
return $values;
5868
}
5969

70+
/**
71+
* @return array<string,string>
72+
*/
73+
public function getCustomHTTPHeaders(): array
74+
{
75+
return $this->customHTTPHeaders;
76+
}
77+
6078
public function setResultFilename(?string $resultFilename): void
6179
{
6280
$this->resultFilename = $resultFilename;
@@ -76,4 +94,10 @@ public function setWebhookURLTimeout(?float $webhookURLTimeout): void
7694
{
7795
$this->webhookURLTimeout = $webhookURLTimeout;
7896
}
97+
98+
public function addWebhookURLHTTPHeader(string $key, string $value): void
99+
{
100+
$key = self::WEBHOOK_URL_BASE_HTTP_HEADER_KEY . $key;
101+
$this->customHTTPHeaders[$key] = $value;
102+
}
79103
}

src/URLRequest.php

+9
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ final class URLRequest extends ChromeRequest implements GotenbergRequestInterfac
88
{
99
private const REMOTE_URL = 'remoteURL';
1010

11+
private const REMOTE_URL_BASE_HTTP_HEADER_KEY = 'Gotenberg-Remoteurl-';
12+
1113
/** @var string */
1214
private $URL;
1315

1416
public function __construct(string $URL)
1517
{
18+
parent::__construct();
1619
$this->URL = $URL;
1720
}
1821

@@ -31,4 +34,10 @@ public function getFormValues(): array
3134

3235
return $values;
3336
}
37+
38+
public function addRemoteURLHTTPHeader(string $key, string $value): void
39+
{
40+
$key = self::REMOTE_URL_BASE_HTTP_HEADER_KEY . $key;
41+
$this->customHTTPHeaders[$key] = $value;
42+
}
3443
}

tests/ClientTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,31 @@ public function testStore(): void
202202
$client->store($this->mergeRequest, $filePath);
203203
$this->assertFileExists($filePath);
204204
}
205+
206+
/**
207+
* @throws ClientException
208+
*/
209+
public function testWebhook(): void
210+
{
211+
$client = new Client(self::API_URL, new \Http\Adapter\Guzzle6\Client());
212+
$request = $this->createMergeRequest();
213+
$request->setWebhookURL('https://google.com');
214+
$request->setWebhookURLTimeout(5.0);
215+
$request->addWebhookURLHTTPHeader('A-Header', 'Foo');
216+
$response = $client->post($request);
217+
$this->assertEquals(200, $response->getStatusCode());
218+
}
219+
220+
/**
221+
* @throws RequestException
222+
* @throws ClientException
223+
*/
224+
public function testRemoteURLHTTPHeader(): void
225+
{
226+
$client = new Client(self::API_URL, new \Http\Adapter\Guzzle6\Client());
227+
$request = $this->createURLRequest();
228+
$request->addRemoteURLHTTPHeader('A-Header', 'Foo');
229+
$response = $client->post($request);
230+
$this->assertEquals(200, $response->getStatusCode());
231+
}
205232
}

tests/DocumentFactoryTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66

77
use GuzzleHttp\Psr7\LazyOpenStream;
88
use PHPUnit\Framework\TestCase;
9+
use Safe\Exceptions\FilesystemException;
910

1011
final class DocumentFactoryTest extends TestCase
1112
{
13+
/**
14+
* @throws FilesystemException
15+
*/
1216
public function testMake(): void
1317
{
1418
// case 1: uses a file path.

0 commit comments

Comments
 (0)