Skip to content

Commit

Permalink
Added basic workersroute script
Browse files Browse the repository at this point in the history
  • Loading branch information
HemeraOne committed Jan 16, 2023
1 parent 732de0d commit cd1ee5e
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 35 deletions.
35 changes: 0 additions & 35 deletions src/Endpoints/Workers.php

This file was deleted.

93 changes: 93 additions & 0 deletions src/Endpoints/WorkersRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* @author Martijn Smidt <[email protected]>
* User: HemeraOne
* Date: 16/01/2023
*/

namespace Cloudflare\API\Endpoints;

use Cloudflare\API\Adapter\Adapter;
use Cloudflare\API\Traits\BodyAccessorTrait;

class WorkersRoute implements API
{
use BodyAccessorTrait;

private $adapter;

public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
}

/**
* @param string $zoneID
* @return mixed
*/
public function listRoutes(string $zoneID)
{
$workerRoutes = $this->adapter->get('zones/' . $zoneID . '/workers/routes');
$this->body = json_decode($workerRoutes->getBody());

return $this->body->result;
}

public function getRoute(string $zoneID, string $routeID) {
$workerRoutes = $this->adapter->get('zones/' . $zoneID . '/workers/routes/' . $routeID);
$this->body = json_decode($workerRoutes->getBody());

return $this->body->result;
}

public function createRoute(string $zoneID, string $pattern, ?string $script = null) {
$options = [
'pattern' => $pattern
];

if($script) {
$options['script'] = $script;
}

$query = $this->adapter->post('zones/' . $zoneID . '/workers/routes', $options);

$this->body = json_decode($query->getBody());

if (isset($this->body->result->id)) {
return true;
}

return false;
}

public function updateRoute(string $zoneID, string $routeID, string $pattern, ?string $script = null) {
$options = [
'pattern' => $pattern
];

if($script) {
$options['script'] = $script;
}

$query = $this->adapter->put('zones/' . $zoneID . '/workers/routes/' . $routeID, $options);

$this->body = json_decode($query->getBody());

if (isset($this->body->result->id)) {
return true;
}

return false;
}

public function deleteRoute(string $zoneID, string $routeID) {
$workerRoutes = $this->adapter->delete('zones/' . $zoneID . '/workers/routes/' . $routeID);
$this->body = json_decode($workerRoutes->getBody());

if (isset($this->body->result->id)) {
return true;
}

return false;
}
}

0 comments on commit cd1ee5e

Please sign in to comment.