Skip to content

Commit

Permalink
refactored compute script code to edge scripting code
Browse files Browse the repository at this point in the history
  • Loading branch information
ToshY committed Nov 17, 2024
1 parent 23c5693 commit 0fb015e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 49 deletions.
21 changes: 21 additions & 0 deletions docs/edge-scripting-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ $edgeScriptingApi = new EdgeScriptingAPI(

## Usage

### Code

#### [Get Code](https://docs.bunny.net/reference/getedgescriptcodeendpoint_getcode)

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

#### [Set Code](https://docs.bunny.net/reference/uploadedgescriptcodeendpoint_setcode)

```php
$edgeScriptingApi->setCode(
id: 1,
body: [
'Code' => "export default function handleQuery(event) {\n console.log(\"Hello world!\")\n return new TxtRecord(\"Hello world!\");\n}",
],
);
```

## 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\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"/>
<violation rule="PHPMD\Rule\Design\LongMethod" file="src/Model/API/Base/PullZone/UpdatePullZone.php" method="getBody"/>
Expand Down
45 changes: 0 additions & 45 deletions src/BaseAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@
use ToshY\BunnyNet\Model\API\Base\Compute\DeleteComputeScript;
use ToshY\BunnyNet\Model\API\Base\Compute\DeleteComputeScriptVariable;
use ToshY\BunnyNet\Model\API\Base\Compute\GetComputeScript;
use ToshY\BunnyNet\Model\API\Base\Compute\GetComputeScriptCode;
use ToshY\BunnyNet\Model\API\Base\Compute\GetComputeScriptVariable;
use ToshY\BunnyNet\Model\API\Base\Compute\ListComputeScriptReleases;
use ToshY\BunnyNet\Model\API\Base\Compute\ListComputeScripts;
use ToshY\BunnyNet\Model\API\Base\Compute\PublishComputeScript;
use ToshY\BunnyNet\Model\API\Base\Compute\PublishComputeScriptByPathParameter;
use ToshY\BunnyNet\Model\API\Base\Compute\UpdateComputeScript;
use ToshY\BunnyNet\Model\API\Base\Compute\UpdateComputeScriptCode;
use ToshY\BunnyNet\Model\API\Base\Compute\UpdateComputeScriptVariable;
use ToshY\BunnyNet\Model\API\Base\Countries\ListCountries;
use ToshY\BunnyNet\Model\API\Base\DNSZone\AddDNSRecord;
Expand Down Expand Up @@ -585,49 +583,6 @@ public function deleteComputeScript(int $id): BunnyClientResponseInterface
);
}

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

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
* @return BunnyClientResponseInterface
* @param int $id
* @param array<string,mixed> $body
*/
public function updateComputeScriptCode(
int $id,
array $body = [],
): BunnyClientResponseInterface {
$endpoint = new UpdateComputeScriptCode();

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

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

/**
* @throws ClientExceptionInterface
* @throws Exception\BunnyClientResponseException
Expand Down
49 changes: 49 additions & 0 deletions src/EdgeScriptingAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

namespace ToshY\BunnyNet;

use Psr\Http\Client\ClientExceptionInterface;
use ToshY\BunnyNet\Client\BunnyClient;
use ToshY\BunnyNet\Enum\Host;
use ToshY\BunnyNet\Helper\BodyContentHelper;
use ToshY\BunnyNet\Model\API\Base\AbuseCase\GetCode;
use ToshY\BunnyNet\Model\API\Base\AbuseCase\SetCode;
use ToshY\BunnyNet\Model\Client\Interface\BunnyClientResponseInterface;
use ToshY\BunnyNet\Validator\ParameterValidator;

class EdgeScriptingAPI
{
Expand All @@ -21,4 +27,47 @@ public function __construct(
->setApiKey($this->apiKey)
->setBaseUrl(Host::API_ENDPOINT);
}

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

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
* @return BunnyClientResponseInterface
* @param int $id
* @param array<string,mixed> $body
*/
public function setCode(
int $id,
array $body = [],
): BunnyClientResponseInterface {
$endpoint = new SetCode();

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

return $this->client->request(
endpoint: $endpoint,
parameters: [$id],
body: BodyContentHelper::getBody($body),
);
}
}
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

namespace ToshY\BunnyNet\Model\API\Base\Compute;
namespace ToshY\BunnyNet\Model\API\Base\AbuseCase;

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

class GetComputeScriptCode implements EndpointInterface
class GetCode implements EndpointInterface
{
public function getMethod(): Method
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace ToshY\BunnyNet\Model\API\Base\Compute;
namespace ToshY\BunnyNet\Model\API\Base\AbuseCase;

use ToshY\BunnyNet\Enum\Header;
use ToshY\BunnyNet\Enum\Method;
Expand All @@ -11,7 +11,7 @@
use ToshY\BunnyNet\Model\EndpointBodyInterface;
use ToshY\BunnyNet\Model\EndpointInterface;

class UpdateComputeScriptCode implements EndpointInterface, EndpointBodyInterface
class SetCode implements EndpointInterface, EndpointBodyInterface
{
public function getMethod(): Method
{
Expand Down

0 comments on commit 0fb015e

Please sign in to comment.