/** * Handles the actual webhook, happens in two steps: * * - Retrieves the $webhook_id and $event_id * * - Uses the key and secret provided to fetch the event object and casts it to an array * * - Calls the callable function with the Event object */ private function handleWebhook() { $identifier = $this->field('identifier'); if ($identifier !== $this->identifier) { return; } $event_id = $this->field('event_id'); $webhook_id = $this->field('webhook_id'); if (!$event_id || !$webhook_id) { Log::info($this->patrol, 'Webhook ' . $this->identifier . ' has no event_id or webhook_id'); return; } $httpClient = new HttpClient($this->patrol, 'GET', 'webhooks/' . $webhook_id . '/events/' . $event_id); $response = $httpClient->response(); if (!$response) { Log::info($this->patrol, 'Retrieving webhook from ' . $httpClient->getUrl() . ' failed'); return; } $data = $this->field($response, 'data'); if (!$data) { Log::info($this->patrol, 'Event has invalid format to be processed: ' . print_r($response, true)); } $callable = $this->callback; if (is_callable($callable)) { $callable($data); } }
/** * Perform a POST request based on this model * * @param string $url * @param array $data * @param string $class The class type where the result should be casted to * * @return PatrolSdk\Model */ protected function _post($url, $data = [], $class = null) { $client = new HttpClient($this->patrol, 'post', $url, $data); $data = $client->response(); if (isset($data['error'])) { $this->_error($data); } if (!isset($data['data'])) { return $data; } $data = $data['data']; $callee = $class ? $class : get_called_class(); return Util::parseResponseToPatrolObject($this->patrol, $data, $callee, $this->defaults); }