Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function routeNotificationForWebhook()
- `userAgent('')`: Accepts a string value for the Webhook user agent.
- `header($name, $value)`: Sets additional headers to send with the POST Webhook.
- `verify()`: Enable the SSL certificate verification or provide the path to a CA bundle
- `method('')`: Set the HTTP method to use for the Webhook request. Defaults to `POST`.

## Changelog

Expand Down
9 changes: 4 additions & 5 deletions src/WebhookChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class WebhookChannel
protected $client;

/**
* @param Client $client
* @param Client $client
*/
public function __construct(Client $client)
{
Expand All @@ -23,9 +23,8 @@ public function __construct(Client $client)
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return \GuzzleHttp\Psr7\Response
*
* @throws \NotificationChannels\Webhook\Exceptions\CouldNotSendNotification
Expand All @@ -38,7 +37,7 @@ public function send($notifiable, Notification $notification)

$webhookData = $notification->toWebhook($notifiable)->toArray();

$response = $this->client->post($url, [
$response = $this->client->request($webhookData['method'], $url, [
'query' => Arr::get($webhookData, 'query'),
'body' => json_encode(Arr::get($webhookData, 'data')),
'verify' => Arr::get($webhookData, 'verify'),
Expand Down
38 changes: 27 additions & 11 deletions src/WebhookMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@ class WebhookMessage
protected $verify = false;

/**
* @param mixed $data
* The Guzzle method option.
*
* @var string
*/
protected $method = 'POST';

/**
* @param mixed $data
* @return static
*/
public static function create($data = '')
Expand All @@ -50,7 +56,7 @@ public static function create($data = '')
}

/**
* @param mixed $data
* @param mixed $data
*/
public function __construct($data = '')
{
Expand All @@ -60,8 +66,7 @@ public function __construct($data = '')
/**
* Set the Webhook parameters to be URL encoded.
*
* @param mixed $query
*
* @param mixed $query
* @return $this
*/
public function query($query)
Expand All @@ -74,8 +79,7 @@ public function query($query)
/**
* Set the Webhook data to be JSON encoded.
*
* @param mixed $data
*
* @param mixed $data
* @return $this
*/
public function data($data)
Expand All @@ -86,11 +90,23 @@ public function data($data)
}

/**
* Add a Webhook request custom header.
* Set the Webhook method.
*
* @param string $name
* @param string $value
* @param mixed $data
* @return $this
*/
public function method($method)
{
$this->method = $method;

return $this;
}

/**
* Add a Webhook request custom header.
*
* @param string $name
* @param string $value
* @return $this
*/
public function header($name, $value)
Expand All @@ -103,8 +119,7 @@ public function header($name, $value)
/**
* Set the Webhook request UserAgent.
*
* @param string $userAgent
*
* @param string $userAgent
* @return $this
*/
public function userAgent($userAgent)
Expand Down Expand Up @@ -136,6 +151,7 @@ public function toArray()
'data' => $this->data,
'headers' => $this->headers,
'verify' => $this->verify,
'method' => $this->method,
];
}
}
11 changes: 7 additions & 4 deletions tests/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ public function it_can_send_a_notification()
{
$response = new Response(200);
$client = Mockery::mock(Client::class);
$client->shouldReceive('post')
$client->shouldReceive('request')
->once()
->with(
'POST',
'https://notifiable-webhook-url.com',
[
'query' => null,
Expand All @@ -42,9 +43,10 @@ public function it_can_send_a_notification_with_2xx_status()
{
$response = new Response(201);
$client = Mockery::mock(Client::class);
$client->shouldReceive('post')
$client->shouldReceive('request')
->once()
->with(
'POST',
'https://notifiable-webhook-url.com',
[
'query' => null,
Expand All @@ -66,9 +68,10 @@ public function it_can_send_a_notification_with_query_string()
{
$response = new Response();
$client = Mockery::mock(Client::class);
$client->shouldReceive('post')
$client->shouldReceive('request')
->once()
->with(
'POST',
'https://notifiable-webhook-url.com',
[
'query' => [
Expand Down Expand Up @@ -100,7 +103,7 @@ public function it_throws_an_exception_when_it_could_not_send_the_notification()
$this->expectExceptionCode(500);

$client = Mockery::mock(Client::class);
$client->shouldReceive('post')
$client->shouldReceive('request')
->once()
->andReturn($response);
$channel = new WebhookChannel($client);
Expand Down
8 changes: 8 additions & 0 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,12 @@ public function it_can_verify_the_request()
$this->message->verify();
$this->assertEquals(true, Arr::get($this->message->toArray(), 'verify'));
}

/** @test */
public function it_can_set_the_method_to_get()
{
$webhookMessage = new WebhookMessage();
$webhookMessage->method('GET');
$this->assertEquals('GET', $webhookMessage->toArray()['method']);
}
}