Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Async generation #84

Merged
merged 26 commits into from
Nov 19, 2024
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9915a29
Add ability to generate asynchronously using webhooks
maelanleborgne Jun 19, 2024
ec46e92
switch to constructor injection
maelanleborgne Sep 9, 2024
b26c12f
cleanup after review
maelanleborgne Oct 3, 2024
f97d4f5
Merge branch 'main' of github.com:Neirda24/GotenbergBundle into featu…
Neirda24 Oct 24, 2024
abd6e47
[UPDATE] Allow to set extra headers when using webhook
Neirda24 Oct 24, 2024
0c6cb61
[UPDATE] Auto docs
Neirda24 Oct 24, 2024
9fb37d7
[UPDATE] Remove custom id generator for the moment
Neirda24 Oct 31, 2024
ed70e51
[UPDATE] Remove strict types
Neirda24 Oct 31, 2024
f6509f6
[UPDATE] Docs
Neirda24 Oct 31, 2024
a40ec71
[UPDATE] Doc & doc generation
Neirda24 Nov 1, 2024
9faba1a
Merge branch 'main' of github.com:sensiolabs/GotenbergBundle into fea…
Neirda24 Nov 1, 2024
967bfad
[Fix] A few stuff
Neirda24 Nov 1, 2024
823faba
[UPDATE] A few stuff
Neirda24 Nov 1, 2024
83e1d26
[UPDATE] Add webhook methods
Neirda24 Nov 3, 2024
42a8544
[UPDATE] Exclude some methods to auto docs
Neirda24 Nov 3, 2024
a574f51
[UPDATE] Exclude some methods to auto docs
Neirda24 Nov 3, 2024
a9694cb
[UPDATE] Change equality check
Neirda24 Nov 3, 2024
8d39bd9
Update profiler icons + Fix webhook configuration + fix some tests
Neirda24 Nov 15, 2024
ebb4656
[Update] a few things
Neirda24 Nov 15, 2024
0f00fdf
fix typo
Neirda24 Nov 15, 2024
ee72e12
fix typo
Neirda24 Nov 15, 2024
eb474ad
Add documentation for webhook
Neirda24 Nov 16, 2024
44764dd
Update docs for webhooks
Neirda24 Nov 16, 2024
f8a1de9
Update docs for webhooks
Neirda24 Nov 16, 2024
c461a88
Add tests on async trait
Neirda24 Nov 16, 2024
931943a
[FIX] CI
Neirda24 Nov 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[UPDATE] Add webhook methods
  • Loading branch information
Neirda24 committed Nov 3, 2024
commit 83e1d26fbe303e2f287c47c0197e4eb2bed4b8ed
14 changes: 11 additions & 3 deletions docs/pdf/builders_api/ConvertPdfBuilder.md
Original file line number Diff line number Diff line change
@@ -10,10 +10,18 @@ Enable PDF for Universal Access for optimal accessibility.

* `downloadFrom(array $downloadFrom)`:

* `webhookConfiguration(string $webhook)`:
Providing an existing $webhook from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.
* `webhookConfiguration(string $name)`:
Providing an existing $name from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.

* `webhookUrls(string $successWebhook, ?string $errorWebhook)`:
* `webhookUrl(string $url, ?string $method)`:
Sets the webhook for cases of success.
Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.

* `errorWebhookUrl(?string $url, ?string $method)`:
Sets the webhook for cases of error.
Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.

* `webhookUrls(string $successWebhook, ?string $errorWebhook, ?string $successMethod, ?string $errorMethod)`:
Allows to set both $successWebhook and $errorWebhook URLs. If $errorWebhook is not provided, it will fallback to $successWebhook one.

* `webhookExtraHeaders(array $extraHeaders)`:
14 changes: 11 additions & 3 deletions docs/pdf/builders_api/HtmlPdfBuilder.md
Original file line number Diff line number Diff line change
@@ -129,10 +129,18 @@ The metadata to write.

* `downloadFrom(array $downloadFrom)`:

* `webhookConfiguration(string $webhook)`:
Providing an existing $webhook from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.
* `webhookConfiguration(string $name)`:
Providing an existing $name from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.

* `webhookUrls(string $successWebhook, ?string $errorWebhook)`:
* `webhookUrl(string $url, ?string $method)`:
Sets the webhook for cases of success.
Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.

* `errorWebhookUrl(?string $url, ?string $method)`:
Sets the webhook for cases of error.
Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.

* `webhookUrls(string $successWebhook, ?string $errorWebhook, ?string $successMethod, ?string $errorMethod)`:
Allows to set both $successWebhook and $errorWebhook URLs. If $errorWebhook is not provided, it will fallback to $successWebhook one.

