hidden = function () { return []; }; $this->method = $method; $this->uri = $uri; $this->data = $data; $this->headers = $headers; $this->client = new Client([ RequestOptions::HEADERS => [ 'User-Agent' => sprintf( 'BOGT CAFEL API client version %s', config('cafel.client_version') ) ] ]); } /** * Generic accessor * * @param string $field * @return mixed|null */ public function __get(string $field) { if (\in_array($field, ['method', 'uri', 'data'])) { return $this->{$field}; } return null; } /** * make the guzzle request * * @return void */ private function makeRequest(): void { $this->request = function () { $uri = $this->uri; // * If query merge query and data, set data to empty. if ($this->method->equals(HttpMethods::get())) { // * Merge query in url with Data parse_str(\parse_url($uri, \PHP_URL_QUERY) ?? '', $query); // * Remove query part. $uri = strtok($uri, '?'); // * Append build query $uri = sprintf("%s?%s", $uri, http_build_query(array_merge($query, $this->data))); $this->data = []; } $request = new Psr7Request( \strtoupper($this->method), sprintf('%s/%s', $this->endpoint, $uri), $this->headers ); $request = Psr7Utils::modifyRequest($request, [ 'body' => GuzzleHttpUtils::jsonEncode($this->data) ]); return $request; }; } /** * Set the api endpoint to the request * * @param string $endpoint * @return void */ public function setEndpoint(string $endpoint): void { $this->endpoint = $endpoint; } /** * Make the Http request * * @return ResponseInterface */ public function call(): ResponseInterface { $this->makeRequest(); /** @var \GuzzleHttp\Psr7\Request */ $request = ($this->request)(); return $this->client->send($request, [ // ! Do not throw HTTP Exceptions. 'http_errors' => false, 'allow_redirects' => false ]); } /** * Add headers to the request * * @param array $headers * @return void */ public function addHeaders(array $headers) { $this->headers = \array_merge($this->headers, $headers); } /** * Add data to the request * * @param array $data * @return void */ public function addData(array $data) { $this->data = \array_merge($this->data, $data); } }