13 Commits

Author SHA1 Message Date
f4b9092175 Update composer.json 2025-07-25 19:13:35 +07:00
SVDIGITAL
7e89626b59 Merge pull request #3 from Edo-1234/rule-lists-implementation
fix api
2025-02-24 09:53:57 +01:00
Edoardo Zuliani
b2187e7b52 fix test 2025-01-02 15:42:37 +01:00
Edoardo Zuliani
04e2032e9f fix api eliminazione lista 2025-01-02 15:05:36 +01:00
Edoardo Zuliani
7a839dfc2a api eliminazione lista 2025-01-02 14:57:34 +01:00
Edoardo Zuliani
10e3e120e4 api eliminazione lista 2025-01-02 14:54:11 +01:00
Edoardo Zuliani
369160682d fix api 2025-01-02 14:07:00 +01:00
Edoardo Zuliani
66e2ba45ca fix api 2025-01-02 14:04:52 +01:00
Edoardo Zuliani
9a5995970b fix api 2025-01-02 14:01:47 +01:00
Edoardo Zuliani
c289ef2ef7 fix api 2025-01-02 12:51:48 +01:00
SVDIGITAL
1c97befdd8 Merge pull request #2 from Edo-1234/rule-lists-implementation
Implementazione api relative alle rule lists
2024-12-31 10:29:22 +01:00
Edoardo Zuliani
0f602b563b modifiche 2024-12-27 18:09:26 +01:00
SVDIGITAL
aa10bcba3c Merge pull request #1 from shellrent/feature/merge-upstream
Merge cloudflare-php upstream
2022-01-05 15:02:44 +01:00
11 changed files with 486 additions and 2 deletions

View File

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

View 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);
}
}

View 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
}
}

View File

@@ -0,0 +1,18 @@
{
"errors": [
{
"code": 1000,
"message": "message"
}
],
"messages": [
{
"code": 1000,
"message": "message"
}
],
"result": {
"operation_id": "4da8780eeb215e6cb7f48dd981c4ea02"
},
"success": true
}

View File

@@ -0,0 +1,18 @@
{
"errors": [
{
"code": 1000,
"message": "message"
}
],
"messages": [
{
"code": 1000,
"message": "message"
}
],
"result": {
"id": "2c0fc9fa937b11eaa1b71c4d701ab86e"
},
"success": true
}

View File

@@ -0,0 +1,18 @@
{
"success": true,
"errors": [
{
"code": 1000,
"message": "message"
}
],
"messages": [
{
"code": 1000,
"message": "message"
}
],
"result": {
"operation_id": "4da8780eeb215e6cb7f48dd981c4ea02"
}
}

View 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
}

View 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
}
}

View 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"
}
}
}

View 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
}
]
}