* `webhookExtraHeaders(array $extraHeaders)`:
14 changes: 11 additions & 3 deletions docs/pdf/builders_api/LibreOfficePdfBuilder.md
Original file line number Diff line number Diff line change
@@ -87,10 +87,18 @@ If the form field reduceImageResolution is set to true, tell if all images will

* `downloadFrom(array $downloadFrom)`:

* `webhookConfiguration(string $webhook)`:
Providing an existing $webhook from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.
* `webhookConfiguration(string $name)`:
Providing an existing $name from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.

* `webhookUrls(string $successWebhook, ?string $errorWebhook)`:
* `webhookUrl(string $url, ?string $method)`:
Sets the webhook for cases of success.
Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.

* `errorWebhookUrl(?string $url, ?string $method)`:
Sets the webhook for cases of error.
Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.

* `webhookUrls(string $successWebhook, ?string $errorWebhook, ?string $successMethod, ?string $errorMethod)`:
Allows to set both $successWebhook and $errorWebhook URLs. If $errorWebhook is not provided, it will fallback to $successWebhook one.

* `webhookExtraHeaders(array $extraHeaders)`:
14 changes: 11 additions & 3 deletions docs/pdf/builders_api/MarkdownPdfBuilder.md
Original file line number Diff line number Diff line change
@@ -132,10 +132,18 @@ The metadata to write.

* `downloadFrom(array $downloadFrom)`:

* `webhookConfiguration(string $webhook)`:
Providing an existing $webhook from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.
* `webhookConfiguration(string $name)`:
Providing an existing $name from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.

* `webhookUrls(string $successWebhook, ?string $errorWebhook)`:
* `webhookUrl(string $url, ?string $method)`:
Sets the webhook for cases of success.
Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.

* `errorWebhookUrl(?string $url, ?string $method)`:
Sets the webhook for cases of error.
Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.

* `webhookUrls(string $successWebhook, ?string $errorWebhook, ?string $successMethod, ?string $errorMethod)`:
Allows to set both $successWebhook and $errorWebhook URLs. If $errorWebhook is not provided, it will fallback to $successWebhook one.

* `webhookExtraHeaders(array $extraHeaders)`:
14 changes: 11 additions & 3 deletions docs/pdf/builders_api/MergePdfBuilder.md
Original file line number Diff line number Diff line change
@@ -18,10 +18,18 @@ The metadata to write.

* `downloadFrom(array $downloadFrom)`:

* `webhookConfiguration(string $webhook)`:
Providing an existing $webhook from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.
* `webhookConfiguration(string $name)`:
Providing an existing $name from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.

* `webhookUrls(string $successWebhook, ?string $errorWebhook)`:
* `webhookUrl(string $url, ?string $method)`:
Sets the webhook for cases of success.
Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.

* `errorWebhookUrl(?string $url, ?string $method)`:
Sets the webhook for cases of error.
Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.

* `webhookUrls(string $successWebhook, ?string $errorWebhook, ?string $successMethod, ?string $errorMethod)`:
Allows to set both $successWebhook and $errorWebhook URLs. If $errorWebhook is not provided, it will fallback to $successWebhook one.

* `webhookExtraHeaders(array $extraHeaders)`:
14 changes: 11 additions & 3 deletions docs/pdf/builders_api/UrlPdfBuilder.md
Original file line number Diff line number Diff line change
@@ -131,10 +131,18 @@ The metadata to write.

* `downloadFrom(array $downloadFrom)`:

* `webhookConfiguration(string $webhook)`:
Providing an existing $webhook from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.
* `webhookConfiguration(string $name)`:
Providing an existing $name from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.

* `webhookUrls(string $successWebhook, ?string $errorWebhook)`:
* `webhookUrl(string $url, ?string $method)`:
Sets the webhook for cases of success.
Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.

* `errorWebhookUrl(?string $url, ?string $method)`:
Sets the webhook for cases of error.
Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.

* `webhookUrls(string $successWebhook, ?string $errorWebhook, ?string $successMethod, ?string $errorMethod)`:
Allows to set both $successWebhook and $errorWebhook URLs. If $errorWebhook is not provided, it will fallback to $successWebhook one.

* `webhookExtraHeaders(array $extraHeaders)`:
14 changes: 11 additions & 3 deletions docs/screenshot/builders_api/HtmlScreenshotBuilder.md
Original file line number Diff line number Diff line change
@@ -74,10 +74,18 @@ Adds a file, like an image, font, stylesheet, and so on.

* `downloadFrom(array $downloadFrom)`:

* `webhookConfiguration(string $webhook)`:
Providing an existing $webhook from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.
* `webhookConfiguration(string $name)`:
Providing an existing $name from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.

