Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Each API call is provided via a similarly named function within various classes
- [x] Crypto
- [x] Load Balancers
- [x] Firewall Settings
- [x] WorkersKV

Note that this repository is currently under development, additional classes and endpoints being actively added.

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"phpmd/phpmd" : "@stable",
"phpmd/phpmd": "@stable",
"friendsofphp/php-cs-fixer": "^2.6"
},
"license": "BSD-3-Clause",
Expand All @@ -27,7 +27,7 @@
},
"autoload-dev": {
"classmap": [
"tests/"
"tests/"
]
},
"config": {
Expand Down
4 changes: 3 additions & 1 deletion src/Adapter/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Cloudflare\API\Adapter;

use Cloudflare\API\Auth\Auth;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;

/**
Expand All @@ -21,9 +22,10 @@ interface Adapter
* Adapter constructor.
*
* @param Auth $auth
* @param Client $client
* @param string $baseURI
*/
public function __construct(Auth $auth, string $baseURI);
public function __construct(Auth $auth, Client $client, string $baseURI);

/**
* Sends a GET request.
Expand Down
19 changes: 12 additions & 7 deletions src/Adapter/Guzzle.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Cloudflare\API\Auth\Auth;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
use InvalidArgumentException;

class Guzzle implements Adapter
{
Expand All @@ -18,19 +19,23 @@ class Guzzle implements Adapter
/**
* @inheritDoc
*/
public function __construct(Auth $auth, string $baseURI = null)
public function __construct(Auth $auth, Client $client = null, string $baseURI = null)
{
if ($baseURI === null) {
$baseURI = 'https://api.cloudflare.com/client/v4/';
}

$headers = $auth->getHeaders();

$this->client = new Client([
'base_uri' => $baseURI,
'headers' => $headers,
'Accept' => 'application/json'
]);
if ($client == null) {
$this->client = new Client([
'base_uri' => $baseURI,
'headers' => $headers,
'Accept' => 'application/json'
]);
} else {
$this->client = $client;
}
}


Expand Down Expand Up @@ -77,7 +82,7 @@ public function delete(string $uri, array $data = [], array $headers = []): Resp
public function request(string $method, string $uri, array $data = [], array $headers = [])
{
if (!in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
throw new \InvalidArgumentException('Request method must be get, post, put, patch, or delete');
throw new InvalidArgumentException('Request method must be get, post, put, patch, or delete');
}

$response = $this->client->$method($uri, [
Expand Down
97 changes: 97 additions & 0 deletions src/Endpoints/WorkersKV.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/**
* User: junade
* Date: 01/02/2017
* Time: 12:30
*/

namespace Cloudflare\API\Endpoints;

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

class WorkersKV implements API
{
use BodyAccessorTrait;

private $adapter;

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

public function createNamespace(string $accountID, string $namespace): \stdClass
{
$response = $this->adapter->post(
'accounts/' . $accountID . '/storage/kv/namespaces',
['title' => $namespace]
);
$this->body = json_decode($response->getBody());
return $this->body->result;
}

public function getNameSpaces(string $accountID): array
{
$response = $this->adapter->get('accounts/' . $accountID . '/storage/kv/namespaces');
$this->body = json_decode($response->getBody());
return $this->body->result;
}

public function listNamespaceKeys(string $accountID, $namespaceIdentifier): array
{
$response = $this->adapter->get('accounts/' . $accountID . '/storage/kv/namespaces/' . $namespaceIdentifier . '/keys');
$this->body = json_decode($response->getBody());
return $this->body->result;
}

public function getReadKeyValuePair(string $accountID, $namespaceIdentifier, $keyName): string
{
$response = $this->adapter->get('accounts/' . $accountID . '/storage/kv/namespaces/' . $namespaceIdentifier . '/values/' . $keyName);
$this->body = (string) $response->getBody();
return $this->body;
}

public function getAllKeysAndValuesForNamespace(string $accountID, string $namespaceIdentifier)
{
$keys = $this->listNamespaceKeys($accountID, $namespaceIdentifier);
foreach ($keys as $index => $values) {
$value = $this->getReadKeyValuePair($accountID, $namespaceIdentifier, $values->name);
$keys[$index]->value = $value;
}
return $keys;
}

public function getListOfNamespaces(string $accountID, int $page = 1, int $perPage = 50, string $order = '', string $direction = '')
{
$query = [
'page' => $page,
'per_page' => $perPage
];

if (!empty($order)) {
$query['order'] = $order;
}

if (!empty($direction)) {
$query['direction'] = $direction;
}

$namespace = $this->adapter->get('accounts/' . $accountID . '/storage/kv/namespaces', $query);
$this->body = json_decode($namespace->getBody());
return $this->body->result;
}

public function writeMultipleKeyValuePairs(string $accountID, $namespaceIdentifier, $keys = []): bool
{
$this->adapter->put('accounts/' . $accountID . '/storage/kv/namespaces/' . $namespaceIdentifier . '/bulk', $keys);
return true;
}

public function deleteKeyValuePair(string $accountID, $namespaceIdentifier, string $key): bool
{
$this->adapter->delete('accounts/' . $accountID . '/storage/kv/namespaces/' . $namespaceIdentifier . '/values', [$key]);
return true;
}
}