Skip to content

Commit 0347b3c

Browse files
authored
Merge pull request #150 from thannaske/main
Add event that is being fired upon the webhook's dispatch
2 parents 5b90694 + 6af707b commit 0347b3c

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ WebhookCall::create()
116116
->dispatch();
117117
```
118118

119-
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.
119+
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`.
120120

121121
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.
122122

@@ -384,8 +384,9 @@ WebhookCall::create()
384384
### Events
385385

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

391392
All these events have these properties:
@@ -396,9 +397,12 @@ All these events have these properties:
396397
- `headers`: the headers that were sent. This array includes the signature header
397398
- `meta`: the array of values passed to the webhook with [the `meta` call](#adding-meta-information)
398399
- `tags`: the array of [tags](#adding-tags) used
400+
- `uuid`: a unique string to identify this call. This uuid will be the same for all attempts of a webhook call.
401+
402+
Except for the `DispatchingWebhookCallEvent`, all events have these additional properties:
403+
399404
- `attempt`: the attempt number
400405
- `response`: the response returned by the remote app. Can be an instance of `\GuzzleHttp\Psr7\Response` or `null`.
401-
- `uuid`: a unique string to identify this call. This uuid will be the same for all attempts of a webhook call.
402406

403407
## Testing
404408

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Spatie\WebhookServer\Events;
4+
5+
class DispatchingWebhookCallEvent
6+
{
7+
public function __construct(
8+
public string $httpVerb,
9+
public string $webhookUrl,
10+
public array|string $payload,
11+
public array $headers,
12+
public array $meta,
13+
public array $tags,
14+
public string $uuid,
15+
) {
16+
}
17+
}

src/WebhookCall.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Foundation\Bus\PendingDispatch;
66
use Illuminate\Support\Str;
77
use Spatie\WebhookServer\BackoffStrategy\BackoffStrategy;
8+
use Spatie\WebhookServer\Events\DispatchingWebhookCallEvent;
89
use Spatie\WebhookServer\Exceptions\CouldNotCallWebhook;
910
use Spatie\WebhookServer\Exceptions\InvalidBackoffStrategy;
1011
use Spatie\WebhookServer\Exceptions\InvalidSigner;
@@ -229,6 +230,16 @@ public function dispatch(): PendingDispatch
229230
{
230231
$this->prepareForDispatch();
231232

233+
event(new DispatchingWebhookCallEvent(
234+
$this->callWebhookJob->httpVerb,
235+
$this->callWebhookJob->webhookUrl,
236+
$this->callWebhookJob->payload,
237+
$this->callWebhookJob->headers,
238+
$this->callWebhookJob->meta,
239+
$this->callWebhookJob->tags,
240+
$this->callWebhookJob->uuid,
241+
));
242+
232243
return dispatch($this->callWebhookJob);
233244
}
234245

tests/WebhookTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

3+
use Illuminate\Support\Facades\Event;
34
use Illuminate\Support\Facades\Queue;
5+
use Spatie\WebhookServer\Events\DispatchingWebhookCallEvent;
46
use function PHPUnit\Framework\assertTrue;
57
use Spatie\WebhookServer\CallWebhookJob;
68
use Spatie\WebhookServer\Exceptions\CouldNotCallWebhook;
@@ -155,3 +157,13 @@
155157

156158
Queue::assertPushed(CallWebhookJob::class);
157159
});
160+
161+
it('will fire an event right before the webhook is initially dispatched', function () {
162+
Event::fake();
163+
164+
$url = 'https://localhost';
165+
166+
WebhookCall::create()->url($url)->useSecret('123')->dispatchIf(true);
167+
168+
Event::assertDispatched(DispatchingWebhookCallEvent::class, 1);
169+
});

0 commit comments

Comments
 (0)