diff --git a/src/Bucket.php b/src/Bucket.php index 513fed0..6ab609d 100755 --- a/src/Bucket.php +++ b/src/Bucket.php @@ -10,6 +10,8 @@ class Bucket protected $id; protected $name; protected $type; + protected $options; + protected $corsRules; /** * Bucket constructor. @@ -17,12 +19,16 @@ class Bucket * @param $id * @param $name * @param $type + * @param $options + * @param $corsRules */ - public function __construct($id, $name, $type) + public function __construct($id, $name, $type, $options, $corsRules) { $this->id = $id; $this->name = $name; $this->type = $type; + $this->options = $options; + $this->corsRules = $corsRules; } public function getId() @@ -39,4 +45,19 @@ public function getType() { return $this->type; } + + public function getOptions() + { + return $this->options; + } + + public function getCorsRules() + { + return $this->corsRules; + } + + public function isS3Compatible() + { + return in_array('s3', $this->getOptions()); + } } diff --git a/src/Client.php b/src/Client.php index 999d0e4..c229cd9 100755 --- a/src/Client.php +++ b/src/Client.php @@ -75,7 +75,7 @@ public function createBucket(array $options) 'bucketType' => $options['BucketType'], ]); - return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType']); + return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType'], $response['options'], $response['corsRules']); } /** @@ -107,7 +107,7 @@ public function updateBucket(array $options) 'bucketType' => $options['BucketType'], ]); - return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType']); + return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType'], $response['options'], $response['corsRules']); } /** @@ -127,7 +127,7 @@ public function listBuckets() ]); foreach ($response['buckets'] as $bucket) { - $buckets[] = new Bucket($bucket['bucketId'], $bucket['bucketName'], $bucket['bucketType']); + $buckets[] = new Bucket($bucket['bucketId'], $bucket['bucketName'], $bucket['bucketType'], $bucket['options'], $bucket['corsRules']); } return $buckets; @@ -776,6 +776,99 @@ protected function finishLargeFile($fileId, array $sha1s) ); } + /** + * List key pairs. + * + * @param array $options + * + * @throws ValidationException + * @throws GuzzleException If the request fails. + * @throws B2Exception If the B2 server replies with an error. + * + * @return Key + */ + public function listKeys(array $options = []) + { + $request = [ + 'accountId' => $this->accountId, + ]; + + if (array_key_exists('MaxKeyCount', $options)) { + $request['maxKeyCount'] = $options['MaxKeyCount']; + } + + if (array_key_exists('StartApplicationKeyId', $options)) { + $request['startApplicationKeyId'] = $options['StartApplicationKeyId']; + } + + $response = $this->sendAuthorizedRequest('POST', 'b2_list_keys', $request); + + $keys = []; + foreach ($response['keys'] as $key) { + $keys[] = new Key($key['applicationKeyId'], $key['keyName'], null, $key['capabilities'], $key['bucketId'], $key['namePrefix'], $key['expirationTimestamp']); + } + + return $keys; + } + + /** + * Create a key pair for the given bucket and permissions. + * + * @param array $options + * + * @throws ValidationException + * @throws GuzzleException If the request fails. + * @throws B2Exception If the B2 server replies with an error. + * + * @return Key + */ + public function createKey(array $options) + { + $request = [ + 'accountId' => $this->accountId, + 'capabilities' => $options['Capabilities'], + 'keyName' => $options['KeyName'], + ]; + + if (array_key_exists('BucketId', $options)) { + $request['bucketId'] = $options['BucketId']; + } + + if (array_key_exists('NamePrefix', $options)) { + $request['namePrefix'] = $options['NamePrefix']; + } + + if (array_key_exists('ValidDurationInSeconds', $options)) { + $request['validDurationInSeconds'] = $options['ValidDurationInSeconds']; + } + + $response = $this->sendAuthorizedRequest('POST', 'b2_create_key', $request); + + return new Key($response['applicationKeyId'], $response['keyName'], $response['applicationKey'], $response['capabilities'], $response['bucketId'], $response['namePrefix'], $response['expirationTimestamp']); + } + + /** + * Delete a key pair. + * + * @param array $options + * + * @throws ValidationException + * @throws GuzzleException If the request fails. + * @throws B2Exception If the B2 server replies with an error. + * + * @return Key + */ + public function deleteKey(array $options) + { + $request = [ + 'applicationKeyId' => $options['ApplicationKeyId'], + ]; + + $response = $this->sendAuthorizedRequest('POST', 'b2_delete_key', $request); + + return new Key($response['applicationKeyId'], $response['keyName'], null, $response['capabilities'], $response['bucketId'], $response['namePrefix'], $response['expirationTimestamp']); + } + /** * Sends a authorized request to b2 API. * diff --git a/src/Key.php b/src/Key.php new file mode 100644 index 0000000..8769fd1 --- /dev/null +++ b/src/Key.php @@ -0,0 +1,79 @@ +id = $id; + $this->name = $name; + $this->secret = $secret; + $this->capabilities = $capabilities; + $this->bucketId = $bucketId; + $this->namePrefix = $namePrefix; + $this->expirationTimestamp = $expirationTimestamp; + } + + public function getId() + { + return $this->id; + } + + public function getName() + { + return $this->name; + } + + public function getSecret() + { + return $this->secret; + } + + public function getCapabilities() + { + return $this->capabilities; + } + + public function getBucketId() + { + return $this->bucketId; + } + + public function getNamePrefix() + { + return $this->namePrefix; + } + + public function getExpirationTimestamp() + { + return $this->expirationTimestamp; + } +} diff --git a/tests/responses/create_bucket_private.json b/tests/responses/create_bucket_private.json index d66f86e..3e19a02 100755 --- a/tests/responses/create_bucket_private.json +++ b/tests/responses/create_bucket_private.json @@ -2,5 +2,18 @@ "bucketId" : "4a48fe8875c6214145260818", "accountId" : "010203040506", "bucketName" : "Test bucket", - "bucketType" : "allPrivate" -} \ No newline at end of file + "bucketType" : "allPrivate", + + "bucketInfo": [], + "corsRules": [], + "defaultFileLockConfiguration": { + "mode": null, + "period": null, + "status": "disabled" + }, + "lifecycleRules": [], + "options": [ + "s3" + ], + "revision": 2 +} diff --git a/tests/responses/create_bucket_public.json b/tests/responses/create_bucket_public.json index 8134124..a306443 100755 --- a/tests/responses/create_bucket_public.json +++ b/tests/responses/create_bucket_public.json @@ -1,6 +1,19 @@ { - "bucketId" : "4a48fe8875c6214145260818", - "accountId" : "010203040506", - "bucketName" : "Test bucket", - "bucketType" : "allPublic" -} \ No newline at end of file + "bucketId": "4a48fe8875c6214145260818", + "accountId": "010203040506", + "bucketName": "Test bucket", + "bucketType": "allPublic", + + "bucketInfo": [], + "corsRules": [], + "defaultFileLockConfiguration": { + "mode": null, + "period": null, + "status": "disabled" + }, + "lifecycleRules": [], + "options": [ + "s3" + ], + "revision": 2 +} diff --git a/tests/responses/list_buckets_3.json b/tests/responses/list_buckets_3.json index 7b6c4fe..0cb933d 100755 --- a/tests/responses/list_buckets_3.json +++ b/tests/responses/list_buckets_3.json @@ -4,19 +4,58 @@ "bucketId": "4a48fe8875c6214145260818", "accountId": "30f20426f0b1", "bucketName" : "Kitten Videos", - "bucketType": "allPrivate" + "bucketType": "allPrivate", + + "bucketInfo": [], + "corsRules": [], + "defaultFileLockConfiguration": { + "mode": null, + "period": null, + "status": "disabled" + }, + "lifecycleRules": [], + "options": [ + "s3" + ], + "revision": 2 }, { "bucketId" : "5b232e8875c6214145260818", "accountId": "30f20426f0b1", "bucketName": "Puppy Videos", - "bucketType": "allPublic" + "bucketType": "allPublic", + + "bucketInfo": [], + "corsRules": [], + "defaultFileLockConfiguration": { + "mode": null, + "period": null, + "status": "disabled" + }, + "lifecycleRules": [], + "options": [ + "s3" + ], + "revision": 2 }, { "bucketId": "87ba238875c6214145260818", "accountId": "30f20426f0b1", "bucketName": "Vacation Pictures", - "bucketType" : "allPrivate" + "bucketType" : "allPrivate", + + "bucketInfo": [], + "corsRules": [], + "defaultFileLockConfiguration": { + "mode": null, + "period": null, + "status": "disabled" + }, + "lifecycleRules": [], + "options": [ + "s3" + ], + "revision": 2 } ] -} \ No newline at end of file +} diff --git a/tests/responses/update_bucket_to_private.json b/tests/responses/update_bucket_to_private.json index 1de222b..2b90c28 100755 --- a/tests/responses/update_bucket_to_private.json +++ b/tests/responses/update_bucket_to_private.json @@ -2,5 +2,18 @@ "accountId": "accountId", "bucketId": "bucketId", "bucketName": "test-bucket", - "bucketType": "allPrivate" -} \ No newline at end of file + "bucketType": "allPrivate", + + "bucketInfo": [], + "corsRules": [], + "defaultFileLockConfiguration": { + "mode": null, + "period": null, + "status": "disabled" + }, + "lifecycleRules": [], + "options": [ + "s3" + ], + "revision": 2 +} diff --git a/tests/responses/update_bucket_to_public.json b/tests/responses/update_bucket_to_public.json index ee57826..5f7acdd 100755 --- a/tests/responses/update_bucket_to_public.json +++ b/tests/responses/update_bucket_to_public.json @@ -2,5 +2,18 @@ "accountId": "accountId", "bucketId": "bucketId", "bucketName": "test-bucket", - "bucketType": "allPublic" -} \ No newline at end of file + "bucketType": "allPublic", + + "bucketInfo": [], + "corsRules": [], + "defaultFileLockConfiguration": { + "mode": null, + "period": null, + "status": "disabled" + }, + "lifecycleRules": [], + "options": [ + "s3" + ], + "revision": 2 +}