Skip to content

Commit

Permalink
Merge pull request #19 from zeroSal/master
Browse files Browse the repository at this point in the history
Implemented deleteKey() and github actions
  • Loading branch information
robregonm authored Apr 28, 2024
2 parents 15a9f35 + 614ead2 commit b484747
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 7 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/actions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: PHP Pipeline

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
lint:
name: PHP Lint
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: PHP Lint
run: php -l src/

phpstan:
name: PHPStan
runs-on: ubuntu-latest
needs: lint
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install Composer dependencies
run: composer install --prefer-dist --no-scripts --no-progress --no-suggest

- name: Run PHPStan
run: composer verify-code

cs-fixer:
name: PHP CS Fixer
runs-on: ubuntu-latest
needs: phpstan
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install Composer dependencies
run: composer install --prefer-dist --no-scripts --no-progress --no-suggest

- name: Run PHP CS Fixer
run: vendor/bin/php-cs-fixer fix --dry-run --diff src
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
vendor/*
.php_cs-fixer.cache
.phpstan.cache/
var/
var/
composer.lock
.php-cs-fixer.cache
phpstan.neon
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ $fileContent = $client->download([
// Delete a file from a bucket. Returns true or false.
$fileDelete = $client->deleteFileFromArray([
'FileId' => $file->getId()

// Can also identify the file via bucket and path:
// 'BucketName' => 'my-special-bucket',
// 'FileName' => 'path/to/file'
Expand All @@ -84,7 +84,7 @@ $fileList = $client->listFilesFromArray([
'BucketId' => '4d2dbbe08e1e983c5e6f0d12'
]);

// Create a new access key
// Create a new access key.
$capabilities = new Capabilities()
$key = $client->createKey($accountId, $name, new Capabilities(
[Capabilities::DELETE_BUCKETS,
Expand All @@ -93,7 +93,14 @@ $key = $client->createKey($accountId, $name, new Capabilities(
));

$keyId = $key->getKeyId();
$applicationKetId = $key->getApplicationKey();
$applicationKeyId = $key->getApplicationKey();

// Delete an existing access key.
try {
$client->deleteKey($keyId);
} catch (RequestException $e) {
// $e->getCode()
}
```

## Installation
Expand Down
46 changes: 44 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace obregonco\B2;

use GuzzleHttp\Exception\RequestException;
use Illuminate\Cache\CacheManager;
use Illuminate\Container\Container;
use Illuminate\Filesystem\Filesystem;
Expand Down Expand Up @@ -354,7 +355,7 @@ public function listFilesFromArray(array $options): array

// B2 returns, at most, 1000 files per "page". Loop through the pages and compile an array of File objects.
while (true) {
$response = $this->request('POST', '/b2_list_file_names', [
$response = $this->request('POST', '/b2_list_file_versions', [
'json' => [
'bucketId' => $options['BucketId'],
'prefix' => $prefix,
Expand Down Expand Up @@ -405,7 +406,7 @@ public function listFiles(
if (!empty($delimiter)) {
$params['delimiter'] = $delimiter;
}
$response = $this->request('POST', '/b2_list_file_names', [
$response = $this->request('POST', '/b2_list_file_versions', [
'json' => $params,
]);

Expand Down Expand Up @@ -859,6 +860,47 @@ public function createKey(string $name, Capabilities $capabilities, string $buck
);
}

/**
* Deletes the key having the provided $id from Backblaze.
*
* @throws RequestException
* @throws \InvalidArgumentException
*/
public function deleteKey(string $id): void
{
if (empty($id)) {
throw new \InvalidArgumentException('The key ID is empty.');
}

$json = [
'applicationKeyId' => $id,
];

$this->request('POST', '/b2_delete_key', [
'json' => $json,
true
]);
}

/**
* @throws \RuntimeException
*/
public function retrieveKeys(): array
{
$json = [
'accountId' => $this->accountId,
'maxKeyCount' => 10000
];

/** @var array */
$response = $this->request('POST', '/b2_list_keys', [
"json" => $json,
true
]);

return $response['keys'];
}

/**
* Authorize the B2 account in order to get an auth token and API/download URLs.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function getUploadTimestamp()
/**
* @return array
*/
public function jsonSerialize():mixed
public function jsonSerialize(): mixed
{
return $this->asArray();
}
Expand Down
5 changes: 5 additions & 0 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/**
* Client wrapper around Guzzle.
*/
// FIXME: Class obregonco\B2\Http\Client extends @final class GuzzleHttp\Client.
// @phpstan-ignore-next-line
class Client extends GuzzleClient
{
public $retryLimit = 10;
Expand All @@ -21,6 +23,9 @@ class Client extends GuzzleClient
*
* @return mixed|string
*/
// FIXME: PHPDoc tag @return with type mixed is not subtype of native type Psr\Http\Message\ResponseInterface.
// FIXME: Default value of the parameter #2 $uri (string) of method obregonco\B2\Http\Client::request() is incompatible with type null.
// @phpstan-ignore-next-line
public function request(string $method, $uri = '', array $options = []): ResponseInterface
{
$response = parent::request($method, $uri, $options);
Expand Down

0 comments on commit b484747

Please sign in to comment.