Compare commits
13 Commits
feature/me
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| f4b9092175 | |||
|
|
7e89626b59 | ||
|
|
b2187e7b52 | ||
|
|
04e2032e9f | ||
|
|
7a839dfc2a | ||
|
|
10e3e120e4 | ||
|
|
369160682d | ||
|
|
66e2ba45ca | ||
|
|
9a5995970b | ||
|
|
c289ef2ef7 | ||
|
|
1c97befdd8 | ||
|
|
0f602b563b | ||
|
|
aa10bcba3c |
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"name": "shellrent/cloudflare-php",
|
||||
"name": "yuemi/cloudflare-php",
|
||||
"description": "PHP binding for v4 of the Cloudflare Client API.",
|
||||
"type": "library",
|
||||
"require": {
|
||||
"guzzlehttp/guzzle": "^7.0.1",
|
||||
"php": ">=7.2.5",
|
||||
"psr/http-message": "~1.0",
|
||||
"psr/http-message": "^1.0 || ^2.0",
|
||||
"ext-json": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
|
||||
117
src/Endpoints/RulesLists.php
Executable file
117
src/Endpoints/RulesLists.php
Executable file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace Cloudflare\API\Endpoints;
|
||||
|
||||
use Cloudflare\API\Adapter\Adapter;
|
||||
use Cloudflare\API\Traits\BodyAccessorTrait;
|
||||
|
||||
class RulesLists implements API
|
||||
{
|
||||
use BodyAccessorTrait;
|
||||
|
||||
private $adapter;
|
||||
|
||||
public function __construct(Adapter $adapter)
|
||||
{
|
||||
$this->adapter = $adapter;
|
||||
}
|
||||
|
||||
public function getLists(string $accountId)
|
||||
{
|
||||
$response = $this->adapter->get('accounts/' . $accountId . '/rules/lists');
|
||||
$this->body = json_decode($response->getBody());
|
||||
|
||||
return $this->body->result;
|
||||
}
|
||||
|
||||
public function getListDetails(string $accountId, string $listId)
|
||||
{
|
||||
$response = $this->adapter->get('accounts/' . $accountId . '/rules/lists/' . $listId);
|
||||
$this->body = json_decode($response->getBody());
|
||||
|
||||
return $this->body->result;
|
||||
}
|
||||
|
||||
public function getListItems(string $accountId, string $listId, string $search = '', int $itemsPerPage = 20, string $cursor = '')
|
||||
{
|
||||
|
||||
$options = [
|
||||
'per_page' => $itemsPerPage,
|
||||
];
|
||||
|
||||
if ($search) {
|
||||
$options['search'] = $search;
|
||||
}
|
||||
|
||||
if ($cursor) {
|
||||
$options['cursor'] = $cursor;
|
||||
}
|
||||
|
||||
$response = $this->adapter->get('accounts/' . $accountId . '/rules/lists/' . $listId . '/items', $options);
|
||||
$this->body = json_decode($response->getBody());
|
||||
|
||||
return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info ?? null];
|
||||
}
|
||||
|
||||
public function createList(string $accountId, string $kind, string $name, string $description = '')
|
||||
{
|
||||
$options = [
|
||||
'kind' => $kind,
|
||||
'name' => $name,
|
||||
];
|
||||
|
||||
if ($description) {
|
||||
$options['description'] = $description;
|
||||
}
|
||||
|
||||
$response = $this->adapter->post('accounts/' . $accountId . '/rules/lists', $options);
|
||||
$this->body = json_decode($response->getBody());
|
||||
|
||||
return $this->body->result;
|
||||
}
|
||||
|
||||
public function deleteList(string $accountId, string $listId)
|
||||
{
|
||||
|
||||
$response = $this->adapter->delete('accounts/' . $accountId . '/rules/lists/' . $listId);
|
||||
$this->body = json_decode($response->getBody());
|
||||
|
||||
return $this->body->result;
|
||||
}
|
||||
|
||||
public function createListItem(string $accountId, string $listId, array $ip)
|
||||
{
|
||||
$options = [];
|
||||
foreach ($ip as $ipAddress) {
|
||||
$options[] = ['ip' => $ipAddress];
|
||||
}
|
||||
|
||||
$response = $this->adapter->post('accounts/' . $accountId . '/rules/lists/' . $listId . '/items', $options);
|
||||
$this->body = json_decode($response->getBody());
|
||||
|
||||
return $this->body->result;
|
||||
}
|
||||
|
||||
public function deleteListItem(string $accountId, string $listId, array $itemIds)
|
||||
{
|
||||
|
||||
$options = ['items' => []];
|
||||
foreach ($itemIds as $itemId) {
|
||||
$options['items'][] = ['id' => $itemId];
|
||||
}
|
||||
|
||||
$response = $this->adapter->delete('accounts/' . $accountId . '/rules/lists/' . $listId . '/items', $options);
|
||||
$this->body = json_decode($response->getBody());
|
||||
|
||||
return $this->body->result;
|
||||
}
|
||||
|
||||
public function getOperationStatus(string $accountId, string $operationId)
|
||||
{
|
||||
$response = $this->adapter->get('accounts/' . $accountId . '/rules/lists/bulk_operations/' . $operationId);
|
||||
$this->body = json_decode($response->getBody());
|
||||
|
||||
return $this->body->result;
|
||||
}
|
||||
|
||||
}
|
||||
191
tests/Endpoints/RulesListsTest.php
Executable file
191
tests/Endpoints/RulesListsTest.php
Executable file
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
namespace Endpoints;
|
||||
|
||||
use TestCase;
|
||||
|
||||
class RulesListsTest extends TestCase
|
||||
{
|
||||
|
||||
public function testCreateRulesList()
|
||||
{
|
||||
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/createRulesList.json');
|
||||
|
||||
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
|
||||
$mock->method('post')->willReturn($response);
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('post')
|
||||
->with(
|
||||
$this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/rules/lists'),
|
||||
$this->equalTo([
|
||||
'kind' => 'ip',
|
||||
'name' => 'ip-allowlist',
|
||||
])
|
||||
);
|
||||
|
||||
$rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock);
|
||||
$result = $rulesLists->createList('01a7362d577a6c3019a474fd6f485823', 'ip', 'ip-allowlist');
|
||||
|
||||
$this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $result->id);
|
||||
$this->assertEquals('ip', $result->kind);
|
||||
$this->assertEquals('ip-allowlist', $result->name);
|
||||
|
||||
$this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $rulesLists->getBody()->result->id);
|
||||
}
|
||||
|
||||
public function testDeleteRulesList()
|
||||
{
|
||||
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteRulesList.json');
|
||||
|
||||
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
|
||||
$mock->method('delete')->willReturn($response);
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('delete')
|
||||
->with(
|
||||
$this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e')
|
||||
);
|
||||
|
||||
$rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock);
|
||||
$result = $rulesLists->deleteList('01a7362d577a6c3019a474fd6f485823', '2c0fc9fa937b11eaa1b71c4d701ab86e');
|
||||
|
||||
$this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $result->id);
|
||||
$this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $rulesLists->getBody()->result->id);
|
||||
}
|
||||
|
||||
public function testGetRulesLists()
|
||||
{
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/listRulesLists.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/01a7362d577a6c3019a474fd6f485823/rules/lists')
|
||||
);
|
||||
|
||||
$rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock);
|
||||
$result = $rulesLists->getLists('01a7362d577a6c3019a474fd6f485823');
|
||||
|
||||
$this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $result[0]->id);
|
||||
$this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $rulesLists->getBody()->result[0]->id);
|
||||
}
|
||||
|
||||
public function testGetRulesListDetails()
|
||||
{
|
||||
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/getRulesListDetails.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/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e')
|
||||
);
|
||||
|
||||
$rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock);
|
||||
$result = $rulesLists->getListDetails('01a7362d577a6c3019a474fd6f485823', '2c0fc9fa937b11eaa1b71c4d701ab86e');
|
||||
|
||||
$this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $result->id);
|
||||
$this->assertEquals('ip', $result->kind);
|
||||
|
||||
$this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $rulesLists->getBody()->result->id);
|
||||
}
|
||||
|
||||
public function testGetRulesListItems()
|
||||
{
|
||||
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/getRulesListItems.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/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e/items'),
|
||||
$this->equalTo([
|
||||
'per_page' => 20,
|
||||
])
|
||||
);
|
||||
|
||||
$rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock);
|
||||
$result = $rulesLists->getListItems('01a7362d577a6c3019a474fd6f485823', '2c0fc9fa937b11eaa1b71c4d701ab86e');
|
||||
|
||||
$this->assertObjectHasAttribute('result', $result);
|
||||
$this->assertObjectHasAttribute('result_info', $result);
|
||||
|
||||
$this->assertEquals('10.0.0.1', $result->result[0]);
|
||||
$this->assertEquals('10.0.0.1', $rulesLists->getBody()->result[0]);
|
||||
}
|
||||
|
||||
public function testCreateRulesListItem() {
|
||||
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/createRulesListItem.json');
|
||||
|
||||
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
|
||||
$mock->method('post')->willReturn($response);
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('post')
|
||||
->with(
|
||||
$this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e/items')
|
||||
);
|
||||
|
||||
$rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock);
|
||||
$result = $rulesLists->createListItem('01a7362d577a6c3019a474fd6f485823', '2c0fc9fa937b11eaa1b71c4d701ab86e', [
|
||||
'10.0.0.1'
|
||||
]);
|
||||
|
||||
$this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $result->operation_id);
|
||||
$this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $rulesLists->getBody()->result->operation_id);
|
||||
}
|
||||
|
||||
public function testDeleteRulesListItem() {
|
||||
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteRulesListItem.json');
|
||||
|
||||
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
|
||||
$mock->method('delete')->willReturn($response);
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('delete')
|
||||
->with(
|
||||
$this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e/items')
|
||||
);
|
||||
|
||||
$rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock);
|
||||
$result = $rulesLists->deleteListItem('01a7362d577a6c3019a474fd6f485823', '2c0fc9fa937b11eaa1b71c4d701ab86e', ['6as9450mma215q6so7p79dd981r4ee09']);
|
||||
|
||||
$this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $result->operation_id);
|
||||
$this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $rulesLists->getBody()->result->operation_id);
|
||||
}
|
||||
|
||||
public function testGetOperationStatus() {
|
||||
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/getOperationStatus.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/01a7362d577a6c3019a474fd6f485823/rules/lists/bulk_operations/4da8780eeb215e6cb7f48dd981c4ea02')
|
||||
);
|
||||
|
||||
$rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock);
|
||||
$result = $rulesLists->getOperationStatus('01a7362d577a6c3019a474fd6f485823', '4da8780eeb215e6cb7f48dd981c4ea02');
|
||||
|
||||
$this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $result->id);
|
||||
$this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $rulesLists->getBody()->result->id);
|
||||
}
|
||||
|
||||
}
|
||||
25
tests/Fixtures/Endpoints/createRulesList.json
Executable file
25
tests/Fixtures/Endpoints/createRulesList.json
Executable file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"success": true,
|
||||
"errors": [
|
||||
{
|
||||
"code": 1000,
|
||||
"message": "message"
|
||||
}
|
||||
],
|
||||
"messages": [
|
||||
{
|
||||
"code": 1000,
|
||||
"message": "message"
|
||||
}
|
||||
],
|
||||
"result": {
|
||||
"id": "2c0fc9fa937b11eaa1b71c4d701ab86e",
|
||||
"created_on": "2020-01-01T08:00:00Z",
|
||||
"description": "",
|
||||
"kind": "ip",
|
||||
"modified_on": "2020-01-10T14:00:00Z",
|
||||
"name": "ip-allowlist",
|
||||
"num_items": 10,
|
||||
"num_referencing_filters": 2
|
||||
}
|
||||
}
|
||||
18
tests/Fixtures/Endpoints/createRulesListItem.json
Executable file
18
tests/Fixtures/Endpoints/createRulesListItem.json
Executable file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"errors": [
|
||||
{
|
||||
"code": 1000,
|
||||
"message": "message"
|
||||
}
|
||||
],
|
||||
"messages": [
|
||||
{
|
||||
"code": 1000,
|
||||
"message": "message"
|
||||
}
|
||||
],
|
||||
"result": {
|
||||
"operation_id": "4da8780eeb215e6cb7f48dd981c4ea02"
|
||||
},
|
||||
"success": true
|
||||
}
|
||||
18
tests/Fixtures/Endpoints/deleteRulesList.json
Executable file
18
tests/Fixtures/Endpoints/deleteRulesList.json
Executable file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"errors": [
|
||||
{
|
||||
"code": 1000,
|
||||
"message": "message"
|
||||
}
|
||||
],
|
||||
"messages": [
|
||||
{
|
||||
"code": 1000,
|
||||
"message": "message"
|
||||
}
|
||||
],
|
||||
"result": {
|
||||
"id": "2c0fc9fa937b11eaa1b71c4d701ab86e"
|
||||
},
|
||||
"success": true
|
||||
}
|
||||
18
tests/Fixtures/Endpoints/deleteRulesListItem.json
Executable file
18
tests/Fixtures/Endpoints/deleteRulesListItem.json
Executable file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"success": true,
|
||||
"errors": [
|
||||
{
|
||||
"code": 1000,
|
||||
"message": "message"
|
||||
}
|
||||
],
|
||||
"messages": [
|
||||
{
|
||||
"code": 1000,
|
||||
"message": "message"
|
||||
}
|
||||
],
|
||||
"result": {
|
||||
"operation_id": "4da8780eeb215e6cb7f48dd981c4ea02"
|
||||
}
|
||||
}
|
||||
21
tests/Fixtures/Endpoints/getOperationStatus.json
Executable file
21
tests/Fixtures/Endpoints/getOperationStatus.json
Executable file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"errors": [
|
||||
{
|
||||
"code": 1000,
|
||||
"message": "message"
|
||||
}
|
||||
],
|
||||
"messages": [
|
||||
{
|
||||
"code": 1000,
|
||||
"message": "message"
|
||||
}
|
||||
],
|
||||
"result": {
|
||||
"id": "4da8780eeb215e6cb7f48dd981c4ea02",
|
||||
"status": "pending",
|
||||
"completed": "2020-01-01T08:00:00Z",
|
||||
"error": "This list is at the maximum number of items"
|
||||
},
|
||||
"success": true
|
||||
}
|
||||
25
tests/Fixtures/Endpoints/getRulesListDetails.json
Executable file
25
tests/Fixtures/Endpoints/getRulesListDetails.json
Executable file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"success": true,
|
||||
"errors": [
|
||||
{
|
||||
"code": 1000,
|
||||
"message": "message"
|
||||
}
|
||||
],
|
||||
"messages": [
|
||||
{
|
||||
"code": 1000,
|
||||
"message": "message"
|
||||
}
|
||||
],
|
||||
"result": {
|
||||
"id": "2c0fc9fa937b11eaa1b71c4d701ab86e",
|
||||
"created_on": "2020-01-01T08:00:00Z",
|
||||
"description": "This is a note",
|
||||
"kind": "ip",
|
||||
"modified_on": "2020-01-10T14:00:00Z",
|
||||
"name": "list1",
|
||||
"num_items": 10,
|
||||
"num_referencing_filters": 2
|
||||
}
|
||||
}
|
||||
24
tests/Fixtures/Endpoints/getRulesListItems.json
Executable file
24
tests/Fixtures/Endpoints/getRulesListItems.json
Executable file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"success": true,
|
||||
"errors": [
|
||||
{
|
||||
"code": 1000,
|
||||
"message": "message"
|
||||
}
|
||||
],
|
||||
"messages": [
|
||||
{
|
||||
"code": 1000,
|
||||
"message": "message"
|
||||
}
|
||||
],
|
||||
"result": [
|
||||
"10.0.0.1"
|
||||
],
|
||||
"result_info": {
|
||||
"cursors": {
|
||||
"after": "yyy",
|
||||
"before": "xxx"
|
||||
}
|
||||
}
|
||||
}
|
||||
27
tests/Fixtures/Endpoints/listRulesLists.json
Executable file
27
tests/Fixtures/Endpoints/listRulesLists.json
Executable file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"success": true,
|
||||
"errors": [
|
||||
{
|
||||
"code": 1000,
|
||||
"message": "message"
|
||||
}
|
||||
],
|
||||
"messages": [
|
||||
{
|
||||
"code": 1000,
|
||||
"message": "message"
|
||||
}
|
||||
],
|
||||
"result": [
|
||||
{
|
||||
"id": "2c0fc9fa937b11eaa1b71c4d701ab86e",
|
||||
"created_on": "2020-01-01T08:00:00Z",
|
||||
"kind": "ip",
|
||||
"modified_on": "2020-01-10T14:00:00Z",
|
||||
"name": "list1",
|
||||
"num_items": 10,
|
||||
"description": "This is a note",
|
||||
"num_referencing_filters": 2
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user