WebflowApiClient.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Http;
  3. use Symfony\Component\HttpClient\HttpClient;
  4. use Symfony\Contracts\HttpClient\HttpClientInterface;
  5. use Symfony\Contracts\HttpClient\ResponseInterface;
  6. class WebflowApiClient implements WebflowApiClientInterface {
  7. private $client;
  8. private $url;
  9. private $options = [];
  10. public function __construct(string $token, string $api_url, string $version = '1.0.0') {
  11. $this->url = $api_url;
  12. $this->options = [
  13. 'auth_bearer' => $token,
  14. 'headers' => [
  15. 'accept-version' => $version,
  16. ],
  17. ];
  18. $this->client = HttpClient::createForBaseUri(
  19. sprintf('https://%s', $api_url),
  20. $this->options
  21. );
  22. }
  23. public function scopeFromBase(string $scope): string {
  24. return sprintf('https://%s/%s', $this->url, ltrim($scope, '/'));
  25. }
  26. public function get(string $scope): ResponseInterface {
  27. return $this->client->request('GET',
  28. $this->scopeFromBase($scope),
  29. );
  30. }
  31. public function getClient(): HttpClientInterface {
  32. return $this->client;
  33. }
  34. }
  35. ?>