Finished user endpoints, started zone ones.

This commit is contained in:
Junade Ali
2017-06-06 17:05:21 +01:00
parent 5fcb00153b
commit bf8716e0b8
14 changed files with 957 additions and 312 deletions

View File

@@ -25,8 +25,8 @@ class Guzzle implements Adapter
$this->client = new Client([
'base_uri' => $baseURI,
'headers' => $headers,
'Accept' => 'application/json'
'headers' => $headers,
'Accept' => 'application/json'
]);
}
@@ -38,6 +38,7 @@ class Guzzle implements Adapter
{
$response = $this->client->get($uri, ['headers' => $headers]);
$this->checkError($response);
return $response;
}
@@ -48,11 +49,12 @@ class Guzzle implements Adapter
public function post(String $uri, array $headers = array(), array $body = array()): ResponseInterface
{
$response = $this->client->post($uri, [
'headers' => $headers,
'headers' => $headers,
'form_params' => $body
]
);
$this->checkError($response);
return $response;
}
@@ -65,10 +67,11 @@ class Guzzle implements Adapter
$response = $this->client->put($uri, [
'headers' => $headers,
'body' => $jsonBody
'body' => $jsonBody
]
);
$this->checkError($response);
return $response;
}
@@ -81,10 +84,11 @@ class Guzzle implements Adapter
$response = $this->client->patch($uri, [
'headers' => $headers,
'body' => $jsonBody
'body' => $jsonBody
]
);
$this->checkError($response);
return $response;
}
@@ -94,11 +98,31 @@ class Guzzle implements Adapter
public function delete(String $uri, array $headers = array(), array $body = array()): ResponseInterface
{
$response = $this->client->delete($uri, [
'headers' => $headers,
'headers' => $headers,
'form_params' => $body
]
);
$this->checkError($response);
return $response;
}
private function checkError(ResponseInterface $response)
{
$json = json_decode($response->getBody());
if (json_last_error() !== JSON_ERROR_NONE) {
throw new JSONException();
}
if (isset($json->errors)) {
foreach ($json->errors as $error) {
throw new ResponseException($error->message, $error->code);
}
}
if (isset($json->success) && ($json->success === false)) {
throw new ResponseException("Request was unsuccessful.");
}
}
}

View File

@@ -0,0 +1,15 @@
<?php
/**
* Created by PhpStorm.
* User: junade
* Date: 21/04/2017
* Time: 06:52
*/
namespace Cloudflare\API\Adapter;
class JSONException extends \Exception
{
}

View File

@@ -0,0 +1,15 @@
<?php
/**
* Created by PhpStorm.
* User: junade
* Date: 21/04/2017
* Time: 07:23
*/
namespace Cloudflare\API\Adapter;
class ResponseException extends \Exception
{
}

View File

@@ -0,0 +1,15 @@
<?php
/**
* Created by PhpStorm.
* User: junade
* Date: 06/06/2017
* Time: 14:24
*/
namespace Cloudflare\API\Endpoints;
class EndpointException extends \Exception
{
}

View File

@@ -19,10 +19,26 @@ class User implements API
$this->adapter = $adapter;
}
public function getUserDetails()
public function getUserDetails(): \stdClass
{
$user = $this->adapter->get('user', []);
$body = json_decode($user->getBody());
var_dump($body);
return $body->result;
}
public function getUserID(): string
{
return ($this->getUserDetails())->id;
}
public function getUserEmail(): string
{
return ($this->getUserDetails())->email;
}
public function updateUserDetails(array $details): \stdClass
{
$response = $this->adapter->patch("user", [], $details);
return json_decode($response->getBody());
}
}

39
src/Endpoints/Zones.php Normal file
View File

@@ -0,0 +1,39 @@
<?php
/**
* Created by PhpStorm.
* User: junade
* Date: 06/06/2017
* Time: 15:45
*/
namespace Cloudflare\API\Endpoints;
use Cloudflare\API\Adapter\Adapter;
class Zones implements API
{
private $adapter;
public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
}
public function addZone(string $name, bool $jumpstart = false, string $organizationID = ''): \stdClass
{
$options = [
'name' => $name,
'jumpstart' => $jumpstart
];
if (!empty($organizationID)) {
$organization = new \stdClass();
$organization->id = $organizationID;
$options["organization"] = $organization;
}
$user = $this->adapter->post('zones', [], $options);
$body = json_decode($user->getBody());
return $body->result;
}
}