From a99e570f79fdcb49e5f155fd103583133ff1f3ba Mon Sep 17 00:00:00 2001 From: Kohei Matsumoto Date: Mon, 7 Mar 2022 15:35:30 +0900 Subject: [PATCH] feat: Add http request options --- src/WebhookChannel.php | 15 +++++++++------ src/WebhookMessage.php | 22 ++++++++++++++++++++++ tests/MessageTest.php | 7 +++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/WebhookChannel.php b/src/WebhookChannel.php index 359c031..77ddb24 100644 --- a/src/WebhookChannel.php +++ b/src/WebhookChannel.php @@ -38,12 +38,15 @@ public function send($notifiable, Notification $notification) $webhookData = $notification->toWebhook($notifiable)->toArray(); - $response = $this->client->post($url, [ - 'query' => Arr::get($webhookData, 'query'), - 'body' => json_encode(Arr::get($webhookData, 'data')), - 'verify' => Arr::get($webhookData, 'verify'), - 'headers' => Arr::get($webhookData, 'headers'), - ]); + $response = $this->client->post($url, array_merge( + [ + 'query' => Arr::get($webhookData, 'query'), + 'body' => json_encode(Arr::get($webhookData, 'data')), + 'verify' => Arr::get($webhookData, 'verify'), + 'headers' => Arr::get($webhookData, 'headers'), + ], + Arr::get($webhookData, 'http') + )); if ($response->getStatusCode() >= 300 || $response->getStatusCode() < 200) { throw CouldNotSendNotification::serviceRespondedWithAnError($response); diff --git a/src/WebhookMessage.php b/src/WebhookMessage.php index 63c3367..1664486 100644 --- a/src/WebhookMessage.php +++ b/src/WebhookMessage.php @@ -39,6 +39,14 @@ class WebhookMessage */ protected $verify = false; + + /** + * Additional request options for the Guzzle HTTP client. + * + * @var array + */ + protected $http = []; + /** * @param mixed $data * @@ -126,6 +134,19 @@ public function verify($value = true) return $this; } + /** + * Set additional request options for the Guzzle HTTP client. + * + * @param array $options + * @return $this + */ + public function http(array $options) + { + $this->http = $options; + + return $this; + } + /** * @return array */ @@ -136,6 +157,7 @@ public function toArray() 'data' => $this->data, 'headers' => $this->headers, 'verify' => $this->verify, + 'http' => $this->http, ]; } } diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 3e5a800..6acabdf 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -67,4 +67,11 @@ public function it_can_verify_the_request() $this->message->verify(); $this->assertEquals(true, Arr::get($this->message->toArray(), 'verify')); } + + /** @test */ + public function it_can_add_request_options() + { + $this->message->http(['timeout' => 3.14]); + $this->assertEquals(3.14, Arr::get($this->message->toArray(), 'http.timeout')); + } }