* `webhookUrls(string $successWebhook, ?string $errorWebhook)`:
* `webhookUrl(string $url, ?string $method)`:
Sets the webhook for cases of success.
Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.

* `errorWebhookUrl(?string $url, ?string $method)`:
Sets the webhook for cases of error.
Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.

* `webhookUrls(string $successWebhook, ?string $errorWebhook, ?string $successMethod, ?string $errorMethod)`:
Allows to set both $successWebhook and $errorWebhook URLs. If $errorWebhook is not provided, it will fallback to $successWebhook one.

* `webhookExtraHeaders(array $extraHeaders)`:
14 changes: 11 additions & 3 deletions docs/screenshot/builders_api/MarkdownScreenshotBuilder.md
Original file line number Diff line number Diff line change
@@ -77,10 +77,18 @@ Adds a file, like an image, font, stylesheet, and so on.

* `downloadFrom(array $downloadFrom)`:

* `webhookConfiguration(string $webhook)`:
Providing an existing $webhook from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.
* `webhookConfiguration(string $name)`:
Providing an existing $name from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.

* `webhookUrls(string $successWebhook, ?string $errorWebhook)`:
* `webhookUrl(string $url, ?string $method)`:
Sets the webhook for cases of success.
Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.

* `errorWebhookUrl(?string $url, ?string $method)`:
Sets the webhook for cases of error.
Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.

* `webhookUrls(string $successWebhook, ?string $errorWebhook, ?string $successMethod, ?string $errorMethod)`:
Allows to set both $successWebhook and $errorWebhook URLs. If $errorWebhook is not provided, it will fallback to $successWebhook one.

* `webhookExtraHeaders(array $extraHeaders)`:
14 changes: 11 additions & 3 deletions docs/screenshot/builders_api/UrlScreenshotBuilder.md
Original file line number Diff line number Diff line change
@@ -76,10 +76,18 @@ Adds a file, like an image, font, stylesheet, and so on.

* `downloadFrom(array $downloadFrom)`:

* `webhookConfiguration(string $webhook)`:
Providing an existing $webhook from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.
* `webhookConfiguration(string $name)`:
Providing an existing $name from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.

* `webhookUrls(string $successWebhook, ?string $errorWebhook)`:
* `webhookUrl(string $url, ?string $method)`:
Sets the webhook for cases of success.
Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.

* `errorWebhookUrl(?string $url, ?string $method)`:
Sets the webhook for cases of error.
Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.

* `webhookUrls(string $successWebhook, ?string $errorWebhook, ?string $successMethod, ?string $errorMethod)`:
Allows to set both $successWebhook and $errorWebhook URLs. If $errorWebhook is not provided, it will fallback to $successWebhook one.

* `webhookExtraHeaders(array $extraHeaders)`:
4 changes: 4 additions & 0 deletions src/Builder/AsyncBuilderInterface.php
Original file line number Diff line number Diff line change
@@ -2,10 +2,14 @@

namespace Sensiolabs\GotenbergBundle\Builder;

use Sensiolabs\GotenbergBundle\Exception\MissingRequiredFieldException;

