HttpClient.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\Testing;
  12. use GuzzleHttp\Client;
  13. use Hyperf\Codec\Packer\JsonPacker;
  14. use Hyperf\Collection\Arr;
  15. use Hyperf\Contract\PackerInterface;
  16. use Hyperf\Coroutine\Coroutine;
  17. use Hyperf\Guzzle\CoroutineHandler;
  18. use Psr\Container\ContainerInterface;
  19. use Psr\Http\Message\UriInterface;
  20. class HttpClient
  21. {
  22. protected Client $client;
  23. protected PackerInterface $packer;
  24. public function __construct(protected ContainerInterface $container, ?PackerInterface $packer = null, $baseUri = 'http://127.0.0.1:9501')
  25. {
  26. $this->packer = $packer ?? new JsonPacker();
  27. $handler = null;
  28. if (Coroutine::inCoroutine()) {
  29. $handler = new CoroutineHandler();
  30. }
  31. $this->client = new Client([
  32. 'base_uri' => $baseUri,
  33. 'timeout' => 2,
  34. 'handler' => $handler,
  35. ]);
  36. }
  37. public function get(string|UriInterface $uri, array $data = [], array $headers = [])
  38. {
  39. $response = $this->client->get($uri, [
  40. 'headers' => $headers,
  41. 'query' => $data,
  42. ]);
  43. return $this->packer->unpack((string) $response->getBody());
  44. }
  45. public function post(string|UriInterface $uri, array $data = [], array $headers = [])
  46. {
  47. $response = $this->client->post($uri, [
  48. 'headers' => $headers,
  49. 'form_params' => $data,
  50. ]);
  51. return $this->packer->unpack((string) $response->getBody());
  52. }
  53. public function put(string|UriInterface $uri, array $data = [], array $headers = [])
  54. {
  55. $response = $this->client->put($uri, [
  56. 'headers' => $headers,
  57. 'form_params' => $data,
  58. ]);
  59. return $this->packer->unpack((string) $response->getBody());
  60. }
  61. public function patch(string|UriInterface $uri, array $data = [], array $headers = [])
  62. {
  63. $response = $this->client->patch($uri, [
  64. 'headers' => $headers,
  65. 'form_params' => $data,
  66. ]);
  67. return $this->packer->unpack((string) $response->getBody());
  68. }
  69. public function json(string|UriInterface $uri, array $data = [], array $headers = [])
  70. {
  71. $headers['Content-Type'] = 'application/json';
  72. $response = $this->client->post($uri, [
  73. 'json' => $data,
  74. 'headers' => $headers,
  75. ]);
  76. return $this->packer->unpack((string) $response->getBody());
  77. }
  78. public function file(string|UriInterface $uri, array $data = [], array $headers = [])
  79. {
  80. $multipart = [];
  81. if (Arr::isAssoc($data)) {
  82. $data = [$data];
  83. }
  84. foreach ($data as $item) {
  85. $name = $item['name'];
  86. $file = $item['file'];
  87. $multipart[] = [
  88. 'name' => $name,
  89. 'contents' => fopen($file, 'r'),
  90. 'filename' => basename($file),
  91. ];
  92. }
  93. $response = $this->client->post($uri, [
  94. 'headers' => $headers,
  95. 'multipart' => $multipart,
  96. ]);
  97. return $this->packer->unpack((string) $response->getBody());
  98. }
  99. public function client(): Client
  100. {
  101. return $this->client;
  102. }
  103. }