Skip to content

Commit

Permalink
Merge pull request #128 from ToshY/feature/127
Browse files Browse the repository at this point in the history
Add Edge Storage API download as ZIP
  • Loading branch information
ToshY authored Aug 16, 2024
2 parents e8f0a7f + 3aa9dcc commit 7ba3f29
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
36 changes: 36 additions & 0 deletions docs/edge-storage-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,42 @@ $edgeStorageApi->downloadFile(
);
```

#### Download Zip

```php
// Root directory.
$edgeStorageApi->downloadZip(
storageZoneName: 'my-storage-zone-1',
body: [
'RootPath' => '/my-storage-zone-1/',
'Paths' => [
'/my-storage-zone-1/',
]
],
);

// Subdirectory.
$edgeStorageApi->downloadZip(
storageZoneName: 'my-storage-zone-1',
body: [
'RootPath' => '/my-storage-zone-1/',
'Paths' => [
'/my-storage-zone-1/images/',
]
],
);
```

!!! note
- Make sure your `RootPath` and `Paths` contain **leading** and **trailing** slashes.
- If you omit the slashes in `RootPath` this will result in a `400` status code.
- If you omit the slashes in `Paths` this will result in a `200` status code with an empty ZIP file.

!!! warning

- This endpoint (with method `POST`) is currently not documented in the API specifications.
- This request may fail or timeout if the requested directory has too many files or is too big.

#### [Upload File](https://docs.bunny.net/reference/put_-storagezonename-path-filename)

```php
Expand Down
1 change: 1 addition & 0 deletions phpmd.baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<violation rule="PHPMD\Rule\Design\TooManyPublicMethods" file="src/BaseAPI.php"/>
<violation rule="PHPMD\Rule\Design\WeightedMethodCount" file="src/BaseAPI.php"/>
<violation rule="PHPMD\Rule\Design\CouplingBetweenObjects" file="src/BaseAPI.php"/>
<violation rule="PHPMD\Rule\Design\CouplingBetweenObjects" file="src/EdgeStorageAPI.php"/>
<violation rule="PHPMD\Rule\Design\LongMethod" file="src/Model/API/Base/PullZone/AddPullZone.php" method="getBody"/>
<violation rule="PHPMD\Rule\Design\LongMethod" file="src/Model/API/Base/PullZone/UpdatePullZone.php" method="getBody"/>
<violation rule="PHPMD\Rule\CleanCode\BooleanArgumentFlag" file="src/Model/AbstractParameter.php"/>
Expand Down
29 changes: 29 additions & 0 deletions src/EdgeStorageAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
use Psr\Http\Client\ClientExceptionInterface;
use ToshY\BunnyNet\Client\BunnyClient;
use ToshY\BunnyNet\Enum\Region;
use ToshY\BunnyNet\Helper\BodyContentHelper;
use ToshY\BunnyNet\Model\API\EdgeStorage\BrowseFiles\ListFiles;
use ToshY\BunnyNet\Model\API\EdgeStorage\ManageFiles\DeleteFile;
use ToshY\BunnyNet\Model\API\EdgeStorage\ManageFiles\DownloadFile;
use ToshY\BunnyNet\Model\API\EdgeStorage\ManageFiles\DownloadZip;
use ToshY\BunnyNet\Model\API\EdgeStorage\ManageFiles\UploadFile;
use ToshY\BunnyNet\Model\Client\Interface\BunnyClientResponseInterface;
use ToshY\BunnyNet\Validator\ParameterValidator;

class EdgeStorageAPI
{
Expand Down Expand Up @@ -52,6 +55,32 @@ public function downloadFile(
);
}

/**
* @throws ClientExceptionInterface
* @throws Exception\BunnyClientResponseException
* @throws Exception\InvalidTypeForKeyValueException
* @throws Exception\InvalidTypeForListValueException
* @throws Exception\JSONException
* @throws Exception\ParameterIsRequiredException
* @param string $storageZoneName
* @param mixed $body
* @return BunnyClientResponseInterface
*/
public function downloadZip(
string $storageZoneName,
mixed $body,
): BunnyClientResponseInterface {
$endpoint = new DownloadZip();

ParameterValidator::validate($body, $endpoint->getBody());

return $this->client->request(
endpoint: $endpoint,
parameters: [$storageZoneName],
body: BodyContentHelper::getBody($body),
);
}

/**
* @throws ClientExceptionInterface
* @throws Exception\BunnyClientResponseException
Expand Down
42 changes: 42 additions & 0 deletions src/Model/API/EdgeStorage/ManageFiles/DownloadZip.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace ToshY\BunnyNet\Model\API\EdgeStorage\ManageFiles;

use ToshY\BunnyNet\Enum\Header;
use ToshY\BunnyNet\Enum\Method;
use ToshY\BunnyNet\Enum\Type;
use ToshY\BunnyNet\Model\AbstractParameter;
use ToshY\BunnyNet\Model\EndpointBodyInterface;
use ToshY\BunnyNet\Model\EndpointInterface;

class DownloadZip implements EndpointInterface, EndpointBodyInterface
{
public function getMethod(): Method
{
return Method::POST;
}

public function getPath(): string
{
return '%s/';
}

public function getBody(): array
{
return [
new AbstractParameter(name: 'RootPath', type: Type::STRING_TYPE),
new AbstractParameter(name: 'Paths', type: Type::ARRAY_TYPE, required: true, children: [
new AbstractParameter(name: null, type: Type::STRING_TYPE),
]),
];
}

public function getHeaders(): array
{
return [
Header::CONTENT_TYPE_JSON,
];
}
}

0 comments on commit 7ba3f29

Please sign in to comment.