Skip to content

Commit 21c550b

Browse files
committed
update for guzzle v7 compatibility
1 parent a2a1b7e commit 21c550b

3 files changed

Lines changed: 54 additions & 41 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
],
2020
"require": {
2121
"php": ">=5.5.0",
22-
"guzzlehttp/guzzle": "^6.1",
22+
"guzzlehttp/guzzle": "^6.5|^7.2",
2323
"illuminate/cache": ">=5.1.0",
2424
"illuminate/container": ">=5.1.0",
2525
"illuminate/filesystem": ">=5.1.0"

src/Client.php

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Cache\CacheManager;
1010
use Illuminate\Container\Container;
1111
use Illuminate\Filesystem\Filesystem;
12+
use Psr\Http\Message\ResponseInterface;
1213

1314
class Client
1415
{
@@ -93,7 +94,7 @@ public function createBucket(array $options)
9394
);
9495
}
9596

96-
$response = $this->client->request('POST', $this->apiUrl . '/b2_create_bucket', [
97+
$response = $this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_create_bucket', [
9798
'headers' => [
9899
'Authorization' => $this->authToken,
99100
],
@@ -102,7 +103,7 @@ public function createBucket(array $options)
102103
'bucketName' => $options['BucketName'],
103104
'bucketType' => $options['BucketType'],
104105
],
105-
]);
106+
]));
106107

107108
return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType']);
108109
}
@@ -126,7 +127,7 @@ public function updateBucket(array $options)
126127
$options['BucketId'] = $this->getBucketIdFromName($options['BucketName']);
127128
}
128129

129-
$response = $this->client->request('POST', $this->apiUrl . '/b2_update_bucket', [
130+
$response = $this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_update_bucket', [
130131
'headers' => [
131132
'Authorization' => $this->authToken,
132133
],
@@ -135,7 +136,7 @@ public function updateBucket(array $options)
135136
'bucketId' => $options['BucketId'],
136137
'bucketType' => $options['BucketType'],
137138
],
138-
]);
139+
]));
139140

140141
return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType']);
141142
}
@@ -149,14 +150,14 @@ public function listBuckets()
149150
{
150151
$buckets = [];
151152

152-
$response = $this->client->request('POST', $this->apiUrl . '/b2_list_buckets', [
153+
$response = $this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_list_buckets', [
153154
'headers' => [
154155
'Authorization' => $this->authToken,
155156
],
156157
'json' => [
157158
'accountId' => $this->accountId,
158159
],
159-
]);
160+
]));
160161

161162
foreach ($response['buckets'] as $bucket) {
162163
$buckets[] = new Bucket($bucket['bucketId'], $bucket['bucketName'], $bucket['bucketType']);
@@ -177,15 +178,15 @@ public function deleteBucket(array $options)
177178
$options['BucketId'] = $this->getBucketIdFromName($options['BucketName']);
178179
}
179180

180-
$this->client->request('POST', $this->apiUrl . '/b2_delete_bucket', [
181+
$this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_delete_bucket', [
181182
'headers' => [
182183
'Authorization' => $this->authToken,
183184
],
184185
'json' => [
185186
'accountId' => $this->accountId,
186187
'bucketId' => $options['BucketId'],
187188
],
188-
]);
189+
]));
189190

190191
return true;
191192
}
@@ -253,9 +254,9 @@ public function download(array $options)
253254

254255
if (isset($options['stream'])) {
255256
$requestOptions['stream'] = $options['stream'];
256-
$response = $this->client->request('GET', $requestUrl, $requestOptions, false, false);
257+
$response = $this->getGuzzleContent($this->client->request('GET', $requestUrl, $requestOptions, false, false));
257258
} else {
258-
$response = $this->client->request('GET', $requestUrl, $requestOptions, false);
259+
$response = $this->getGuzzleContent($this->client->request('GET', $requestUrl, $requestOptions, false));
259260
}
260261

261262
return isset($options['SaveAs']) ? true : $response;
@@ -298,7 +299,7 @@ public function listFiles(array $options)
298299

