From fe3ee8a2ae63b1c31e23e6c0681e1d4f896c48c9 Mon Sep 17 00:00:00 2001 From: Andrew Minion Date: Sun, 25 Oct 2020 00:14:40 -0500 Subject: [PATCH 01/11] add createKey method --- src/Client.php | 36 +++++++++++++++++++++++ src/Key.php | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 src/Key.php diff --git a/src/Client.php b/src/Client.php index cad34cd..c682fe6 100755 --- a/src/Client.php +++ b/src/Client.php @@ -667,6 +667,42 @@ protected function finishLargeFile($fileId, array $sha1s) ); } + /** + * 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 Bucket + */ + 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['applicationKey'], $response['keyName'], $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..6f806fb --- /dev/null +++ b/src/Key.php @@ -0,0 +1,80 @@ +id = $id; + $this->secret = $secret; + $this->name = $name; + $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; + } + +} From 1f216e921ff3b0a6231236e577bd7cc6a9bcff35 Mon Sep 17 00:00:00 2001 From: Andrew Minion Date: Sun, 25 Oct 2020 00:17:36 -0500 Subject: [PATCH 02/11] fix style --- src/Key.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Key.php b/src/Key.php index 6f806fb..24f646c 100644 --- a/src/Key.php +++ b/src/Key.php @@ -76,5 +76,4 @@ public function getExpirationTimestamp() { return $this->expirationTimestamp; } - } From 02bb1a8be90d48daa8ca373ef7a812df1ddcdf99 Mon Sep 17 00:00:00 2001 From: Andrew Minion Date: Sun, 25 Oct 2020 16:04:29 -0500 Subject: [PATCH 03/11] add more bucket info --- src/Bucket.php | 23 ++++++++++++++++++++++- src/Client.php | 6 +++--- 2 files changed, 25 insertions(+), 4 deletions(-) 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 c682fe6..19b5746 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'], $bucket['options'], $bucket['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'], $bucket['options'], $bucket['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; From 04c3fd9cfe043de181ecdde006b9d09591228cde Mon Sep 17 00:00:00 2001 From: Andrew Minion Date: Tue, 3 Nov 2020 19:19:00 -0600 Subject: [PATCH 04/11] fix typo --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 19b5746..c13db98 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'], $bucket['options'], $bucket['corsRules']); + return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType'], $response['options'], $response['corsRules']); } /** From 25ef0a1e9d558c3215dc2e48ee5c916c8bfb60ee Mon Sep 17 00:00:00 2001 From: Andrew Minion Date: Tue, 3 Nov 2020 19:25:31 -0600 Subject: [PATCH 05/11] fix more typos --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index c13db98..e6935af 100755 --- a/src/Client.php +++ b/src/Client.php @@ -107,7 +107,7 @@ public function updateBucket(array $options) 'bucketType' => $options['BucketType'], ]); - return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType'], $bucket['options'], $bucket['corsRules']); + return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType'], $response['options'], $response['corsRules']); } /** From 4bcd32b4454dacd94dcb4fd51013e8d0b2e561cd Mon Sep 17 00:00:00 2001 From: Andrew Minion Date: Tue, 3 Nov 2020 19:25:40 -0600 Subject: [PATCH 06/11] add mock data to API responses --- tests/responses/create_bucket_private.json | 17 ++++++- tests/responses/create_bucket_public.json | 23 +++++++-- tests/responses/list_buckets_3.json | 47 +++++++++++++++++-- tests/responses/update_bucket_to_private.json | 17 ++++++- tests/responses/update_bucket_to_public.json | 17 ++++++- 5 files changed, 106 insertions(+), 15 deletions(-) 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 +} From 6edc18cb1b88a0dfe9257d76b5c79f50e53460cd Mon Sep 17 00:00:00 2001 From: Andrew Minion Date: Tue, 1 Dec 2020 09:21:28 -0600 Subject: [PATCH 07/11] fix typo in key creation --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index e6935af..bde1c25 100755 --- a/src/Client.php +++ b/src/Client.php @@ -700,7 +700,7 @@ public function createKey(array $options) $response = $this->sendAuthorizedRequest('POST', 'b2_create_key', $request); - return new Key($response['applicationKeyId'], $response['applicationKey'], $response['keyName'], $response['capabilities'], $response['bucketId'], $response['namePrefix'], $response['expirationTimestamp']); + return new Key($response['applicationKeyId'], $response['keyName'], $response['applicationKey'], $response['capabilities'], $response['bucketId'], $response['namePrefix'], $response['expirationTimestamp']); } /** From f85c05418a7ffeac8fb5d1c3cff4583be3c94f62 Mon Sep 17 00:00:00 2001 From: Andrew Minion Date: Tue, 1 Dec 2020 09:31:58 -0600 Subject: [PATCH 08/11] tweak assignment order to match params order --- src/Key.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Key.php b/src/Key.php index 24f646c..8769fd1 100644 --- a/src/Key.php +++ b/src/Key.php @@ -34,8 +34,8 @@ class Key public function __construct($id, $name, $secret, $capabilities, $bucketId, $namePrefix, $expirationTimestamp) { $this->id = $id; - $this->secret = $secret; $this->name = $name; + $this->secret = $secret; $this->capabilities = $capabilities; $this->bucketId = $bucketId; $this->namePrefix = $namePrefix; From 86dcb8e1f2b9d7b7781129782cb3149301e18ca4 Mon Sep 17 00:00:00 2001 From: Andrew Minion Date: Tue, 1 Dec 2020 09:32:09 -0600 Subject: [PATCH 09/11] fix docs typo --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index bde1c25..d001ef1 100755 --- a/src/Client.php +++ b/src/Client.php @@ -676,7 +676,7 @@ protected function finishLargeFile($fileId, array $sha1s) * @throws GuzzleException If the request fails. * @throws B2Exception If the B2 server replies with an error. * - * @return Bucket + * @return Key */ public function createKey(array $options) { From aec7d62253dfc099984d785a3cf9e6d50b7e055a Mon Sep 17 00:00:00 2001 From: Andrew Minion Date: Tue, 1 Dec 2020 09:32:16 -0600 Subject: [PATCH 10/11] add list and delete key methods --- src/Client.php | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/Client.php b/src/Client.php index d001ef1..d284b22 100755 --- a/src/Client.php +++ b/src/Client.php @@ -667,6 +667,40 @@ 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. * @@ -703,6 +737,28 @@ public function createKey(array $options) 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. * From dc8da999d8b477c72d954c4eb3d8e53d79b9607c Mon Sep 17 00:00:00 2001 From: Andrew Minion Date: Thu, 4 Feb 2021 20:47:03 -0600 Subject: [PATCH 11/11] fix code syle for scrutinizer --- src/Client.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Client.php b/src/Client.php index d284b22..57716cd 100755 --- a/src/Client.php +++ b/src/Client.php @@ -698,6 +698,7 @@ public function listKeys(array $options = []) foreach ($response['keys'] as $key) { $keys[] = new Key($key['applicationKeyId'], $key['keyName'], null, $key['capabilities'], $key['bucketId'], $key['namePrefix'], $key['expirationTimestamp']); } + return $keys; }