Skip to content

Commit

Permalink
Refactor, improve exception handling, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zaxbux committed Jul 6, 2021
1 parent 47d7752 commit a355cfa
Show file tree
Hide file tree
Showing 54 changed files with 596 additions and 270 deletions.
16 changes: 13 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class Client
{
public const VERSION = '2.0.0';
public const USER_AGENT_PREFIX = 'backblaze-b2-php/';
public const BASE_URI = 'https://api.backblazeb2.com';
public const B2_API_VERSION = '/b2api/v2';
public const BASE_URI = 'https://api.backblazeb2.com/';
public const B2_API_VERSION = 'b2api/v2/';

use FileOperationsTrait;
use BucketOperationsTrait;
Expand Down Expand Up @@ -92,7 +92,7 @@ public function __construct($config)
* @param ClientInterface $client
*/
public function authorizeAccount(): AccountAuthorization {
$response = $this->http->request('GET', Client::BASE_URI . Client::B2_API_VERSION . '/b2_authorize_account', [
$response = $this->http->request('GET', Client::BASE_URI . Client::B2_API_VERSION . 'b2_authorize_account', [
'headers' => [
'Authorization' => Utils::basicAuthorization($this->config->applicationKeyId(), $this->config->applicationKey()),
],
Expand Down Expand Up @@ -151,6 +151,16 @@ protected function createDefaultHttpClient(): ClientInterface {
return $client;
}

public function getAllowedBucketId(): ?string
{
return $this->accountAuthorization->getAllowed('bucketId') ?? null;
}

public function getAllowedBucketName(): ?string
{
return $this->accountAuthorization->getAllowed('bucketName') ?? null;
}

/**
* @see __construct()
*/
Expand Down
7 changes: 2 additions & 5 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

namespace Zaxbux\BackblazeB2;

use Exception;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\HandlerStack;
use Zaxbux\BackblazeB2\Classes\BuiltinAuthorizationCache;
use Zaxbux\BackblazeB2\Http\ClientFactory;
use Zaxbux\BackblazeB2\Interfaces\AuthorizationCacheInterface;
use Zaxbux\BackblazeB2\Object\AccountAuthorization;

/**
* The main configuration object for the client.
Expand Down Expand Up @@ -66,7 +62,7 @@ class Config
/**
* Maximum amount of time, in seconds, to wait before retrying a failed request.
* Ignored for HTTP status codes: 429, 503.
* Should be a power of 3.
* Should be a power of 2.
*
* @var int
*/
Expand Down Expand Up @@ -184,6 +180,7 @@ public static function fromArray($data): Config

private function setOptions(array $options) {
$this->handler = $options['handler'] ?? null;
$this->maxRetries = $options['maxRetries'] ?? 4;
$this->authorizationCache = $options['authorizationCache'] ?? new BuiltinAuthorizationCache();
}
}
21 changes: 0 additions & 21 deletions src/Exceptions/B2APIException.php

This file was deleted.

5 changes: 0 additions & 5 deletions src/Exceptions/BadBucketIdException.php

This file was deleted.

5 changes: 0 additions & 5 deletions src/Exceptions/BucketNotEmptyException.php

This file was deleted.

5 changes: 0 additions & 5 deletions src/Exceptions/HttpException.php

This file was deleted.

4 changes: 3 additions & 1 deletion src/Exceptions/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

namespace Zaxbux\BackblazeB2\Exceptions;

class NotFoundException extends B2APIException {}
class NotFoundException extends \Exception {

}
5 changes: 0 additions & 5 deletions src/Exceptions/RangeNotSatisfiableException.php

This file was deleted.

5 changes: 5 additions & 0 deletions src/Exceptions/Request/AccessDeniedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions\Request;

class AccessDeniedException extends B2APIException {}
44 changes: 44 additions & 0 deletions src/Exceptions/Request/B2APIException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions\Request;

use GuzzleHttp\Exception\RequestException;
use JsonException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Throwable;

class B2APIException extends RequestException {

private $statusCode;

public function __construct(
RequestInterface $request,
ResponseInterface $response,
?Throwable $previous = null
) {
$message = $response->getReasonPhrase();
$code = $response->getStatusCode();

try {
$responseJson = json_decode((string) $response->getBody(), true, 512, JSON_THROW_ON_ERROR);

$this->statusCode = $responseJson['status'] ?? null;
$message = $responseJson['message'] ?? null;
$code = $responseJson['code'] ?? null;
} catch (JsonException $e) {
// Ignore JSON exceptions, response object is available instead.
}

parent::__construct($message, $request, $response, $previous);
$this->code = $code;
}

/**
* Get the status code as returned by the B2 API.
*/
public function getStatus(): ?string
{
return $this->statusCode;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions;
namespace Zaxbux\BackblazeB2\Exceptions\Request;

class BadAuthTokenException extends B2APIException {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions;
namespace Zaxbux\BackblazeB2\Exceptions\Request;

class BadRequestException extends B2APIException {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions;
namespace Zaxbux\BackblazeB2\Exceptions\Request;

class CapExceededException extends B2APIException {}
5 changes: 5 additions & 0 deletions src/Exceptions/Request/ConflictException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions\Request;

class ConflictException extends B2APIException {}
5 changes: 5 additions & 0 deletions src/Exceptions/Request/DownloadCapExceededException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions\Request;

class DownloadCapExceededException extends CapExceededException {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions;
namespace Zaxbux\BackblazeB2\Exceptions\Request;

class DuplicateBucketNameException extends B2APIException {}
5 changes: 5 additions & 0 deletions src/Exceptions/Request/ExpiredAuthTokenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions\Request;

class ExpiredAuthTokenException extends B2APIException {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions;
namespace Zaxbux\BackblazeB2\Exceptions\Request;

class FileNotPresentException extends B2APIException {}
5 changes: 5 additions & 0 deletions src/Exceptions/Request/InvalidBucketIdException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions\Request;

class InvalidBucketIdException extends B2APIException {}
5 changes: 5 additions & 0 deletions src/Exceptions/Request/InvalidFileIdException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions\Request;

class InvalidFileIdException extends B2APIException {}
5 changes: 5 additions & 0 deletions src/Exceptions/Request/MethodNotAllowedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions\Request;

class MethodNotAllowedException extends B2APIException {}
5 changes: 5 additions & 0 deletions src/Exceptions/Request/NotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions\Request;

class NotFoundException extends B2APIException {}
5 changes: 5 additions & 0 deletions src/Exceptions/Request/OutOfRangeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions\Request;

class OutOfRangeException extends B2APIException {}
5 changes: 5 additions & 0 deletions src/Exceptions/Request/RangeNotSatisfiableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions\Request;

class RangeNotSatisfiableException extends B2APIException {}
5 changes: 5 additions & 0 deletions src/Exceptions/Request/RequestTimeoutException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions\Request;

class RequestTimeoutException extends B2APIException {}
5 changes: 5 additions & 0 deletions src/Exceptions/Request/ServiceUnavailableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions\Request;

class ServiceUnavailableException extends B2APIException {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions;
namespace Zaxbux\BackblazeB2\Exceptions\Request;

class StorageCapExceededException extends CapExceededException {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions;
namespace Zaxbux\BackblazeB2\Exceptions\Request;

class TooManyBucketsException extends B2APIException {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions;
namespace Zaxbux\BackblazeB2\Exceptions\Request;

class TransactionCapExceededException extends CapExceededException {}
5 changes: 5 additions & 0 deletions src/Exceptions/Request/UnauthorizedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions\Request;

class UnauthorizedException extends B2APIException {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

namespace Zaxbux\BackblazeB2\Exceptions;
namespace Zaxbux\BackblazeB2\Exceptions\Request;

class UnsupportedException extends B2APIException {}
5 changes: 0 additions & 5 deletions src/Exceptions/UnauthorizedException.php

This file was deleted.

5 changes: 0 additions & 5 deletions src/Exceptions/ValidationException.php

This file was deleted.

Loading

0 comments on commit a355cfa

Please sign in to comment.