Skip to content

Commit

Permalink
update for guzzle v7 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
AmmarRazaman committed Sep 7, 2023
1 parent a2a1b7e commit 21c550b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 41 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": "^6.1",
"guzzlehttp/guzzle": "^6.5|^7.2",
"illuminate/cache": ">=5.1.0",
"illuminate/container": ">=5.1.0",
"illuminate/filesystem": ">=5.1.0"
Expand Down
75 changes: 44 additions & 31 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Cache\CacheManager;
use Illuminate\Container\Container;
use Illuminate\Filesystem\Filesystem;
use Psr\Http\Message\ResponseInterface;

class Client
{
Expand Down Expand Up @@ -93,7 +94,7 @@ public function createBucket(array $options)
);
}

$response = $this->client->request('POST', $this->apiUrl . '/b2_create_bucket', [
$response = $this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_create_bucket', [
'headers' => [
'Authorization' => $this->authToken,
],
Expand All @@ -102,7 +103,7 @@ public function createBucket(array $options)
'bucketName' => $options['BucketName'],
'bucketType' => $options['BucketType'],
],
]);
]));

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

$response = $this->client->request('POST', $this->apiUrl . '/b2_update_bucket', [
$response = $this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_update_bucket', [
'headers' => [
'Authorization' => $this->authToken,
],
Expand All @@ -135,7 +136,7 @@ public function updateBucket(array $options)
'bucketId' => $options['BucketId'],
'bucketType' => $options['BucketType'],
],
]);
]));

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

$response = $this->client->request('POST', $this->apiUrl . '/b2_list_buckets', [
$response = $this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_list_buckets', [
'headers' => [
'Authorization' => $this->authToken,
],
'json' => [
'accountId' => $this->accountId,
],
]);
]));

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

$this->client->request('POST', $this->apiUrl . '/b2_delete_bucket', [
$this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_delete_bucket', [
'headers' => [
'Authorization' => $this->authToken,
],
'json' => [
'accountId' => $this->accountId,
'bucketId' => $options['BucketId'],
],
]);
]));

return true;
}
Expand Down Expand Up @@ -253,9 +254,9 @@ public function download(array $options)

if (isset($options['stream'])) {
$requestOptions['stream'] = $options['stream'];
$response = $this->client->request('GET', $requestUrl, $requestOptions, false, false);
$response = $this->getGuzzleContent($this->client->request('GET', $requestUrl, $requestOptions, false, false));
} else {
$response = $this->client->request('GET', $requestUrl, $requestOptions, false);
$response = $this->getGuzzleContent($this->client->request('GET', $requestUrl, $requestOptions, false));
}

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

// B2 returns, at most, 1000 files per "page". Loop through the pages and compile an array of File objects.
while (true) {
$response = $this->client->request('POST', $this->apiUrl . '/b2_list_file_names', [
$response = $this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_list_file_names', [
'headers' => [
'Authorization' => $this->authToken,
],
Expand All @@ -307,7 +308,7 @@ public function listFiles(array $options)
'startFileName' => $nextFileName,
'maxFileCount' => $maxFileCount,
],
]);
]));

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

$response = $this->client->request('POST', $this->apiUrl . '/b2_get_file_info', [
$response = $this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_get_file_info', [
'headers' => [
'Authorization' => $this->authToken,
],
'json' => [
'fileId' => $options['FileId'],
],
]);
]));

return new File(
$response['fileId'],
Expand Down Expand Up @@ -399,15 +400,15 @@ public function deleteFile(array $options)
$options['FileId'] = $file->getId();
}

$this->client->request('POST', $this->apiUrl . '/b2_delete_file_version', [
$this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_delete_file_version', [
'headers' => [
'Authorization' => $this->authToken,
],
'json' => [
'fileName' => $options['FileName'],
'fileId' => $options['FileId'],
],
]);
]));

return true;
}
Expand All @@ -424,9 +425,9 @@ protected function authorizeAccount()
$applicationKey = $this->applicationKey;

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

