From 5fcb00153b2b63f1ffca2479b8d832250d7f0af5 Mon Sep 17 00:00:00 2001 From: Junade Ali Date: Wed, 1 Feb 2017 18:07:33 +0000 Subject: [PATCH] Finished Guzzle implementation, started work on Endpoints. --- .idea/cloudflare-php.iml | 20 + .idea/copyright/profiles_settings.xml | 3 + .idea/misc.xml | 13 + .idea/modules.xml | 8 + .idea/php.xml | 4 + .idea/vcs.xml | 6 + .idea/workspace.xml | 645 ++++++++++++++++++++++++++ README.md | 19 + src/API.php | 26 -- src/Adapter/Adapter.php | 18 +- src/Adapter/Guzzle.php | 62 ++- src/Endpoints/API.php | 16 + src/Endpoints/User.php | 28 ++ tests/Adapter/GuzzleTest.php | 70 ++- tests/Endpoints/UserTest.php | 18 + 15 files changed, 912 insertions(+), 44 deletions(-) create mode 100644 .idea/cloudflare-php.iml create mode 100644 .idea/copyright/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/php.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 README.md delete mode 100644 src/API.php create mode 100644 src/Endpoints/API.php create mode 100644 src/Endpoints/User.php create mode 100644 tests/Endpoints/UserTest.php diff --git a/.idea/cloudflare-php.iml b/.idea/cloudflare-php.iml new file mode 100644 index 0000000..2f9219e --- /dev/null +++ b/.idea/cloudflare-php.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..72abef0 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..52668a2 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 0000000..10b171f --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..4f30e22 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,645 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + project + + + + + + + + + + + + + + + + project + + + true + + + + DIRECTORY + + false + + + + + + + + + 1484237519814 + + + 1484351548066 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..823bd3a --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# cloudflare-php + +## Installation + +The recommended way to install this package is via the Packagist Dependency Manager. + +## Cloudflare API version 4 + +The Cloudflare API can be found [here](https://api.cloudflare.com/). +Each API call is provided via a similarly named function within the **CloudFlare** class. +A full list is provided below. + +## Example code + +All example code is available on GitHub (see [package](https://github.com/cloudflare/python-cloudflare) in the [examples](https://github.com/cloudflare/python-cloudflare/tree/master/examples) folder. + +## Getting Started + +## Licensing \ No newline at end of file diff --git a/src/API.php b/src/API.php deleted file mode 100644 index fde7e31..0000000 --- a/src/API.php +++ /dev/null @@ -1,26 +0,0 @@ -auth = $auth; - } - - public function verifyAuth() - { - - } -} \ No newline at end of file diff --git a/src/Adapter/Adapter.php b/src/Adapter/Adapter.php index 860406d..e94feb4 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 Psr\Http\Message\ResponseInterface; /** * Interface Adapter @@ -34,7 +35,7 @@ interface Adapter * * @return mixed */ - public function get(String $uri, array $headers); + public function get(String $uri, array $headers): ResponseInterface; /** * @param String $uri @@ -43,7 +44,7 @@ interface Adapter * * @return mixed */ - public function post(String $uri, array $headers, array $body); + public function post(String $uri, array $headers, array $body): ResponseInterface; /** * @param String $uri @@ -52,7 +53,7 @@ interface Adapter * * @return mixed */ - public function put(String $uri, array $headers, array $body); + public function put(String $uri, array $headers, array $body): ResponseInterface; /** * @param String $uri @@ -61,5 +62,14 @@ interface Adapter * * @return mixed */ - public function delete(String $uri, array $headers, array $body); + public function patch(String $uri, array $headers, array $body): ResponseInterface; + + /** + * @param String $uri + * @param array $headers + * @param array $body + * + * @return mixed + */ + public function delete(String $uri, array $headers, array $body): ResponseInterface; } \ No newline at end of file diff --git a/src/Adapter/Guzzle.php b/src/Adapter/Guzzle.php index 4315892..901d76d 100644 --- a/src/Adapter/Guzzle.php +++ b/src/Adapter/Guzzle.php @@ -10,6 +10,7 @@ namespace Cloudflare\API\Adapter; use Cloudflare\API\Auth\Auth; use GuzzleHttp\Client; +use Psr\Http\Message\ResponseInterface; class Guzzle implements Adapter { @@ -20,8 +21,12 @@ class Guzzle implements Adapter */ public function __construct(Auth $auth, String $baseURI = "https://api.cloudflare.com/client/v4/") { + $headers = $auth->getHeaders(); + $this->client = new Client([ - 'base_uri' => $baseURI + 'base_uri' => $baseURI, + 'headers' => $headers, + 'Accept' => 'application/json' ]); } @@ -29,34 +34,71 @@ class Guzzle implements Adapter /** * @inheritDoc */ - public function get(String $uri, array $headers = array()) + public function get(String $uri, array $headers = array()): ResponseInterface { - $response = $this->client->get($uri, $headers); - var_dump((string)$response); + $response = $this->client->get($uri, ['headers' => $headers]); + + return $response; } /** * @inheritDoc */ - public function post(String $uri, array $headers = array(), array $body = array()) + public function post(String $uri, array $headers = array(), array $body = array()): ResponseInterface { - // TODO: Implement post() method. + $response = $this->client->post($uri, [ + 'headers' => $headers, + 'form_params' => $body + ] + ); + + return $response; } /** * @inheritDoc */ - public function put(String $uri, array $headers = array(), array $body = array()) + public function put(String $uri, array $headers = array(), array $body = array()): ResponseInterface { - // TODO: Implement put() method. + $jsonBody = json_encode($body); + + $response = $this->client->put($uri, [ + 'headers' => $headers, + 'body' => $jsonBody + ] + ); + + return $response; } /** * @inheritDoc */ - public function delete(String $uri, array $headers = array(), array $body = array()) + public function patch(String $uri, array $headers = array(), array $body = array()): ResponseInterface { - // TODO: Implement delete() method. + $jsonBody = json_encode($body); + + $response = $this->client->patch($uri, [ + 'headers' => $headers, + 'body' => $jsonBody + ] + ); + + return $response; + } + + /** + * @inheritDoc + */ + public function delete(String $uri, array $headers = array(), array $body = array()): ResponseInterface + { + $response = $this->client->delete($uri, [ + 'headers' => $headers, + 'form_params' => $body + ] + ); + + return $response; } } \ No newline at end of file diff --git a/src/Endpoints/API.php b/src/Endpoints/API.php new file mode 100644 index 0000000..ebcaab0 --- /dev/null +++ b/src/Endpoints/API.php @@ -0,0 +1,16 @@ +adapter = $adapter; + } + + public function getUserDetails() + { + $user = $this->adapter->get('user', []); + $body = json_decode($user->getBody()); + var_dump($body); + } +} \ No newline at end of file diff --git a/tests/Adapter/GuzzleTest.php b/tests/Adapter/GuzzleTest.php index bd68013..c639167 100644 --- a/tests/Adapter/GuzzleTest.php +++ b/tests/Adapter/GuzzleTest.php @@ -7,14 +7,76 @@ */ class GuzzleTest extends PHPUnit_Framework_TestCase { - public function testGet() - { + private $client; + public function setUp() + { $auth = $this->getMockBuilder(\Cloudflare\API\Auth\Auth::class) ->setMethods(['getHeaders']) ->getMock(); - $client = new \Cloudflare\API\Adapter\Guzzle($auth, 'https://httpbin.org/'); - $client->get('https://httpbin.org/get'); + $auth->method('getHeaders') + ->willReturn(['X-Testing' => 'Test']); + + $this->client = new \Cloudflare\API\Adapter\Guzzle($auth, 'https://httpbin.org/'); + } + + public function testGet() + { + $response = $this->client->get('https://httpbin.org/get'); + + $headers = $response->getHeaders(); + $this->assertEquals("application/json", $headers["Content-Type"][0]); + + $body = json_decode($response->getBody()); + $this->assertEquals("Test", $body->headers->{"X-Testing"}); + + $response = $this->client->get('https://httpbin.org/get', ['X-Another-Test' => 'Test2']); + $body = json_decode($response->getBody()); + $this->assertEquals("Test2", $body->headers->{"X-Another-Test"}); + } + + public function testPost() + { + $response = $this->client->post('https://httpbin.org/post', [], ['X-Post-Test' => 'Testing a POST request.']); + + $headers = $response->getHeaders(); + $this->assertEquals("application/json", $headers["Content-Type"][0]); + + $body = json_decode($response->getBody()); + $this->assertEquals("Testing a POST request.", $body->form->{"X-Post-Test"}); + } + + public function testPut() + { + $response = $this->client->put('https://httpbin.org/put', [], ['X-Put-Test' => 'Testing a PUT request.']); + + $headers = $response->getHeaders(); + $this->assertEquals("application/json", $headers["Content-Type"][0]); + + $body = json_decode($response->getBody()); + $this->assertEquals("Testing a PUT request.", $body->json->{"X-Put-Test"}); + } + + public function testPatch() + { + $response = $this->client->patch('https://httpbin.org/patch', [], ['X-Patch-Test' => 'Testing a PATCH request.']); + + $headers = $response->getHeaders(); + $this->assertEquals("application/json", $headers["Content-Type"][0]); + + $body = json_decode($response->getBody()); + $this->assertEquals("Testing a PATCH request.", $body->json->{"X-Patch-Test"}); + } + + public function testDelete() + { + $response = $this->client->delete('https://httpbin.org/delete', [], ['X-Delete-Test' => 'Testing a DELETE request.']); + + $headers = $response->getHeaders(); + $this->assertEquals("application/json", $headers["Content-Type"][0]); + + $body = json_decode($response->getBody()); + $this->assertEquals("Testing a DELETE request.", $body->form->{"X-Delete-Test"}); } } diff --git a/tests/Endpoints/UserTest.php b/tests/Endpoints/UserTest.php new file mode 100644 index 0000000..da94907 --- /dev/null +++ b/tests/Endpoints/UserTest.php @@ -0,0 +1,18 @@ +getUserDetails(); + } +}