299300
// B2 returns, at most, 1000 files per "page". Loop through the pages and compile an array of File objects.
300301
while (true) {
301-
$response = $this->client->request('POST', $this->apiUrl . '/b2_list_file_names', [
302+
$response = $this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_list_file_names', [
302303
'headers' => [
303304
'Authorization' => $this->authToken,
304305
],
@@ -307,7 +308,7 @@ public function listFiles(array $options)
307308
'startFileName' => $nextFileName,
308309
'maxFileCount' => $maxFileCount,
309310
],
310-
]);
311+
]));
311312

312313
foreach ($response['files'] as $file) {
313314
// if we have a file name set, only retrieve information if the file name matches
@@ -357,14 +358,14 @@ public function getFile(array $options)
357358
}
358359
}
359360

360-
$response = $this->client->request('POST', $this->apiUrl . '/b2_get_file_info', [
361+
$response = $this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_get_file_info', [
361362
'headers' => [
362363
'Authorization' => $this->authToken,
363364
],
364365
'json' => [
365366
'fileId' => $options['FileId'],
366367
],
367-
]);
368+
]));
368369

369370
return new File(
370371
$response['fileId'],
@@ -399,15 +400,15 @@ public function deleteFile(array $options)
399400
$options['FileId'] = $file->getId();
400401
}
401402

402-
$this->client->request('POST', $this->apiUrl . '/b2_delete_file_version', [
403+
$this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_delete_file_version', [
403404
'headers' => [
404405
'Authorization' => $this->authToken,
405406
],
406407
'json' => [
407408
'fileName' => $options['FileName'],
408409
'fileId' => $options['FileId'],
409410
],
410-
]);
411+
]));
411412

412413
return true;
413414
}
@@ -424,9 +425,9 @@ protected function authorizeAccount()
424425
$applicationKey = $this->applicationKey;
425426

426427
$response = $this->cache->remember('RunCloud-B2-SDK-Authorization', 60, function () use ($client, $accountId, $applicationKey) {
427-
return $client->request('GET', 'https://api.backblazeb2.com/b2api/v1/b2_authorize_account', [
428+
return $this->getGuzzleContent($client->request('GET', 'https://api.backblazeb2.com/b2api/v1/b2_authorize_account', [
428429
'auth' => [$accountId, $applicationKey],
429-
]);
430+
]));
430431
});
431432

432433
$this->authToken = $response['authorizationToken'];
@@ -556,19 +557,19 @@ protected function getPartOfFile($data, $offset, $partSize)
556557
protected function uploadStandardFile($options = array())
557558
{
558559
// Retrieve the URL that we should be uploading to.
559-
$response = $this->client->request('POST', $this->apiUrl . '/b2_get_upload_url', [
560+
$response = $this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_get_upload_url', [
560561
'headers' => [
561562
'Authorization' => $this->authToken,
562563
],
563564
'json' => [
564565
'bucketId' => $options['BucketId'],
565566
],
566-
]);
567+
]));
567568

568569
$uploadEndpoint = $response['uploadUrl'];
569570
$uploadAuthToken = $response['authorizationToken'];
570571

571-
$response = $this->client->request('POST', $uploadEndpoint, [
572+
$response = $this->getGuzzleContent($this->client->request('POST', $uploadEndpoint, [
572573
'headers' => [
573574
'Authorization' => $uploadAuthToken,
574575
'Content-Type' => $options['FileContentType'],
@@ -578,7 +579,7 @@ protected function uploadStandardFile($options = array())
578579
'X-Bz-Info-src_last_modified_millis' => $options['FileLastModified'],
579580
],
580581
'body' => $options['Body'],
581-
]);
582+
]));
582583

583584
return new File(
584585
$response['fileId'],
@@ -599,7 +600,7 @@ protected function uploadStandardFile($options = array())
599600
protected function uploadLargeFile($options)
600601
{
601602
// Prepare for uploading the parts of a large file.
602-
$response = $this->client->request('POST', $this->apiUrl . '/b2_start_large_file', [
603+
$response = $this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_start_large_file', [
603604
'headers' => [
604605
'Authorization' => $this->authToken,
605606
],
@@ -614,7 +615,7 @@ protected function uploadLargeFile($options)
614615
]
615616
**/
616617
],
617-
]);
618+
]));
618619
$fileId = $response['fileId'];
619620

