Skip to content

Commit

Permalink
Webhook response (#28)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Adam Campbell authored and atymic committed Sep 17, 2019
1 parent 8b8edb7 commit ff94cba
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
37 changes: 35 additions & 2 deletions src/Exceptions/CouldNotSendNotification.php
Expand Up @@ -2,10 +2,43 @@

namespace NotificationChannels\Webhook\Exceptions;

use GuzzleHttp\Psr7\Response;

class CouldNotSendNotification extends \Exception
{
public static function serviceRespondedWithAnError($response)
private $response;

/**
* @param Response $response
* @param string $message
* @param int|null $code
*/
public function __construct(Response $response, string $message, int $code = null)
{
$this->response = $response;
$this->message = $message;
$this->code = $code ?? $response->getStatusCode();

parent::__construct($message, $code);
}

/**
* @param Response $response
* @return self
*/
public static function serviceRespondedWithAnError(Response $response)
{
return new self(
$response,
sprintf('Webhook responded with an error: `%s`', $response->getBody()->getContents())
);
}

/**
* @return Response
*/
public function getResponse()
{
return new static('Webhook responded with an error: `'.$response->getBody()->getContents().'`');
return $this->response;
}
}
4 changes: 4 additions & 0 deletions src/WebhookChannel.php
Expand Up @@ -26,6 +26,8 @@ public function __construct(Client $client)
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
*
* @return \GuzzleHttp\Psr7\Response
*
* @throws \NotificationChannels\Webhook\Exceptions\CouldNotSendNotification
*/
public function send($notifiable, Notification $notification)
Expand All @@ -46,5 +48,7 @@ public function send($notifiable, Notification $notification)
if ($response->getStatusCode() >= 300 || $response->getStatusCode() < 200) {
throw CouldNotSendNotification::serviceRespondedWithAnError($response);
}

return $response;
}
}
26 changes: 19 additions & 7 deletions tests/ChannelTest.php
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Notifications\Notification;
use NotificationChannels\Webhook\WebhookChannel;
use NotificationChannels\Webhook\WebhookMessage;
use NotificationChannels\Webhook\Exceptions\CouldNotSendNotification;

class ChannelTest extends TestCase
{
Expand All @@ -19,7 +20,8 @@ public function it_can_send_a_notification()
$client = Mockery::mock(Client::class);
$client->shouldReceive('post')
->once()
->with('https://notifiable-webhook-url.com',
->with(
'https://notifiable-webhook-url.com',
[
'query' => null,
'body' => '{"payload":{"webhook":"data"}}',
Expand All @@ -28,7 +30,8 @@ public function it_can_send_a_notification()
'User-Agent' => 'WebhookAgent',
'X-Custom' => 'CustomHeader',
],
])
]
)
->andReturn($response);
$channel = new WebhookChannel($client);
$channel->send(new TestNotifiable(), new TestNotification());
Expand All @@ -41,7 +44,8 @@ public function it_can_send_a_notification_with_2xx_status()
$client = Mockery::mock(Client::class);
$client->shouldReceive('post')
->once()
->with('https://notifiable-webhook-url.com',
->with(
'https://notifiable-webhook-url.com',
[
'query' => null,
'body' => '{"payload":{"webhook":"data"}}',
Expand All @@ -50,7 +54,8 @@ public function it_can_send_a_notification_with_2xx_status()
'User-Agent' => 'WebhookAgent',
'X-Custom' => 'CustomHeader',
],
])
]
)
->andReturn($response);
$channel = new WebhookChannel($client);
$channel->send(new TestNotifiable(), new TestNotification());
Expand All @@ -63,7 +68,8 @@ public function it_can_send_a_notification_with_query_string()
$client = Mockery::mock(Client::class);
$client->shouldReceive('post')
->once()
->with('https://notifiable-webhook-url.com',
->with(
'https://notifiable-webhook-url.com',
[
'query' => [
'webhook' => 'data',
Expand All @@ -74,7 +80,8 @@ public function it_can_send_a_notification_with_query_string()
'User-Agent' => 'WebhookAgent',
'X-Custom' => 'CustomHeader',
],
])
]
)
->andReturn($response);

$channel = new WebhookChannel($client);
Expand All @@ -88,6 +95,11 @@ public function it_can_send_a_notification_with_query_string()
public function it_throws_an_exception_when_it_could_not_send_the_notification()
{
$response = new Response(500);

$this->expectExceptionObject(
new CouldNotSendNotification($response, 'Webhook responded with an error: ``', 500)
);

$client = Mockery::mock(Client::class);
$client->shouldReceive('post')
->once()
Expand Down Expand Up @@ -122,7 +134,7 @@ public function toWebhook($notifiable)
],
]
))->userAgent('WebhookAgent')
->header('X-Custom', 'CustomHeader');
->header('X-Custom', 'CustomHeader');
}
}

Expand Down

0 comments on commit ff94cba

Please sign in to comment.