interface AsyncBuilderInterface
{
/**
* Generates a file asynchronously.
*
* @throws MissingRequiredFieldException if webhook URL was not configured
*/
public function generateAsync(): void;
}
90 changes: 76 additions & 14 deletions src/Builder/AsyncBuilderTrait.php
Original file line number Diff line number Diff line change
@@ -9,9 +9,19 @@ trait AsyncBuilderTrait
{
use DefaultBuilderTrait;

private string $webhookUrl;
private string|null $successWebhookUrl = null;

private string $errorWebhookUrl;
/**
* @var 'POST'|'PATCH'|'PUT'|null
*/
private string|null $successWebhookMethod = null;

private string|null $errorWebhookUrl = null;

/**
* @var 'POST'|'PATCH'|'PUT'|null
*/
private string|null $errorWebhookMethod = null;

/**
* @var array<string, mixed>
@@ -22,17 +32,29 @@ trait AsyncBuilderTrait

public function generateAsync(): void
{
if (!isset($this->webhookUrl)) {
if (null === $this->successWebhookUrl) {
throw new MissingRequiredFieldException('->webhookUrls() was never called.');
}

$errorWebhookUrl = $this->errorWebhookUrl ?? $this->webhookUrl;
$errorWebhookUrl = $this->errorWebhookUrl ?? $this->successWebhookUrl;

$headers = [
'Gotenberg-Webhook-Url' => $this->webhookUrl,
'Gotenberg-Webhook-Url' => $this->successWebhookUrl,
'Gotenberg-Webhook-Error-Url' => $errorWebhookUrl,
'Gotenberg-Webhook-Extra-Http-Headers' => json_encode($this->webhookExtraHeaders, \JSON_THROW_ON_ERROR),
];

if (null !== $this->successWebhookMethod) {
$headers['Gotenberg-Webhook-Method'] = $this->successWebhookMethod;
}

if (null !== $this->errorWebhookMethod) {
$headers['Gotenberg-Webhook-Error-Method'] = $this->errorWebhookMethod;
}

if ([] !== $this->webhookExtraHeaders) {
$headers['Gotenberg-Webhook-Extra-Http-Headers'] = json_encode($this->webhookExtraHeaders, \JSON_THROW_ON_ERROR);
}

if (null !== $this->fileName) {
// Gotenberg will add the extension to the file name (e.g. filename : "file.pdf" => generated file : "file.pdf.pdf").
$headers['Gotenberg-Output-Filename'] = $this->fileName;
@@ -41,13 +63,18 @@ public function generateAsync(): void
}

/**
* Providing an existing $webhook from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.
* Providing an existing $name from the configuration file, it will correctly set both success and error webhook URLs as well as extra_http_headers if defined.
*/
public function webhookConfiguration(string $webhook): static
public function webhookConfiguration(string $name): static
{
$webhookConfiguration = $this->webhookConfigurationRegistry->get($webhook);
$webhookConfiguration = $this->webhookConfigurationRegistry->get($name);

$result = $this->webhookUrls($webhookConfiguration['success'], $webhookConfiguration['error']);
$result = $this->webhookUrls(
$webhookConfiguration['success']['url'],
$webhookConfiguration['error']['url'],
$webhookConfiguration['success']['method'],
$webhookConfiguration['error']['method'],
);

if (\array_key_exists('extra_http_headers', $webhookConfiguration)) {
$result = $result->webhookExtraHeaders($webhookConfiguration['extra_http_headers']);
@@ -57,16 +84,51 @@ public function webhookConfiguration(string $webhook): static
}

/**
* Allows to set both $successWebhook and $errorWebhook URLs. If $errorWebhook is not provided, it will fallback to $successWebhook one.
* Sets the webhook for cases of success.
* Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.
*
* @param 'POST'|'PATCH'|'PUT'|null $method
*
* @see https://gotenberg.dev/docs/webhook
*/
public function webhookUrls(string $successWebhook, string|null $errorWebhook = null): static
public function webhookUrl(string $url, string|null $method = null): static
{
$this->webhookUrl = $successWebhook;
$this->errorWebhookUrl = $errorWebhook ?? $successWebhook;
$this->successWebhookUrl = $url;
$this->successWebhookMethod = $method;

return $this;
}

/**
* Sets the webhook for cases of error.
* Optionaly sets a custom HTTP method for such endpoint among : POST, PUT or PATCH.
*
* @param 'POST'|'PATCH'|'PUT'|null $method
*
* @see https://gotenberg.dev/docs/webhook
*/
public function errorWebhookUrl(string|null $url = null, string|null $method = null): static
{
$this->errorWebhookUrl = $url;
$this->errorWebhookMethod = $method;

return $this;
}

/**
* Allows to set both $successWebhook and $errorWebhook URLs. If $errorWebhook is not provided, it will fallback to $successWebhook one.
*
* @param 'POST'|'PATCH'|'PUT'|null $successMethod
* @param 'POST'|'PATCH'|'PUT'|null $errorMethod
*/
public function webhookUrls(string $successWebhook, string|null $errorWebhook = null, string|null $successMethod = null, string|null $errorMethod = null): static
{
return $this
->webhookUrl($successWebhook, $successMethod)
->errorWebhookUrl($errorWebhook, $errorMethod)
;
}

/**
* Extra headers that will be provided to the webhook endpoint. May it either be Success or Error.
*
5 changes: 5 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -774,6 +774,11 @@ private function addWebhookConfigurationNode(string $name): NodeDefinition
->thenInvalid('The "route" parameter must be a string or an array containing a string and an array.')
->end()
->end()
->enumNode('method')
->info('HTTP method to use on that endpoint.')
->values(['POST', 'PUT', 'PATCH'])
->defaultNull()
->end()
->end()
;
}
2 changes: 1 addition & 1 deletion src/DependencyInjection/SensiolabsGotenbergExtension.php
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
use Symfony\Component\Routing\RequestContext;

/**
* @phpstan-type WebhookDefinition array{url?: string, route?: array{0: string, 1: array<string, mixed>}}
* @phpstan-type WebhookDefinition array{url?: string, route?: array{0: string, 1: array<string, mixed>}, method?: 'POST'|'PUT'|'PATCH'|null}
*/
class SensiolabsGotenbergExtension extends Extension
{
Loading