Skip to content

Commit 2cb8fed

Browse files
committed
feat: add dedicated function to create a guzzle request
1 parent daf2434 commit 2cb8fed

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

src/BunnyCDNClient.php

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use GuzzleHttp\Client as Guzzle;
66
use GuzzleHttp\Exception\GuzzleException;
7+
use GuzzleHttp\Psr7\Request;
78
use PlatformCommunity\Flysystem\BunnyCDN\Exceptions\BunnyCDNException;
89
use PlatformCommunity\Flysystem\BunnyCDN\Exceptions\NotFoundException;
10+
use Psr\Http\Client\ClientExceptionInterface;
911

1012
class BunnyCDNClient
1113
{
@@ -41,13 +43,9 @@ private static function get_base_url($region): string
4143
};
4244
}
4345

44-
/**
45-
* @throws GuzzleException
46-
*/
47-
private function request(string $path, string $method = 'GET', array $options = []): mixed
46+
public function createRequest(string $path, string $method = 'GET', array $options = []): Request
4847
{
49-
$response = $this->client->request(
50-
$method,
48+
return new Request($method,
5149
self::get_base_url($this->region).Util::normalizePath('/'.$this->storage_zone_name.'/').$path,
5250
array_merge_recursive([
5351
'headers' => [
@@ -56,8 +54,16 @@ private function request(string $path, string $method = 'GET', array $options =
5654
],
5755
], $options)
5856
);
57+
}
58+
59+
/**
60+
* @throws ClientExceptionInterface
61+
*/
62+
private function request(string $path, string $method = 'GET', array $options = []): mixed
63+
{
64+
$request = $this->createRequest($path, $method, $options);
5965

60-
$contents = $response->getBody()->getContents();
66+
$contents = $this->client->sendRequest($request)->getBody()->getContents();
6167

6268
return json_decode($contents, true) ?? $contents;
6369
}
@@ -128,17 +134,7 @@ public function download(string $path): string
128134
public function stream(string $path)
129135
{
130136
try {
131-
return $this->client->request(
132-
'GET',
133-
self::get_base_url($this->region).Util::normalizePath('/'.$this->storage_zone_name.'/').$path,
134-
array_merge_recursive([
135-
'stream' => true,
136-
'headers' => [
137-
'Accept' => '*/*',
138-
'AccessKey' => $this->api_key, // Honestly... Why do I have to specify this twice... @BunnyCDN
139-
],
140-
])
141-
)->getBody()->detach();
137+
return $this->createRequest($path, 'GET', ['stream' => true])->getBody()->detach();
142138
// @codeCoverageIgnoreStart
143139
} catch (GuzzleException $e) {
144140
throw match ($e->getCode()) {
@@ -167,9 +163,7 @@ public function upload(string $path, $contents): mixed
167163
]);
168164
// @codeCoverageIgnoreStart
169165
} catch (GuzzleException $e) {
170-
throw match ($e->getCode()) {
171-
default => new BunnyCDNException($e->getMessage())
172-
};
166+
throw new BunnyCDNException($e->getMessage());
173167
}
174168
// @codeCoverageIgnoreEnd
175169
}

0 commit comments

Comments
 (0)