Skip to content

Commit

Permalink
added edge scripting secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
ToshY committed Nov 17, 2024
1 parent f110f7e commit 7e618a2
Show file tree
Hide file tree
Showing 10 changed files with 362 additions and 2 deletions.
55 changes: 55 additions & 0 deletions docs/edge-scripting-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,61 @@ $edgeScriptingApi->deleteVariable(
);
```

### Secret

#### [List Secrets](https://docs.bunny.net/reference/listedgescriptsecretsendpoint_listedgescriptsecrets)

```php
$edgeScriptingApi->listSecrets(
id: 1,
);
```

#### [Add Secret](https://docs.bunny.net/reference/addedgescriptsecretendpoint_addedgescriptsecret)

```php
$edgeScriptingApi->addSecret(
id: 1,
body: [
'Name' => 'VerySecretKey',
'Secret' => 'V2UncmUgbm8gc3RyYW5nZXJzIHRvIGxvdmUKWW91IGtub3cgdGhlIHJ1bGVzIGFuZCBzbyBkbyBJIChEbyBJKQpBIGZ1bGwgY29tbWl0bWVudCdzIHdoYXQgSSdtIHRoaW5raW5nIG9mCllvdSB3b3VsZG4ndCBnZXQgdGhpcyBmcm9tIGFueSBvdGhlciBndXk=',
],
);
```

#### [Update Secret](https://docs.bunny.net/reference/updateedgescriptsecretendpoint_updateedgescriptsecret)

```php
$edgeScriptingApi->updateSecret(
id: 1,
secretId: 2,
body: [
'Secret' => 'TmV2ZXIgZ29ubmEgZ2l2ZSB5b3UgdXAKTmV2ZXIgZ29ubmEgbGV0IHlvdSBkb3duCk5ldmVyIGdvbm5hIHJ1biBhcm91bmQgYW5kIGRlc2VydCB5b3UKTmV2ZXIgZ29ubmEgbWFrZSB5b3UgY3J5Ck5ldmVyIGdvbm5hIHNheSBnb29kYnllCk5ldmVyIGdvbm5hIHRlbGwgYSBsaWUgYW5kIGh1cnQgeW91',
],
);
```

#### [Upsert Secret](https://docs.bunny.net/reference/upsertedgescriptsecretendpoint_upsertedgescriptsecret)

```php
$edgeScriptingApi->upsertSecret(
id: 1,
body: [
'Name' => 'VerySecretKey',
'Secret' => 'SSBqdXN0IHdhbm5hIHRlbGwgeW91IGhvdyBJJ20gZmVlbGluZwpHb3R0YSBtYWtlIHlvdSB1bmRlcnN0YW5k',
],
);
```

#### [Delete Secret](https://docs.bunny.net/reference/deleteedgescriptsecretendpoint_deletesecret)

```php
$edgeScriptingApi->deleteSecret(
id: 1,
secretId: 2,
);
```

## Reference

* [Edge Scripting API](https://docs.bunny.net/reference/getedgescriptcodeendpoint_getcode)
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\TooManyPublicMethods" file="src/EdgeScriptingAPI.php"/>
<violation rule="PHPMD\Rule\Design\CouplingBetweenObjects" file="src/EdgeScriptingAPI.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"/>
Expand Down
126 changes: 125 additions & 1 deletion src/EdgeScriptingAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
use ToshY\BunnyNet\Model\API\EdgeScripting\EdgeScript\UpdateEdgeScript;
use ToshY\BunnyNet\Model\API\EdgeScripting\Code\GetCode;
use ToshY\BunnyNet\Model\API\EdgeScripting\Code\SetCode;
use ToshY\BunnyNet\Model\API\EdgeScripting\Secret\AddSecret;
use ToshY\BunnyNet\Model\API\EdgeScripting\Secret\DeleteSecret;
use ToshY\BunnyNet\Model\API\EdgeScripting\Secret\ListSecrets;
use ToshY\BunnyNet\Model\API\EdgeScripting\Secret\UpdateSecret;
use ToshY\BunnyNet\Model\API\EdgeScripting\Secret\UpsertSecret;
use ToshY\BunnyNet\Model\API\EdgeScripting\Variable\AddVariable;
use ToshY\BunnyNet\Model\API\EdgeScripting\Variable\DeleteVariable;
use ToshY\BunnyNet\Model\API\EdgeScripting\Variable\GetVariable;
Expand Down Expand Up @@ -199,7 +204,6 @@ public function listEdgeScripts(array $query = []): BunnyClientResponseInterface
);
}


/**
* @throws ClientExceptionInterface
* @throws Exception\BunnyClientResponseException
Expand Down Expand Up @@ -358,4 +362,124 @@ public function upsertVariable(
body: BodyContentHelper::getBody($body),
);
}

/**
* @throws ClientExceptionInterface
* @throws Exception\BunnyClientResponseException
* @throws Exception\JSONException
* @throws Exception\InvalidTypeForKeyValueException
* @throws Exception\InvalidTypeForListValueException
* @throws Exception\ParameterIsRequiredException
* @return BunnyClientResponseInterface
* @param int $id
* @param array<string,mixed> $body
*/
public function addSecret(
int $id,
array $body,
): BunnyClientResponseInterface {
$endpoint = new AddSecret();

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

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

/**
* @throws ClientExceptionInterface
* @throws Exception\BunnyClientResponseException
* @throws Exception\JSONException
* @throws Exception\InvalidTypeForKeyValueException
* @throws Exception\InvalidTypeForListValueException
* @throws Exception\ParameterIsRequiredException
* @param int $id
* @return BunnyClientResponseInterface
*/
public function listSecrets(int $id): BunnyClientResponseInterface
{
$endpoint = new ListSecrets();

return $this->client->request(
endpoint: $endpoint,
parameters: [$id],
);
}

/**
* @throws ClientExceptionInterface
* @throws Exception\BunnyClientResponseException
* @throws Exception\JSONException
* @throws Exception\InvalidTypeForKeyValueException
* @throws Exception\InvalidTypeForListValueException
* @throws Exception\ParameterIsRequiredException
* @param array<string,mixed> $body
* @return BunnyClientResponseInterface
* @param int $id
*/
public function upsertSecret(
int $id,
array $body,
): BunnyClientResponseInterface {
$endpoint = new UpsertSecret();

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

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

/**
* @throws ClientExceptionInterface
* @throws Exception\BunnyClientResponseException
* @throws Exception\JSONException
* @param int $secretId
* @return BunnyClientResponseInterface
* @param int $id
*/
public function deleteSecret(
int $id,
int $secretId,
): BunnyClientResponseInterface {
$endpoint = new DeleteSecret();

return $this->client->request(
endpoint: $endpoint,
parameters: [$id, $secretId],
);
}

/**
* @throws ClientExceptionInterface
* @throws Exception\BunnyClientResponseException
* @throws Exception\JSONException
* @throws Exception\InvalidTypeForKeyValueException
* @throws Exception\InvalidTypeForListValueException
* @throws Exception\ParameterIsRequiredException
* @param int $secretId
* @param array<string,mixed> $body
* @return BunnyClientResponseInterface
* @param int $id
*/
public function updateSecret(
int $id,
int $secretId,
array $body,
): BunnyClientResponseInterface {
$endpoint = new UpdateSecret();

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

return $this->client->request(
endpoint: $endpoint,
parameters: [$id, $secretId],
body: BodyContentHelper::getBody($body),
);
}
}
Empty file.
41 changes: 41 additions & 0 deletions src/Model/API/EdgeScripting/Secret/AddSecret.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace ToshY\BunnyNet\Model\API\EdgeScripting\Secret;

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 AddSecret implements EndpointInterface, EndpointBodyInterface
{
public function getMethod(): Method
{
return Method::POST;
}

public function getPath(): string
{
return 'compute/script/%d/secrets';
}

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

public function getBody(): array
{
return [
new AbstractParameter(name: 'Name', type: Type::STRING_TYPE, required: true),
new AbstractParameter(name: 'Secret', type: Type::STRING_TYPE),
];
}
}
29 changes: 29 additions & 0 deletions src/Model/API/EdgeScripting/Secret/DeleteSecret.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace ToshY\BunnyNet\Model\API\EdgeScripting\Secret;

use ToshY\BunnyNet\Enum\Header;
use ToshY\BunnyNet\Enum\Method;
use ToshY\BunnyNet\Model\EndpointInterface;

class DeleteSecret implements EndpointInterface
{
public function getMethod(): Method
{
return Method::DELETE;
}

public function getPath(): string
{
return 'compute/script/%d/secrets/%d';
}

public function getHeaders(): array
{
return [
Header::ACCEPT_JSON,
];
}
}
29 changes: 29 additions & 0 deletions src/Model/API/EdgeScripting/Secret/ListSecrets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace ToshY\BunnyNet\Model\API\EdgeScripting\Secret;

use ToshY\BunnyNet\Enum\Header;
use ToshY\BunnyNet\Enum\Method;
use ToshY\BunnyNet\Model\EndpointInterface;

class ListSecrets implements EndpointInterface
{
public function getMethod(): Method
{
return Method::GET;
}

public function getPath(): string
{
return 'compute/script/%d/secrets';
}

public function getHeaders(): array
{
return [
Header::ACCEPT_JSON,
];
}
}
40 changes: 40 additions & 0 deletions src/Model/API/EdgeScripting/Secret/UpdateSecret.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace ToshY\BunnyNet\Model\API\EdgeScripting\Secret;

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 UpdateSecret implements EndpointInterface, EndpointBodyInterface
{
public function getMethod(): Method
{
return Method::POST;
}

public function getPath(): string
{
return 'compute/script/%d/secrets/%d';
}

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

public function getBody(): array
{
return [
new AbstractParameter(name: 'Secret', type: Type::STRING_TYPE),
];
}
}
41 changes: 41 additions & 0 deletions src/Model/API/EdgeScripting/Secret/UpsertSecret.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace ToshY\BunnyNet\Model\API\EdgeScripting\Secret;

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 UpsertSecret implements EndpointInterface, EndpointBodyInterface
{
public function getMethod(): Method
{
return Method::PUT;
}

public function getPath(): string
{
return 'compute/script/%d/secrets';
}

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

public function getBody(): array
{
return [
new AbstractParameter(name: 'Name', type: Type::STRING_TYPE, required: true),
new AbstractParameter(name: 'Secret', type: Type::STRING_TYPE),
];
}
}
Loading

0 comments on commit 7e618a2

Please sign in to comment.