620621
$partsCount = ceil($options['size'] / $this->recommendedPartSize);
@@ -626,42 +627,42 @@ protected function uploadLargeFile($options)
626627
$partSize = ($bytesLeft > $this->recommendedPartSize) ? $this->recommendedPartSize : $bytesLeft;
627628

628629
// Retrieve the URL that we should be uploading to.
629-
$response = $this->client->request('POST', $this->apiUrl . '/b2_get_upload_part_url', [
630+
$response = $this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_get_upload_part_url', [
630631
'headers' => [
631632
'Authorization' => $this->authToken,
632633
],
633634
'json' => [
634635
'fileId' => $fileId,
635636
],
636-
]);
637+
]));
637638

638639
$uploadEndpoint = $response['uploadUrl'];
639640
$uploadAuthToken = $response['authorizationToken'];
640641

641642
list($hash, $size) = $this->getFileHashAndSize($options['Body'], $bytesSent, $partSize);
642643
$hashParts[] = $hash;
643644

644-
$response = $this->client->request('POST', $uploadEndpoint, [
645+
$response = $this->getGuzzleContent($this->client->request('POST', $uploadEndpoint, [
645646
'headers' => [
646647
'Authorization' => $uploadAuthToken,
647648
'X-Bz-Part-Number' => $i,
648649
'Content-Length' => $size,
649650
'X-Bz-Content-Sha1' => $hash,
650651
],
651652
'body' => $this->getPartOfFile($options['Body'], $bytesSent, $partSize),
652-
]);
653+
]));
653654
}
654655

655656
// Finish upload of large file
656-
$response = $this->client->request('POST', $this->apiUrl . '/b2_finish_large_file', [
657+
$response = $this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_finish_large_file', [
657658
'headers' => [
658659
'Authorization' => $this->authToken,
659660
],
660661
'json' => [
661662
'fileId' => $fileId,
662663
'partSha1Array' => $hashParts,
663664
],
664-
]);
665+
]));
665666
return new File(
666667
$response['fileId'],
667668
$response['fileName'],
@@ -671,4 +672,16 @@ protected function uploadLargeFile($options)
671672
$response['fileInfo']
672673
);
673674
}
674-
}
675+
676+
private function getGuzzleContent(ResponseInterface $response, $asJson = true, $wantsGetContents = true) {
677+
if ($asJson) {
678+
return json_decode($response->getBody(), true);
679+
}
680+
681+
if (!$wantsGetContents) {
682+
return $response->getBody();
683+
}
684+
685+
return $response->getBody();
686+
}
687+
}

src/Http/Client.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Client extends GuzzleClient
2424
* @param bool $asJson
2525
* @return mixed|string
2626
*/
27-
public function request($method, $uri = null, array $options = [], $asJson = true, $wantsGetContents = true)
27+
public function request($method, $uri = null, array $options = [], $asJson = false, $wantsGetContents = false): \Psr\Http\Message\ResponseInterface
2828
{
2929
$response = parent::request($method, $uri, $options);
3030

@@ -42,14 +42,14 @@ public function request($method, $uri = null, array $options = [], $asJson = tru
4242
ErrorHandler::handleErrorResponse($response);
4343
}
4444

45-
if ($asJson) {
46-
return json_decode($response->getBody(), true);
47-
}
45+
// if ($asJson) {
46+
// return json_decode($response->getBody(), true);
47+
// }
4848

49-
if (!$wantsGetContents) {
50-
return $response->getBody();
51-
}
49+
// if (!$wantsGetContents) {
50+
// return $response->getBody();
51+
// }
5252

53-
return $response->getBody();
53+
return $response;
5454
}
55-
}
55+
}

0 commit comments

Comments
 (0)