packer = $packer ?? new JsonPacker(); $handler = null; if (Coroutine::inCoroutine()) { $handler = new CoroutineHandler(); } $this->client = new Client([ 'base_uri' => $baseUri, 'timeout' => 2, 'handler' => $handler, ]); } public function get(string|UriInterface $uri, array $data = [], array $headers = []) { $response = $this->client->get($uri, [ 'headers' => $headers, 'query' => $data, ]); return $this->packer->unpack((string) $response->getBody()); } public function post(string|UriInterface $uri, array $data = [], array $headers = []) { $response = $this->client->post($uri, [ 'headers' => $headers, 'form_params' => $data, ]); return $this->packer->unpack((string) $response->getBody()); } public function put(string|UriInterface $uri, array $data = [], array $headers = []) { $response = $this->client->put($uri, [ 'headers' => $headers, 'form_params' => $data, ]); return $this->packer->unpack((string) $response->getBody()); } public function patch(string|UriInterface $uri, array $data = [], array $headers = []) { $response = $this->client->patch($uri, [ 'headers' => $headers, 'form_params' => $data, ]); return $this->packer->unpack((string) $response->getBody()); } public function json(string|UriInterface $uri, array $data = [], array $headers = []) { $headers['Content-Type'] = 'application/json'; $response = $this->client->post($uri, [ 'json' => $data, 'headers' => $headers, ]); return $this->packer->unpack((string) $response->getBody()); } public function file(string|UriInterface $uri, array $data = [], array $headers = []) { $multipart = []; if (Arr::isAssoc($data)) { $data = [$data]; } foreach ($data as $item) { $name = $item['name']; $file = $item['file']; $multipart[] = [ 'name' => $name, 'contents' => fopen($file, 'r'), 'filename' => basename($file), ]; } $response = $this->client->post($uri, [ 'headers' => $headers, 'multipart' => $multipart, ]); return $this->packer->unpack((string) $response->getBody()); } public function client(): Client { return $this->client; } }