$this->authToken = $response['authorizationToken'];
Expand Down Expand Up @@ -556,19 +557,19 @@ protected function getPartOfFile($data, $offset, $partSize)
protected function uploadStandardFile($options = array())
{
// Retrieve the URL that we should be uploading to.
$response = $this->client->request('POST', $this->apiUrl . '/b2_get_upload_url', [
$response = $this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_get_upload_url', [
'headers' => [
'Authorization' => $this->authToken,
],
'json' => [
'bucketId' => $options['BucketId'],
],
]);
]));

$uploadEndpoint = $response['uploadUrl'];
$uploadAuthToken = $response['authorizationToken'];

$response = $this->client->request('POST', $uploadEndpoint, [
$response = $this->getGuzzleContent($this->client->request('POST', $uploadEndpoint, [
'headers' => [
'Authorization' => $uploadAuthToken,
'Content-Type' => $options['FileContentType'],
Expand All @@ -578,7 +579,7 @@ protected function uploadStandardFile($options = array())
'X-Bz-Info-src_last_modified_millis' => $options['FileLastModified'],
],
'body' => $options['Body'],
]);
]));

return new File(
$response['fileId'],
Expand All @@ -599,7 +600,7 @@ protected function uploadStandardFile($options = array())
protected function uploadLargeFile($options)
{
// Prepare for uploading the parts of a large file.
$response = $this->client->request('POST', $this->apiUrl . '/b2_start_large_file', [
$response = $this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_start_large_file', [
'headers' => [
'Authorization' => $this->authToken,
],
Expand All @@ -614,7 +615,7 @@ protected function uploadLargeFile($options)
]
**/
],
]);
]));
$fileId = $response['fileId'];

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

// Retrieve the URL that we should be uploading to.
$response = $this->client->request('POST', $this->apiUrl . '/b2_get_upload_part_url', [
$response = $this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_get_upload_part_url', [
'headers' => [
'Authorization' => $this->authToken,
],
'json' => [
'fileId' => $fileId,
],
]);
]));

$uploadEndpoint = $response['uploadUrl'];
$uploadAuthToken = $response['authorizationToken'];

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

$response = $this->client->request('POST', $uploadEndpoint, [
$response = $this->getGuzzleContent($this->client->request('POST', $uploadEndpoint, [
'headers' => [
'Authorization' => $uploadAuthToken,
'X-Bz-Part-Number' => $i,
'Content-Length' => $size,
'X-Bz-Content-Sha1' => $hash,
],
'body' => $this->getPartOfFile($options['Body'], $bytesSent, $partSize),
]);
]));
}

// Finish upload of large file
$response = $this->client->request('POST', $this->apiUrl . '/b2_finish_large_file', [
$response = $this->getGuzzleContent($this->client->request('POST', $this->apiUrl . '/b2_finish_large_file', [
'headers' => [
'Authorization' => $this->authToken,
],
'json' => [
'fileId' => $fileId,
'partSha1Array' => $hashParts,
],
]);
]));
return new File(
$response['fileId'],
$response['fileName'],
Expand All @@ -671,4 +672,16 @@ protected function uploadLargeFile($options)
$response['fileInfo']
);
}
}

private function getGuzzleContent(ResponseInterface $response, $asJson = true, $wantsGetContents = true) {
if ($asJson) {
return json_decode($response->getBody(), true);
}

if (!$wantsGetContents) {
return $response->getBody();
}

return $response->getBody();
}
}
18 changes: 9 additions & 9 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Client extends GuzzleClient
* @param bool $asJson
* @return mixed|string
*/
public function request($method, $uri = null, array $options = [], $asJson = true, $wantsGetContents = true)
public function request($method, $uri = null, array $options = [], $asJson = false, $wantsGetContents = false): \Psr\Http\Message\ResponseInterface
{
$response = parent::request($method, $uri, $options);

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

if ($asJson) {
return json_decode($response->getBody(), true);
}
// if ($asJson) {
// return json_decode($response->getBody(), true);
// }

if (!$wantsGetContents) {
return $response->getBody();
}
// if (!$wantsGetContents) {
// return $response->getBody();
// }

return $response->getBody();
return $response;
}
}
}

0 comments on commit 21c550b

Please sign in to comment.