Skip to content

Commit

Permalink
Merge pull request #150 from thannaske/main
Browse files Browse the repository at this point in the history
Add event that is being fired upon the webhook's dispatch
  • Loading branch information
freekmurze committed Nov 27, 2023
2 parents 5b90694 + 6af707b commit 0347b3c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ WebhookCall::create()
->dispatch();
```

This will send a post request to `https://other-app.com/webhooks`. The body of the request will be JSON encoded version of the array passed to `payload`. The request will have a header called `Signature` that will contain a signature the receiving app can use [to verify](https://github.com/spatie/laravel-webhook-server#how-signing-requests-works) the payload hasn't been tampered with.
This will send a post request to `https://other-app.com/webhooks`. The body of the request will be JSON encoded version of the array passed to `payload`. The request will have a header called `Signature` that will contain a signature the receiving app can use [to verify](https://github.com/spatie/laravel-webhook-server#how-signing-requests-works) the payload hasn't been tampered with. Dispatching a webhook call will also fire a `DispatchingWebhookCallEvent`.

If the receiving app doesn't respond with a response code starting with `2`, the package will retry calling the webhook after 10 seconds. If that second attempt fails, the package will attempt to call the webhook a final time after 100 seconds. Should that attempt fail, the `FinalWebhookCallFailedEvent` will be raised.

Expand Down Expand Up @@ -384,8 +384,9 @@ WebhookCall::create()
### Events

The package fires these events:
- `DispatchingWebhookCallEvent`: right before the webhook call will be dispatched to the queue.
- `WebhookCallSucceededEvent`: the remote app responded with a `2xx` response code.
- `WebhookCallFailedEvent`: the remote app responded with a non `2xx` response code, or it did not respond at all
- `WebhookCallFailedEvent`: the remote app responded with a non `2xx` response code, or it did not respond at all.
- `FinalWebhookCallFailedEvent`: the final attempt to call the webhook failed.

All these events have these properties:
Expand All @@ -396,9 +397,12 @@ All these events have these properties:
- `headers`: the headers that were sent. This array includes the signature header
- `meta`: the array of values passed to the webhook with [the `meta` call](#adding-meta-information)
- `tags`: the array of [tags](#adding-tags) used
- `uuid`: a unique string to identify this call. This uuid will be the same for all attempts of a webhook call.

Except for the `DispatchingWebhookCallEvent`, all events have these additional properties:

- `attempt`: the attempt number
- `response`: the response returned by the remote app. Can be an instance of `\GuzzleHttp\Psr7\Response` or `null`.
- `uuid`: a unique string to identify this call. This uuid will be the same for all attempts of a webhook call.

## Testing

Expand Down
17 changes: 17 additions & 0 deletions src/Events/DispatchingWebhookCallEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Spatie\WebhookServer\Events;

class DispatchingWebhookCallEvent
{
public function __construct(
public string $httpVerb,
public string $webhookUrl,
public array|string $payload,
public array $headers,
public array $meta,
public array $tags,
public string $uuid,
) {
}
}
11 changes: 11 additions & 0 deletions src/WebhookCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Foundation\Bus\PendingDispatch;
use Illuminate\Support\Str;
use Spatie\WebhookServer\BackoffStrategy\BackoffStrategy;
use Spatie\WebhookServer\Events\DispatchingWebhookCallEvent;
use Spatie\WebhookServer\Exceptions\CouldNotCallWebhook;
use Spatie\WebhookServer\Exceptions\InvalidBackoffStrategy;
use Spatie\WebhookServer\Exceptions\InvalidSigner;
Expand Down Expand Up @@ -229,6 +230,16 @@ public function dispatch(): PendingDispatch
{
$this->prepareForDispatch();

event(new DispatchingWebhookCallEvent(
$this->callWebhookJob->httpVerb,
$this->callWebhookJob->webhookUrl,
$this->callWebhookJob->payload,
$this->callWebhookJob->headers,
$this->callWebhookJob->meta,
$this->callWebhookJob->tags,
$this->callWebhookJob->uuid,
));

return dispatch($this->callWebhookJob);
}

Expand Down
12 changes: 12 additions & 0 deletions tests/WebhookTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Queue;
use Spatie\WebhookServer\Events\DispatchingWebhookCallEvent;
use function PHPUnit\Framework\assertTrue;
use Spatie\WebhookServer\CallWebhookJob;
use Spatie\WebhookServer\Exceptions\CouldNotCallWebhook;
Expand Down Expand Up @@ -155,3 +157,13 @@

Queue::assertPushed(CallWebhookJob::class);
});

it('will fire an event right before the webhook is initially dispatched', function () {
Event::fake();

$url = 'https://localhost';

WebhookCall::create()->url($url)->useSecret('123')->dispatchIf(true);

Event::assertDispatched(DispatchingWebhookCallEvent::class, 1);
});

0 comments on commit 0347b3c

Please sign in to comment.