Skip to content

Commit 5e9aaa7

Browse files
committed
test caching authorize request
1 parent 94ec181 commit 5e9aaa7

5 files changed

Lines changed: 53 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
/vendor
2-
composer.lock
2+
composer.lock

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
],
2020
"require": {
2121
"php": ">=5.5.0",
22-
"guzzlehttp/guzzle": "^6.1"
22+
"guzzlehttp/guzzle": "^6.1",
23+
"illuminate/cache": ">=5.1.0",
24+
"illuminate/container": ">=5.1.0",
25+
"illuminate/filesystem": ">=5.1.0"
2326
},
2427
"require-dev": {
2528
"phpunit/phpunit": "4.8.*"

src/Cache/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

src/Client.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22

33
namespace ChrisWhite\B2;
44

5+
use ChrisWhite\B2\Exceptions\CacheException;
56
use ChrisWhite\B2\Exceptions\NotFoundException;
67
use ChrisWhite\B2\Exceptions\ValidationException;
78
use ChrisWhite\B2\Http\Client as HttpClient;
9+
use Illuminate\Cache\CacheManager;
10+
use Illuminate\Container\Container;
11+
use Illuminate\Filesystem\Filesystem;
812

913
class Client
1014
{
1115
protected $accountId;
1216
protected $applicationKey;
17+
protected $cache;
1318

1419
protected $authToken;
1520
protected $apiUrl;
@@ -45,9 +50,34 @@ public function __construct($accountId, $applicationKey, array $options = [])
4550
$this->client = new HttpClient(['exceptions' => false]);
4651
}
4752

53+
// initialize cache
54+
$this->createCacheContainer();
55+
4856
$this->authorizeAccount();
4957
}
5058

59+
private function createCacheContainer()
60+
{
61+
$container = new Container;
62+
$container['config'] = [
63+
'cache.default' => 'file',
64+
'cache.stores.file' => [
65+
'driver' => 'file',
66+
'path' => __DIR__ . '/Cache',
67+
],
68+
];
69+
$container['files'] = new Filesystem;
70+
71+
try {
72+
$cacheManager = new CacheManager($container);
73+
$this->cache = $cacheManager->store();
74+
} catch (\Exception $e) {
75+
throw new CacheException(
76+
$e->getMessage()
77+
);
78+
}
79+
}
80+
5181
/**
5282
* Create a bucket with the given name and type.
5383
*
@@ -378,9 +408,15 @@ public function deleteFile(array $options)
378408
*/
379409
protected function authorizeAccount()
380410
{
381-
$response = $this->client->request('GET', 'https://api.backblazeb2.com/b2api/v1/b2_authorize_account', [
382-
'auth' => [$this->accountId, $this->applicationKey],
383-
]);
411+
$client = $this->client;
412+
$accountId = $this->accountId;
413+
$applicationKey = $this->applicationKey;
414+
415+
$response = $this->cache->remember('RunCloud-B2-SDK-Authorization', 60, function () use ($client, $accountId, $applicationKey) {
416+
return $client->request('GET', 'https://api.backblazeb2.com/b2api/v1/b2_authorize_account', [
417+
'auth' => [$accountId, $applicationKey],
418+
]);
419+
});
384420

385421
$this->authToken = $response['authorizationToken'];
386422
$this->apiUrl = $response['apiUrl'] . '/b2api/v1';

src/Exceptions/CacheException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace ChrisWhite\B2\Exceptions;
4+
5+
class CacheException extends \Exception
6+
{
7+
}

0 commit comments

Comments
 (0)