Skip to content

Commit 19a360e

Browse files
author
Jonathan Visser
committed
Add support for listing whitelist entries (GET /v2/whitelist/{app}/)
1 parent 4f4408d commit 19a360e

3 files changed

Lines changed: 114 additions & 0 deletions

File tree

src/HypernodeClient.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Hypernode\Api\Service\Logbook;
1717
use Hypernode\Api\Service\Product;
1818
use Hypernode\Api\Service\Settings;
19+
use Hypernode\Api\Service\Whitelist;
1920
use Psr\Http\Message\ResponseInterface;
2021

2122
class HypernodeClient
@@ -30,6 +31,7 @@ class HypernodeClient
3031
public Product $product;
3132
public Settings $settings;
3233
public Logbook $logbook;
34+
public Whitelist $whitelist;
3335

3436
public function __construct(HttpMethodsClientInterface $apiClient)
3537
{
@@ -43,6 +45,7 @@ public function __construct(HttpMethodsClientInterface $apiClient)
4345
$this->product = new Product($this);
4446
$this->settings = new Settings($this);
4547
$this->logbook = new Logbook($this);
48+
$this->whitelist = new Whitelist($this);
4649
}
4750

4851
public function getJsonFromResponse(ResponseInterface $response)

src/Service/Whitelist.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypernode\Api\Service;
6+
7+
use Hypernode\Api\Exception\HypernodeApiClientException;
8+
use Hypernode\Api\Exception\HypernodeApiServerException;
9+
10+
class Whitelist extends AbstractService
11+
{
12+
public const V2_WHITELIST_URL = "/v2/whitelist/%s/";
13+
14+
/**
15+
* Get all whitelist entries for the Hypernode.
16+
*
17+
* @param string $app Name of the Hypernode
18+
* @param array $params Query parameters to filter the results, e.g. ['type' => 'waf']
19+
* @return array List of whitelist entries
20+
* @throws HypernodeApiClientException
21+
* @throws HypernodeApiServerException
22+
*/
23+
public function getList(string $app, array $params = []): array
24+
{
25+
$url = sprintf(self::V2_WHITELIST_URL, $app);
26+
if ($params) {
27+
$url .= '?' . http_build_query($params);
28+
}
29+
30+
$response = $this->client->api->get($url);
31+
32+
$this->client->maybeThrowApiExceptions($response);
33+
34+
return $this->client->getJsonFromResponse($response);
35+
}
36+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypernode\Api\Service;
6+
7+
use GuzzleHttp\Psr7\Response;
8+
use Hypernode\Api\Exception\HypernodeApiClientException;
9+
use Hypernode\Api\Exception\HypernodeApiServerException;
10+
use Hypernode\Api\HypernodeClientTestCase;
11+
12+
class WhitelistTest extends HypernodeClientTestCase
13+
{
14+
public function testGetList()
15+
{
16+
$entries = [
17+
[
18+
'id' => 1234,
19+
'created' => '2026-07-14T14:49:06Z',
20+
'type' => 'waf',
21+
'description' => 'Office IP',
22+
'ip' => '1.2.3.4',
23+
'app' => 'johndoe',
24+
],
25+
];
26+
$this->responses->append(
27+
new Response(200, [], json_encode($entries)),
28+
);
29+
30+
$result = $this->client->whitelist->getList('johndoe');
31+
32+
$request = $this->responses->getLastRequest();
33+
$this->assertEquals('GET', $request->getMethod());
34+
$this->assertEquals('/v2/whitelist/johndoe/', $request->getUri());
35+
$this->assertEquals($entries, $result);
36+
}
37+
38+
public function testGetListAcceptsParams()
39+
{
40+
$this->responses->append(
41+
new Response(200, [], json_encode([])),
42+
);
43+
44+
$result = $this->client->whitelist->getList('johndoe', ['type' => 'waf']);
45+
46+
$request = $this->responses->getLastRequest();
47+
$this->assertEquals('GET', $request->getMethod());
48+
$this->assertEquals('/v2/whitelist/johndoe/?type=waf', $request->getUri());
49+
$this->assertEquals([], $result);
50+
}
51+
52+
public function testGetListRaisesClientExceptions()
53+
{
54+
$badRequestResponse = new Response(400, [], json_encode([
55+
'non_field_errors' => ['Your request was invalid.']
56+
]));
57+
$this->responses->append($badRequestResponse);
58+
59+
$this->expectExceptionObject(new HypernodeApiClientException($badRequestResponse));
60+
61+
$this->client->whitelist->getList('johndoe');
62+
}
63+
64+
public function testGetListRaisesServerExceptions()
65+
{
66+
$badRequestResponse = new Response(500, [], json_encode([
67+
'non_field_errors' => ['Something went wrong processing your request.']
68+
]));
69+
$this->responses->append($badRequestResponse);
70+
71+
$this->expectExceptionObject(new HypernodeApiServerException($badRequestResponse));
72+
73+
$this->client->whitelist->getList('johndoe');
74+
}
75+
}

0 commit comments

Comments
 (0)