From e004df4225f7cb513ca7486ed381a758aa918024 Mon Sep 17 00:00:00 2001 From: Emerson Jair Date: Tue, 31 Aug 2021 16:02:15 -0300 Subject: [PATCH 1/3] dasdsa --- composer.json | 4 +- src/Adapter/Guzzle.php | 19 +++++--- src/Endpoints/WorkersKV.php | 97 +++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 9 deletions(-) create mode 100644 src/Endpoints/WorkersKV.php diff --git a/composer.json b/composer.json index 5ffd529e..d108ad0c 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -27,7 +27,7 @@ }, "autoload-dev": { "classmap": [ - "tests/" + "tests/" ] }, "config": { diff --git a/src/Adapter/Guzzle.php b/src/Adapter/Guzzle.php index a7d8ad1c..efd2f824 100644 --- a/src/Adapter/Guzzle.php +++ b/src/Adapter/Guzzle.php @@ -10,6 +10,7 @@ use Cloudflare\API\Auth\Auth; use GuzzleHttp\Client; use Psr\Http\Message\ResponseInterface; +use InvalidArgumentException; class Guzzle implements Adapter { @@ -18,7 +19,7 @@ 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/'; @@ -26,11 +27,15 @@ public function __construct(Auth $auth, string $baseURI = null) $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; + } } @@ -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, [ diff --git a/src/Endpoints/WorkersKV.php b/src/Endpoints/WorkersKV.php new file mode 100644 index 00000000..45b86010 --- /dev/null +++ b/src/Endpoints/WorkersKV.php @@ -0,0 +1,97 @@ +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; + } +} From 87e7dfc086ebda0b27eeab841e8b99cbf71bda46 Mon Sep 17 00:00:00 2001 From: Emerson Jair Date: Tue, 31 Aug 2021 16:17:47 -0300 Subject: [PATCH 2/3] dwqdwqdw --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f07db941..b0407baa 100644 --- a/README.md +++ b/README.md @@ -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. From 70e2ad68f2abad9c8dc75b3088b984ea9f2a115b Mon Sep 17 00:00:00 2001 From: Emerson Jair Date: Tue, 31 Aug 2021 16:39:51 -0300 Subject: [PATCH 3/3] dadsads --- src/Adapter/Adapter.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Adapter/Adapter.php b/src/Adapter/Adapter.php index 44d83c51..c0e5b318 100644 --- a/src/Adapter/Adapter.php +++ b/src/Adapter/Adapter.php @@ -8,6 +8,7 @@ namespace Cloudflare\API\Adapter; use Cloudflare\API\Auth\Auth; +use GuzzleHttp\Client; use Psr\Http\Message\ResponseInterface; /** @@ -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.