4
4
5
5
use GuzzleHttp \Client as Guzzle ;
6
6
use GuzzleHttp \Exception \GuzzleException ;
7
+ use GuzzleHttp \Psr7 \Request ;
7
8
use PlatformCommunity \Flysystem \BunnyCDN \Exceptions \BunnyCDNException ;
8
9
use PlatformCommunity \Flysystem \BunnyCDN \Exceptions \NotFoundException ;
10
+ use Psr \Http \Client \ClientExceptionInterface ;
9
11
10
12
class BunnyCDNClient
11
13
{
12
- public string $ storage_zone_name ;
13
-
14
- private string $ api_key ;
15
-
16
- private string $ region ;
17
-
18
- public Guzzle $ client ;
19
-
20
- public function __construct (string $ storage_zone_name , string $ api_key , string $ region = BunnyCDNRegion::FALKENSTEIN )
21
- {
22
- $ this ->storage_zone_name = $ storage_zone_name ;
23
- $ this ->api_key = $ api_key ;
24
- $ this ->region = $ region ;
25
-
26
- $ this ->client = new Guzzle ();
14
+ public Guzzle $ guzzleClient ;
15
+
16
+ public function __construct (
17
+ public string $ storage_zone_name ,
18
+ private string $ api_key ,
19
+ private string $ region = BunnyCDNRegion::FALKENSTEIN
20
+ ) {
21
+ $ this ->guzzleClient = new Guzzle ();
27
22
}
28
23
29
24
private static function get_base_url ($ region ): string
@@ -41,23 +36,25 @@ private static function get_base_url($region): string
41
36
};
42
37
}
43
38
44
- /**
45
- * @throws GuzzleException
46
- */
47
- private function request (string $ path , string $ method = 'GET ' , array $ options = []): mixed
39
+ public function createRequest (string $ path , string $ method = 'GET ' , array $ headers = [], $ body = null ): Request
48
40
{
49
- $ response = $ this -> client -> request (
41
+ return new Request (
50
42
$ method ,
51
43
self ::get_base_url ($ this ->region ).Util::normalizePath ('/ ' .$ this ->storage_zone_name .'/ ' ).$ path ,
52
- array_merge_recursive ([
53
- 'headers ' => [
54
- 'Accept ' => '*/* ' ,
55
- 'AccessKey ' => $ this ->api_key , // Honestly... Why do I have to specify this twice... @BunnyCDN
56
- ],
57
- ], $ options )
44
+ array_merge ([
45
+ 'Accept ' => '*/* ' ,
46
+ 'AccessKey ' => $ this ->api_key ,
47
+ ], $ headers ),
48
+ $ body
58
49
);
50
+ }
59
51
60
- $ contents = $ response ->getBody ()->getContents ();
52
+ /**
53
+ * @throws ClientExceptionInterface
54
+ */
55
+ private function request (Request $ request , array $ options = []): mixed
56
+ {
57
+ $ contents = $ this ->guzzleClient ->send ($ request , $ options )->getBody ()->getContents ();
61
58
62
59
return json_decode ($ contents , true ) ?? $ contents ;
63
60
}
@@ -71,7 +68,7 @@ private function request(string $path, string $method = 'GET', array $options =
71
68
public function list (string $ path ): array
72
69
{
73
70
try {
74
- $ listing = $ this ->request (Util::normalizePath ($ path ).'/ ' );
71
+ $ listing = $ this ->request ($ this -> createRequest ( Util::normalizePath ($ path ).'/ ' ) );
75
72
76
73
// Throw an exception if we don't get back an array
77
74
if (! is_array ($ listing )) {
@@ -101,7 +98,7 @@ public function list(string $path): array
101
98
public function download (string $ path ): string
102
99
{
103
100
try {
104
- $ content = $ this ->request ($ path .'?download ' );
101
+ $ content = $ this ->request ($ this -> createRequest ( $ path .'?download ' ) );
105
102
106
103
if (\is_array ($ content )) {
107
104
return \json_encode ($ content );
@@ -128,17 +125,7 @@ public function download(string $path): string
128
125
public function stream (string $ path )
129
126
{
130
127
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 ();
128
+ return $ this ->guzzleClient ->send ($ this ->createRequest ($ path ), ['stream ' => true ])->getBody ()->detach ();
142
129
// @codeCoverageIgnoreStart
143
130
} catch (GuzzleException $ e ) {
144
131
throw match ($ e ->getCode ()) {
@@ -149,6 +136,18 @@ public function stream(string $path)
149
136
// @codeCoverageIgnoreEnd
150
137
}
151
138
139
+ public function getUploadRequest (string $ path , $ contents ): Request
140
+ {
141
+ return $ this ->createRequest (
142
+ $ path ,
143
+ 'PUT ' ,
144
+ [
145
+ 'Content-Type ' => 'application/x-www-form-urlencoded; charset=UTF-8 ' ,
146
+ ],
147
+ $ contents
148
+ );
149
+ }
150
+
152
151
/**
153
152
* @param string $path
154
153
* @param $contents
@@ -159,17 +158,10 @@ public function stream(string $path)
159
158
public function upload (string $ path , $ contents ): mixed
160
159
{
161
160
try {
162
- return $ this ->request ($ path , 'PUT ' , [
163
- 'headers ' => [
164
- 'Content-Type ' => 'application/x-www-form-urlencoded; charset=UTF-8 ' ,
165
- ],
166
- 'body ' => $ contents ,
167
- ]);
161
+ return $ this ->request ($ this ->getUploadRequest ($ path , $ contents ));
168
162
// @codeCoverageIgnoreStart
169
163
} catch (GuzzleException $ e ) {
170
- throw match ($ e ->getCode ()) {
171
- default => new BunnyCDNException ($ e ->getMessage ())
172
- };
164
+ throw new BunnyCDNException ($ e ->getMessage ());
173
165
}
174
166
// @codeCoverageIgnoreEnd
175
167
}
@@ -183,11 +175,9 @@ public function upload(string $path, $contents): mixed
183
175
public function make_directory (string $ path ): mixed
184
176
{
185
177
try {
186
- return $ this ->request (Util::normalizePath ($ path ).'/ ' , 'PUT ' , [
187
- 'headers ' => [
188
- 'Content-Length ' => 0 ,
189
- ],
190
- ]);
178
+ return $ this ->request ($ this ->createRequest (Util::normalizePath ($ path ).'/ ' , 'PUT ' , [
179
+ 'Content-Length ' => 0 ,
180
+ ]));
191
181
// @codeCoverageIgnoreStart
192
182
} catch (GuzzleException $ e ) {
193
183
throw match ($ e ->getCode ()) {
@@ -208,7 +198,7 @@ public function make_directory(string $path): mixed
208
198
public function delete (string $ path ): mixed
209
199
{
210
200
try {
211
- return $ this ->request ($ path , 'DELETE ' );
201
+ return $ this ->request ($ this -> createRequest ( $ path , 'DELETE ' ) );
212
202
// @codeCoverageIgnoreStart
213
203
} catch (GuzzleException $ e ) {
214
204
throw match ($ e ->getCode ()) {
0 commit comments