Client.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Consul;
  12. use Closure;
  13. use GuzzleHttp\ClientInterface;
  14. use GuzzleHttp\Exception\TransferException;
  15. use Hyperf\Consul\Exception\ClientException;
  16. use Hyperf\Consul\Exception\ServerException;
  17. use Psr\Log\LoggerInterface;
  18. use Psr\Log\NullLogger;
  19. abstract class Client
  20. {
  21. public const DEFAULT_URI = 'http://127.0.0.1:8500';
  22. /**
  23. * Will execute this closure everytime when the consul client send an HTTP request,
  24. * and the closure should return a GuzzleHttp\ClientInterface instance.
  25. * $clientFactory(array $options).
  26. *
  27. * @var Closure
  28. */
  29. private $clientFactory;
  30. /**
  31. * @var LoggerInterface
  32. */
  33. private $logger;
  34. public function __construct(Closure $clientFactory, ?LoggerInterface $logger = null)
  35. {
  36. $this->clientFactory = $clientFactory;
  37. $this->logger = $logger ?: new NullLogger();
  38. }
  39. protected function resolveOptions(array $options, array $availableOptions): array
  40. {
  41. // Add key of ACL token to $availableOptions
  42. $availableOptions[] = 'token';
  43. return array_intersect_key($options, array_flip($availableOptions));
  44. }
  45. /**
  46. * Send a HTTP request.
  47. */
  48. protected function request(string $method, string $url, array $options = []): ConsulResponse
  49. {
  50. $this->logger->debug(sprintf('Consul Request [%s] %s', strtoupper($method), $url));
  51. try {
  52. // Create an HTTP Client by $clientFactory closure.
  53. $clientFactory = $this->clientFactory;
  54. $client = $clientFactory($options);
  55. if (! $client instanceof ClientInterface) {
  56. throw new ClientException(sprintf('The client factory should create a %s instance.', ClientInterface::class));
  57. }
  58. $response = $client->request($method, $url, $options);
  59. } catch (TransferException $e) {
  60. $message = sprintf('Something went wrong when calling consul (%s).', $e->getMessage());
  61. $this->logger->error($message);
  62. throw new ServerException($e->getMessage(), (int) $e->getCode(), $e);
  63. }
  64. if ($response->getStatusCode() >= 400) {
  65. $message = sprintf('Something went wrong when calling consul (%s - %s).', $response->getStatusCode(), $response->getReasonPhrase());
  66. $this->logger->error($message);
  67. $message .= PHP_EOL . $response->getBody();
  68. if ($response->getStatusCode() >= 500) {
  69. throw new ServerException($message, $response->getStatusCode());
  70. }
  71. throw new ClientException($message, $response->getStatusCode());
  72. }
  73. return new ConsulResponse($response);
  74. }
  75. }