Feature: add accounts end point, add get domains
This commit is contained in:
61
src/Endpoints/Accounts.php
Normal file
61
src/Endpoints/Accounts.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/**
|
||||
* User: kanasite
|
||||
* Date: 01/28/2019
|
||||
* Time: 10:00
|
||||
*/
|
||||
|
||||
namespace Cloudflare\API\Endpoints;
|
||||
|
||||
use Cloudflare\API\Adapter\Adapter;
|
||||
use Cloudflare\API\Traits\BodyAccessorTrait;
|
||||
|
||||
class Accounts implements API
|
||||
{
|
||||
use BodyAccessorTrait;
|
||||
|
||||
private $adapter;
|
||||
|
||||
public function __construct(Adapter $adapter)
|
||||
{
|
||||
$this->adapter = $adapter;
|
||||
}
|
||||
|
||||
public function listAccounts(
|
||||
int $page = 1,
|
||||
int $perPage = 20,
|
||||
string $direction = ''
|
||||
): \stdClass {
|
||||
$query = [
|
||||
'page' => $page,
|
||||
'per_page' => $perPage
|
||||
];
|
||||
|
||||
if (!empty($direction)) {
|
||||
$query['direction'] = $direction;
|
||||
}
|
||||
|
||||
$user = $this->adapter->get('accounts', $query);
|
||||
$this->body = json_decode($user->getBody());
|
||||
|
||||
return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info];
|
||||
}
|
||||
|
||||
public function getDomains(string $accountID): \stdClass
|
||||
{
|
||||
$response = $this->adapter->get('accounts/' . $accountID . '/registrar/domains');
|
||||
|
||||
$this->body = $response->getBody();
|
||||
|
||||
return json_decode($this->body)->result;
|
||||
}
|
||||
|
||||
public function getDomainDetails(string $accountID, string $domainName): \stdClass
|
||||
{
|
||||
$response = $this->adapter->get('accounts/' . $accountID . '/registrar/domains' . $domainName);
|
||||
|
||||
$this->body = $response->getBody();
|
||||
|
||||
return json_decode($this->body)->result;
|
||||
}
|
||||
}
|
||||
37
tests/Endpoints/AccountsTest.php
Normal file
37
tests/Endpoints/AccountsTest.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* User: kanasite
|
||||
* Date: 01/28/2019
|
||||
* Time: 10:00
|
||||
*/
|
||||
class AccountsTest extends TestCase
|
||||
{
|
||||
public function testListZones()
|
||||
{
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/listAccounts.json');
|
||||
|
||||
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
|
||||
$mock->method('get')->willReturn($response);
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('get')
|
||||
->with(
|
||||
$this->equalTo('accounts'),
|
||||
$this->equalTo([
|
||||
'page' => 1,
|
||||
'per_page' => 20,
|
||||
'direction' => 'desc',
|
||||
])
|
||||
);
|
||||
|
||||
$accounts = new \Cloudflare\API\Endpoints\Accounts($mock);
|
||||
$result = $accounts->listAccounts(1, 20, 'desc');
|
||||
|
||||
$this->assertObjectHasAttribute('result', $result);
|
||||
$this->assertObjectHasAttribute('result_info', $result);
|
||||
|
||||
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $result->result[0]->id);
|
||||
$this->assertEquals(1, $result->result_info->page);
|
||||
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $accounts->getBody()->result[0]->id);
|
||||
}
|
||||
}
|
||||
20
tests/Fixtures/Endpoints/listAccounts.json
Normal file
20
tests/Fixtures/Endpoints/listAccounts.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"success": true,
|
||||
"errors": [],
|
||||
"messages": [],
|
||||
"result": [
|
||||
{
|
||||
"id": "023e105f4ecef8ad9ca31a8372d0c353",
|
||||
"name": "Example Account",
|
||||
"settings": {
|
||||
"enforce_twofactor": true
|
||||
}
|
||||
}
|
||||
],
|
||||
"result_info": {
|
||||
"page": 1,
|
||||
"per_page": 20,
|
||||
"count": 1,
|
||||
"total_count": 2000
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user