diff --git a/README.md b/README.md index 3fd40a3..5a3d580 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/WebhookChannel.php b/src/WebhookChannel.php index 3785ccd..869b8c1 100644 --- a/src/WebhookChannel.php +++ b/src/WebhookChannel.php @@ -13,7 +13,7 @@ class WebhookChannel protected $client; /** - * @param Client $client + * @param Client $client */ public function __construct(Client $client) { @@ -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 @@ -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'), diff --git a/src/WebhookMessage.php b/src/WebhookMessage.php index 63c3367..6b1e390 100644 --- a/src/WebhookMessage.php +++ b/src/WebhookMessage.php @@ -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 = '') @@ -50,7 +56,7 @@ public static function create($data = '') } /** - * @param mixed $data + * @param mixed $data */ public function __construct($data = '') { @@ -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) @@ -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) @@ -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) @@ -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) @@ -136,6 +151,7 @@ public function toArray() 'data' => $this->data, 'headers' => $this->headers, 'verify' => $this->verify, + 'method' => $this->method, ]; } } diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index 6e57427..50bd3ee 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -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, @@ -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, @@ -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' => [ @@ -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); diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 3e5a800..ad5897f 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -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']); + } }