diff --git a/README.md b/README.md index a2922c8..375fa12 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ `b2-sdk-php` is a client library for working with Backblaze's B2 storage service. It aims to make using the service as easy as possible by exposing a clear API and taking influence from other SDKs that you may be familiar with. +This package will **cache authorization request** for 1 hour so that you won't receive API Limit from B2. + ### Credits Forked from [RunCloudIO](https://github.com/RunCloudIO/b2-sdk-php), based on the original by [Chris White](https://github.com/cwhite92/b2-sdk-php). diff --git a/composer.json b/composer.json index 795ee73..e055222 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,10 @@ ], "require": { "php": ">=5.6.0", - "guzzlehttp/guzzle": "^6.1" + "guzzlehttp/guzzle": "^6.1", + "illuminate/cache": ">=5.1.0", + "illuminate/container": ">=5.1.0", + "illuminate/filesystem": ">=5.1.0" }, "require-dev": { "phpunit/phpunit": "4.8.*" diff --git a/src/Cache/.gitignore b/src/Cache/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/src/Cache/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/src/Client.php b/src/Client.php index d0a3e51..d1cba81 100644 --- a/src/Client.php +++ b/src/Client.php @@ -2,14 +2,19 @@ namespace ChrisWhite\B2; +use ChrisWhite\B2\Exceptions\CacheException; use ChrisWhite\B2\Exceptions\NotFoundException; use ChrisWhite\B2\Exceptions\ValidationException; use ChrisWhite\B2\Http\Client as HttpClient; +use Illuminate\Cache\CacheManager; +use Illuminate\Container\Container; +use Illuminate\Filesystem\Filesystem; class Client { protected $accountId; protected $applicationKey; + protected $cache; protected $authToken; protected $apiUrl; @@ -45,9 +50,34 @@ public function __construct($accountId, $applicationKey, array $options = []) $this->client = new HttpClient(['exceptions' => false]); } + // Initialize cache + $this->createCacheContainer(); + $this->authorizeAccount(); } + private function createCacheContainer() + { + $container = new Container; + $container['config'] = array( + 'cache.default' => 'file', + 'cache.stores.file' => array( + 'driver' => 'file', + 'path' => __DIR__ . '/Cache', + ), + ); + $container['files'] = new Filesystem; + + try { + $cacheManager = new CacheManager($container); + $this->cache = $cacheManager->store(); + } catch (\Exception $e) { + throw new CacheException( + $e->getMessage() + ); + } + } + /** * Create a bucket with the given name and type. * @@ -452,14 +482,22 @@ public function listFileVersions(array $options) */ protected function authorizeAccount() { - $response = $this->client->request('GET', 'https://api.backblazeb2.com/b2api/v1/b2_authorize_account', [ - 'auth' => [$this->accountId, $this->applicationKey], - ]); + + $client = $this->client; + $accountId = $this->accountId; + $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', array( + 'auth' => [ $accountId, $applicationKey ], + )); + }); $this->authToken = $response['authorizationToken']; $this->apiUrl = $response['apiUrl'] . '/b2api/v1'; $this->downloadUrl = $response['downloadUrl']; $this->recommendedPartSize = $response['recommendedPartSize']; + } /** diff --git a/src/Exceptions/CacheException.php b/src/Exceptions/CacheException.php new file mode 100644 index 0000000..83b4545 --- /dev/null +++ b/src/Exceptions/CacheException.php @@ -0,0 +1,5 @@ +