Initial commit, WIP.

This commit is contained in:
Junade Ali
2017-01-13 23:52:27 +00:00
commit a49c655e77
13 changed files with 2150 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/vendor/

26
composer.json Normal file
View File

@@ -0,0 +1,26 @@
{
"name": "cloudflare/api",
"description": "PHP binding for v4 of the Cloudflare Client API.",
"type": "library",
"require": {
"guzzlehttp/guzzle": "^6.2.2",
"php": ">=7.0.0",
"psr/http-message": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "5.7.5",
"phpmd/phpmd" : "@stable"
},
"license": "MIT",
"authors": [
{
"name": "Junade Ali",
"email": "junade@cloudflare.com"
}
],
"autoload": {
"psr-4": {
"Cloudflare\\API\\": "src/"
}
}
}

1826
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

8
phpunit.xml Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Cloudflare API PHP Binding">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>

26
src/API.php Normal file
View File

@@ -0,0 +1,26 @@
<?php
/**
* User: junade
* Date: 13/01/2017
* Time: 16:34
*/
namespace Cloudflare\API;
use Cloudflare\API\Auth\Auth;
class API
{
private $auth;
public function __construct(Auth $auth, bool $verifyAuth = true)
{
$this->auth = $auth;
}
public function verifyAuth()
{
}
}

65
src/Adapter/Adapter.php Normal file
View File

@@ -0,0 +1,65 @@
<?php
/**
* User: junade
* Date: 13/01/2017
* Time: 16:06
*/
namespace Cloudflare\API\Adapter;
use Cloudflare\API\Auth\Auth;
/**
* Interface Adapter
* @package Cloudflare\API\Adapter
* Note that the $body fields expect a JSON key value store.
*/
interface Adapter
{
/**
* Adapter constructor.
*
* @param Auth $auth
* @param String $baseURI
*/
public function __construct(Auth $auth, String $baseURI);
/**
* Sends a GET request.
* Per Robustness Principle - not including the ability to send a body with a GET request (though possible in the
* RFCs, it is never useful).
*
* @param String $uri
* @param array $headers
*
* @return mixed
*/
public function get(String $uri, array $headers);
/**
* @param String $uri
* @param array $headers
* @param array $body
*
* @return mixed
*/
public function post(String $uri, array $headers, array $body);
/**
* @param String $uri
* @param array $headers
* @param array $body
*
* @return mixed
*/
public function put(String $uri, array $headers, array $body);
/**
* @param String $uri
* @param array $headers
* @param array $body
*
* @return mixed
*/
public function delete(String $uri, array $headers, array $body);
}

62
src/Adapter/Guzzle.php Normal file
View File

@@ -0,0 +1,62 @@
<?php
/**
* User: junade
* Date: 13/01/2017
* Time: 18:26
*/
namespace Cloudflare\API\Adapter;
use Cloudflare\API\Auth\Auth;
use GuzzleHttp\Client;
class Guzzle implements Adapter
{
private $client;
/**
* @inheritDoc
*/
public function __construct(Auth $auth, String $baseURI = "https://api.cloudflare.com/client/v4/")
{
$this->client = new Client([
'base_uri' => $baseURI
]);
}
/**
* @inheritDoc
*/
public function get(String $uri, array $headers = array())
{
$response = $this->client->get($uri, $headers);
var_dump((string)$response);
}
/**
* @inheritDoc
*/
public function post(String $uri, array $headers = array(), array $body = array())
{
// TODO: Implement post() method.
}
/**
* @inheritDoc
*/
public function put(String $uri, array $headers = array(), array $body = array())
{
// TODO: Implement put() method.
}
/**
* @inheritDoc
*/
public function delete(String $uri, array $headers = array(), array $body = array())
{
// TODO: Implement delete() method.
}
}

29
src/Auth/APIKey.php Normal file
View File

@@ -0,0 +1,29 @@
<?php
/**
* User: junade
* Date: 13/01/2017
* Time: 16:55
*/
namespace Cloudflare\API\Auth;
class APIKey implements Auth
{
private $email;
private $apiKey;
public function __construct(String $email, String $apiKey)
{
$this->email = $email;
$this->apiKey = $apiKey;
}
public function getHeaders(): array
{
return [
'X-Auth-Email' => $this->email,
'X-Auth-Key' => $this->apiKey
];
}
}

14
src/Auth/Auth.php Normal file
View File

@@ -0,0 +1,14 @@
<?php
/**
* User: junade
* Date: 13/01/2017
* Time: 16:52
*/
namespace Cloudflare\API\Auth;
interface Auth
{
public function getHeaders(): array;
}

View File

@@ -0,0 +1,26 @@
<?php
/**
* User: junade
* Date: 13/01/2017
* Time: 18:01
*/
namespace Cloudflare\API\Auth;
class UserServiceKey implements Auth
{
private $userServiceKey;
public function __construct(String $userServiceKey)
{
$this->userServiceKey = $userServiceKey;
}
public function getHeaders(): array
{
return [
'X-Auth-User-Service-Key' => $this->userServiceKey,
];
}
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* User: junade
* Date: 13/01/2017
* Time: 23:35
*/
class GuzzleTest extends PHPUnit_Framework_TestCase
{
public function testGet()
{
$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');
}
}

24
tests/Auth/APIKeyTest.php Normal file
View File

@@ -0,0 +1,24 @@
<?php
/**
* User: junade
* Date: 13/01/2017
* Time: 17:15
*/
class APIKeyTest extends PHPUnit_Framework_TestCase
{
public function testGetHeaders()
{
$auth = new \Cloudflare\API\Auth\APIKey('example@example.com', '1234567893feefc5f0q5000bfo0c38d90bbeb');
$headers = $auth->getHeaders();
$this->assertArrayHasKey('X-Auth-Key', $headers);
$this->assertArrayHasKey('X-Auth-Email', $headers);
$this->assertEquals('example@example.com', $headers['X-Auth-Email']);
$this->assertEquals('1234567893feefc5f0q5000bfo0c38d90bbeb', $headers['X-Auth-Key']);
$this->assertEquals(2, sizeof($headers));
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* User: junade
* Date: 13/01/2017
* Time: 18:03
*/
class UserServiceKeyTest extends PHPUnit_Framework_TestCase
{
public function testGetHeaders()
{
$auth = new \Cloudflare\API\Auth\UserServiceKey("v1.0-e24fd090c02efcfecb4de8f4ff246fd5c75b48946fdf0ce26c59f91d0d90797b-cfa33fe60e8e34073c149323454383fc9005d25c9b4c502c2f063457ef65322eade065975001a0b4b4c591c5e1bd36a6e8f7e2d4fa8a9ec01c64c041e99530c2-07b9efe0acd78c82c8d9c690aacb8656d81c369246d7f996a205fe3c18e9254a");
$headers = $auth->getHeaders();
$this->assertArrayHasKey('X-Auth-User-Service-Key', $headers);
$this->assertEquals('v1.0-e24fd090c02efcfecb4de8f4ff246fd5c75b48946fdf0ce26c59f91d0d90797b-cfa33fe60e8e34073c149323454383fc9005d25c9b4c502c2f063457ef65322eade065975001a0b4b4c591c5e1bd36a6e8f7e2d4fa8a9ec01c64c041e99530c2-07b9efe0acd78c82c8d9c690aacb8656d81c369246d7f996a205fe3c18e9254a',
$headers['X-Auth-User-Service-Key']);
$this->assertEquals(1, sizeof($headers));
}
}