Skip to content

Commit ff94cba

Browse files
Adam Campbellatymic
authored andcommitted
Webhook response (#28)
* Return full response from the request rather than throwing an exception * Remove unused import * Update dependencies for 5.6 * Revert dependency changes * Update for 5.7 * Update more requires * Bump for Illuminate 5.8 * Return response on successful webhook * Remove .idea from .gitignore * Remove unused variable * Add response to CouldNotSendNotification exception * Update test PHPdocs and assert exception object * Fix broken test. * Update format for StyleCI
1 parent 8b8edb7 commit ff94cba

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

src/Exceptions/CouldNotSendNotification.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,43 @@
22

33
namespace NotificationChannels\Webhook\Exceptions;
44

5+
use GuzzleHttp\Psr7\Response;
6+
57
class CouldNotSendNotification extends \Exception
68
{
7-
public static function serviceRespondedWithAnError($response)
9+
private $response;
10+
11+
/**
12+
* @param Response $response
13+
* @param string $message
14+
* @param int|null $code
15+
*/
16+
public function __construct(Response $response, string $message, int $code = null)
17+
{
18+
$this->response = $response;
19+
$this->message = $message;
20+
$this->code = $code ?? $response->getStatusCode();
21+
22+
parent::__construct($message, $code);
23+
}
24+
25+
/**
26+
* @param Response $response
27+
* @return self
28+
*/
29+
public static function serviceRespondedWithAnError(Response $response)
30+
{
31+
return new self(
32+
$response,
33+
sprintf('Webhook responded with an error: `%s`', $response->getBody()->getContents())
34+
);
35+
}
36+
37+
/**
38+
* @return Response
39+
*/
40+
public function getResponse()
841
{
9-
return new static('Webhook responded with an error: `'.$response->getBody()->getContents().'`');
42+
return $this->response;
1043
}
1144
}

src/WebhookChannel.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public function __construct(Client $client)
2626
* @param mixed $notifiable
2727
* @param \Illuminate\Notifications\Notification $notification
2828
*
29+
* @return \GuzzleHttp\Psr7\Response
30+
*
2931
* @throws \NotificationChannels\Webhook\Exceptions\CouldNotSendNotification
3032
*/
3133
public function send($notifiable, Notification $notification)
@@ -46,5 +48,7 @@ public function send($notifiable, Notification $notification)
4648
if ($response->getStatusCode() >= 300 || $response->getStatusCode() < 200) {
4749
throw CouldNotSendNotification::serviceRespondedWithAnError($response);
4850
}
51+
52+
return $response;
4953
}
5054
}

tests/ChannelTest.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Notifications\Notification;
1010
use NotificationChannels\Webhook\WebhookChannel;
1111
use NotificationChannels\Webhook\WebhookMessage;
12+
use NotificationChannels\Webhook\Exceptions\CouldNotSendNotification;
1213

1314
class ChannelTest extends TestCase
1415
{
@@ -19,7 +20,8 @@ public function it_can_send_a_notification()
1920
$client = Mockery::mock(Client::class);
2021
$client->shouldReceive('post')
2122
->once()
22-
->with('https://notifiable-webhook-url.com',
23+
->with(
24+
'https://notifiable-webhook-url.com',
2325
[
2426
'query' => null,
2527
'body' => '{"payload":{"webhook":"data"}}',
@@ -28,7 +30,8 @@ public function it_can_send_a_notification()
2830
'User-Agent' => 'WebhookAgent',
2931
'X-Custom' => 'CustomHeader',
3032
],
31-
])
33+
]
34+
)
3235
->andReturn($response);
3336
$channel = new WebhookChannel($client);
3437
$channel->send(new TestNotifiable(), new TestNotification());
@@ -41,7 +44,8 @@ public function it_can_send_a_notification_with_2xx_status()
4144
$client = Mockery::mock(Client::class);
4245
$client->shouldReceive('post')
4346
->once()
44-
->with('https://notifiable-webhook-url.com',
47+
->with(
48+
'https://notifiable-webhook-url.com',
4549
[
4650
'query' => null,
4751
'body' => '{"payload":{"webhook":"data"}}',
@@ -50,7 +54,8 @@ public function it_can_send_a_notification_with_2xx_status()
5054
'User-Agent' => 'WebhookAgent',
5155
'X-Custom' => 'CustomHeader',
5256
],
53-
])
57+
]
58+
)
5459
->andReturn($response);
5560
$channel = new WebhookChannel($client);
5661
$channel->send(new TestNotifiable(), new TestNotification());
@@ -63,7 +68,8 @@ public function it_can_send_a_notification_with_query_string()
6368
$client = Mockery::mock(Client::class);
6469
$client->shouldReceive('post')
6570
->once()
66-
->with('https://notifiable-webhook-url.com',
71+
->with(
72+
'https://notifiable-webhook-url.com',
6773
[
6874
'query' => [
6975
'webhook' => 'data',
@@ -74,7 +80,8 @@ public function it_can_send_a_notification_with_query_string()
7480
'User-Agent' => 'WebhookAgent',
7581
'X-Custom' => 'CustomHeader',
7682
],
77-
])
83+
]
84+
)
7885
->andReturn($response);
7986

8087
$channel = new WebhookChannel($client);
@@ -88,6 +95,11 @@ public function it_can_send_a_notification_with_query_string()
8895
public function it_throws_an_exception_when_it_could_not_send_the_notification()
8996
{
9097
$response = new Response(500);
98+
99+
$this->expectExceptionObject(
100+
new CouldNotSendNotification($response, 'Webhook responded with an error: ``', 500)
101+
);
102+
91103
$client = Mockery::mock(Client::class);
92104
$client->shouldReceive('post')
93105
->once()
@@ -122,7 +134,7 @@ public function toWebhook($notifiable)
122134
],
123135
]
124136
))->userAgent('WebhookAgent')
125-
->header('X-Custom', 'CustomHeader');
137+
->header('X-Custom', 'CustomHeader');
126138
}
127139
}
128140

0 commit comments

Comments
 (0)