Finished Guzzle implementation, started work on Endpoints.

This commit is contained in:
Junade Ali
2017-02-01 18:07:33 +00:00
parent a49c655e77
commit 5fcb00153b
15 changed files with 912 additions and 44 deletions

View File

@@ -1,26 +0,0 @@
<?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()
{
}
}

View File

@@ -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;
}

View File

@@ -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;
}
}

16
src/Endpoints/API.php Normal file
View File

@@ -0,0 +1,16 @@
<?php
/**
* User: junade
* Date: 01/02/2017
* Time: 12:31
*/
namespace Cloudflare\API\Endpoints;
use Cloudflare\API\Adapter\Adapter;
interface API
{
public function __construct(Adapter $adapter);
}

28
src/Endpoints/User.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
/**
* User: junade
* Date: 01/02/2017
* Time: 12:30
*/
namespace Cloudflare\API\Endpoints;
use Cloudflare\API\Adapter\Adapter;
class User implements API
{
private $adapter;
public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
}
public function getUserDetails()
{
$user = $this->adapter->get('user', []);
$body = json_decode($user->getBody());
var_dump($body);
}
}