X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/917598f7c857a07e8a07f92d71e56e2a214142e2..refs/pull/3118/head:/app/Actions/DispatchWebhookJob.php diff --git a/app/Actions/DispatchWebhookJob.php b/app/Actions/DispatchWebhookJob.php index 4cc749af3..ece6b6f08 100644 --- a/app/Actions/DispatchWebhookJob.php +++ b/app/Actions/DispatchWebhookJob.php @@ -6,20 +6,21 @@ use BookStack\Auth\User; use BookStack\Entities\Models\Entity; use BookStack\Interfaces\Loggable; use BookStack\Model; -use GuzzleHttp\Client; -use GuzzleHttp\Psr7\Request; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; -use Psr\Http\Client\ClientExceptionInterface; class DispatchWebhookJob implements ShouldQueue { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + use Dispatchable; + use InteractsWithQueue; + use Queueable; + use SerializesModels; /** * @var Webhook @@ -67,22 +68,13 @@ class DispatchWebhookJob implements ShouldQueue */ public function handle() { - $httpClient = new Client([ - 'timeout' => 3, - 'allow_redirects' => ['strict' => true], - ]); - - $request = new Request('POST', $this->webhook->endpoint, [ - 'Content-Type' => 'application/json' - ], json_encode($this->buildWebhookData())); - - try { - $response = $httpClient->send($request); - if ($response->getStatusCode() >= 400) { - Log::error("Webhook call to endpoint {$this->webhook->endpoint} failed with status {$response->getStatusCode()}"); - } - } catch (ClientExceptionInterface $exception) { - Log::error("Received error during webhook call to endpoint {$this->webhook->endpoint}: {$exception->getMessage()}"); + $response = Http::asJson() + ->withOptions(['allow_redirects' => ['strict' => true]]) + ->timeout(3) + ->post($this->webhook->endpoint, $this->buildWebhookData()); + + if ($response->failed()) { + Log::error("Webhook call to endpoint {$this->webhook->endpoint} failed with status {$response->status()}"); } } @@ -97,14 +89,14 @@ class DispatchWebhookJob implements ShouldQueue $textParts[] = '"' . $this->detail->name . '"'; } - $data = [ - 'event' => $this->event, - 'text' => implode(' ', $textParts), - 'triggered_at' => Carbon::createFromTimestampUTC($this->initiatedTime)->toISOString(), - 'triggered_by' => $this->initiator->attributesToArray(), + $data = [ + 'event' => $this->event, + 'text' => implode(' ', $textParts), + 'triggered_at' => Carbon::createFromTimestampUTC($this->initiatedTime)->toISOString(), + 'triggered_by' => $this->initiator->attributesToArray(), 'triggered_by_profile_url' => $this->initiator->getProfileUrl(), - 'webhook_id' => $this->webhook->id, - 'webhook_name' => $this->webhook->name, + 'webhook_id' => $this->webhook->id, + 'webhook_name' => $this->webhook->name, ]; if (method_exists($this->detail, 'getUrl')) {