/** * Execute the job. * * @return void */ public function handle() { $config = app('Illuminate\\Contracts\\Config\\Repository'); $client = app(Client::class); $logging = $config->get('captain_hook.log.active') && $config->get('queue.driver') != 'sync'; foreach ($this->webhooks as $webhook) { if ($logging) { if ($config->get('captain_hook.log.storage_quantity') != -1 && $webhook->logs()->count() >= $config->get('captain_hook.log.storage_quantity')) { $webhook->logs()->orderBy('updated_at', 'desc')->first()->delete(); } $log = new WebhookLog(['webhook_id' => $webhook['id'], 'url' => $webhook['url']]); $middleware = Middleware::tap(function (RequestInterface $request, $options) use($log) { $log->payload_format = isset($request->getHeader('Content-Type')[0]) ? $request->getHeader('Content-Type')[0] : null; $log->payload = $request->getBody()->getContents(); }, function ($request, $options, Promise $response) use($log) { $response->then(function (ResponseInterface $response) use($log) { $log->status = $response->getStatusCode(); $log->response = $response->getBody()->getContents(); $log->response_format = $log->payload_format = isset($response->getHeader('Content-Type')[0]) ? $response->getHeader('Content-Type')[0] : null; $log->save(); }); }); $client->post($webhook['url'], ['body' => $this->eventData, 'handler' => $middleware($client->getConfig('handler'))]); } else { $client->postAsync($webhook['url'], ['body' => $this->eventData, 'verify' => false, 'future' => true]); } } }
/** * Execute the job. * * @return void */ public function handle() { $config = app('Illuminate\\Contracts\\Config\\Repository'); $client = app(Client::class); if ($config->get('captain_hook.log.max_attempts', -1) !== -1 && $this->attempts() > $config->get('captain_hook.log.max_attempts')) { return; } $logging = $config->get('captain_hook.log.active'); $transformer = $this->resolveCallable($config->get('captain_hook.transformer'), 'transform'); $responseCallback = $this->resolveCallable($config->get('captain_hook.response_callback'), 'handle'); foreach ($this->webhooks as $webhook) { if ($logging) { if ($config->get('captain_hook.log.storage_quantity') != -1 && $webhook->logs()->count() >= $config->get('captain_hook.log.storage_quantity')) { $webhook->logs()->orderBy('updated_at', 'asc')->first()->delete(); } $log = new WebhookLog(['webhook_id' => $webhook['id'], 'url' => $webhook['url']]); $middleware = Middleware::tap(function (RequestInterface $request, $options) use($log) { $log->payload_format = isset($request->getHeader('Content-Type')[0]) ? $request->getHeader('Content-Type')[0] : null; $log->payload = $request->getBody()->getContents(); }, function ($request, $options, PromiseInterface $response) use($log, $webhook, $responseCallback) { $response->then(function (ResponseInterface $response) use($log, $webhook, $responseCallback) { $log->status = $response->getStatusCode(); $log->response = $response->getBody()->getContents(); $log->response_format = $log->payload_format = isset($response->getHeader('Content-Type')[0]) ? $response->getHeader('Content-Type')[0] : null; $log->save(); // Retry this job if the webhook response didn't give us a HTTP 200 OK if ($response->getStatusCode() >= 300 || $response->getStatusCode() < 200) { $this->release(30); } $responseCallback($webhook, $response); }); }); $client->post($webhook['url'], ['exceptions' => false, 'body' => $transformer($this->eventData, $webhook), 'verify' => false, 'handler' => $middleware($client->getConfig('handler'))]); } else { $client->post($webhook['url'], ['exceptions' => false, 'body' => $transformer($this->eventData, $webhook), 'verify' => false, 'timeout' => 10]); } } }