hidden = function () { return []; }; $this->method = $method; $this->uri = $uri; $this->data = $data; $this->headers = $headers; $this->client = new Client([ RequestOptions::HEADERS => [ 'User-Agent' => sprintf( 'LDC CRM Client v%s', Utils::getPackageInfo()['version'] ) ] ]); } public function __get($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 ($this->method->equals(HttpMethods::get())) { $uri = sprintf("%s?%s", $uri, http_build_query($this->data)); } $headers = array_merge($this->headers, ($this->hidden)('headers')); $data = array_merge($this->data, ($this->hidden)('data')); return new Psr7Request( \strtoupper($this->method), sprintf('%s/%s', $this->endpoint, $uri), $headers, $this->method->equals(HttpMethods::get()) ? null : \json_encode($data) ); }; } /** * 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, [ 'http_errors' => false // do not throw http errors ]); } /** * 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); } /** * Set hidden header and data * * @param array $array * @return void */ public function setHidden(array $array) { $this->hidden = function ($headersOrArray) use ($array) { if ( $headersOrArray == 'headers' and isset($array['headers']) and is_array($array['headers']) ) { return $array['headers']; } if ( $headersOrArray == 'data' and isset($array['data']) and is_array($array['data']) ) { return $array['data']; } return []; }; } /** * Get attempted calls * * @return integer */ public function getAttempts(): int { return $this->attemps; } /** * Does max attempts have been reached * * @return integer */ public function maxAttempted(): bool { return $this->attemps >= $this->maxAttemps; } /** * Increment the attempt to call * * @return integer */ public function incrementAttempt(): int { return $this->attemps = $this->attemps + 1; } }