Skip to content

Commit 99e8fc2

Browse files
author
Jonathan Visser
committed
Add support for listing Insights annotations (GET /v2/insights-annotation/)
1 parent ff789cb commit 99e8fc2

3 files changed

Lines changed: 178 additions & 0 deletions

File tree

src/HypernodeClient.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Hypernode\Api\Service\Backup;
1313
use Hypernode\Api\Service\BrancherApp;
1414
use Hypernode\Api\Service\Configuration;
15+
use Hypernode\Api\Service\InsightsAnnotation;
1516
use Hypernode\Api\Service\Logbook;
1617
use Hypernode\Api\Service\Settings;
1718
use Psr\Http\Message\ResponseInterface;
@@ -24,6 +25,7 @@ class HypernodeClient
2425
public Backup $backup;
2526
public BrancherApp $brancherApp;
2627
public Configuration $configuration;
28+
public InsightsAnnotation $insightsAnnotation;
2729
public Settings $settings;
2830
public Logbook $logbook;
2931

@@ -35,6 +37,7 @@ public function __construct(HttpMethodsClientInterface $apiClient)
3537
$this->backup = new Backup($this);
3638
$this->brancherApp = new BrancherApp($this);
3739
$this->configuration = new Configuration($this);
40+
$this->insightsAnnotation = new InsightsAnnotation($this);
3841
$this->settings = new Settings($this);
3942
$this->logbook = new Logbook($this);
4043
}

src/Service/InsightsAnnotation.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 InsightsAnnotation extends AbstractService
11+
{
12+
public const V2_INSIGHTS_ANNOTATION_LIST_URL = "/v2/insights-annotation/";
13+
14+
/**
15+
* List all custom Insights annotations for the Hypernodes you have access
16+
* to. Only annotations created through the API are listed; system
17+
* annotations generated from platform events are not included.
18+
*
19+
* @param array $params Query parameters, e.g. limit and offset
20+
* @return array List of Insights annotations
21+
* @throws HypernodeApiClientException
22+
* @throws HypernodeApiServerException
23+
*/
24+
public function getList(array $params = []): array
25+
{
26+
$annotations = [];
27+
28+
$requestUrl = self::V2_INSIGHTS_ANNOTATION_LIST_URL;
29+
if ($params) {
30+
$requestUrl .= '?' . http_build_query($params);
31+
}
32+
33+
while ($requestUrl) {
34+
$response = $this->client->api->get($requestUrl);
35+
36+
$this->client->maybeThrowApiExceptions($response);
37+
$data = $this->client->getJsonFromResponse($response);
38+
39+
foreach ($data['results'] as $item) {
40+
$annotations[] = $item;
41+
}
42+
43+
$requestUrl = $data['next'];
44+
}
45+
46+
return $annotations;
47+
}
48+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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 InsightsAnnotationTest extends HypernodeClientTestCase
13+
{
14+
public function testGetList()
15+
{
16+
$annotations = [
17+
[
18+
'name' => 'Deployed release 1.2.3',
19+
'x_axis' => 1756384957,
20+
'app_name' => 'johndoe',
21+
'metrics_list' => ['memory_usage'],
22+
'metadata' => [],
23+
],
24+
];
25+
$this->responses->append(
26+
new Response(200, [], json_encode([
27+
'count' => 1,
28+
'next' => null,
29+
'previous' => null,
30+
'results' => $annotations,
31+
])),
32+
);
33+
34+
$result = $this->client->insightsAnnotation->getList();
35+
36+
$request = $this->responses->getLastRequest();
37+
$this->assertEquals('GET', $request->getMethod());
38+
$this->assertEquals('/v2/insights-annotation/', $request->getUri());
39+
$this->assertEquals($annotations, $result);
40+
}
41+
42+
public function testGetListAcceptsParams()
43+
{
44+
$this->responses->append(
45+
new Response(200, [], json_encode([
46+
'count' => 0,
47+
'next' => null,
48+
'previous' => null,
49+
'results' => [],
50+
])),
51+
);
52+
53+
$result = $this->client->insightsAnnotation->getList(['limit' => 10, 'offset' => 20]);
54+
55+
$request = $this->responses->getLastRequest();
56+
$this->assertEquals('GET', $request->getMethod());
57+
$this->assertEquals('/v2/insights-annotation/?limit=10&offset=20', $request->getUri());
58+
$this->assertEquals([], $result);
59+
}
60+
61+
public function testGetListPaginates()
62+
{
63+
$this->responses->append(
64+
new Response(200, [], json_encode([
65+
'count' => 2,
66+
'next' => 'https://api.hypernode.com/v2/insights-annotation/?limit=1&offset=1',
67+
'previous' => null,
68+
'results' => [
69+
[
70+
'name' => 'Deployed release 1.2.3',
71+
'x_axis' => 1756384957,
72+
'app_name' => 'johndoe',
73+
],
74+
],
75+
])),
76+
new Response(200, [], json_encode([
77+
'count' => 2,
78+
'next' => null,
79+
'previous' => null,
80+
'results' => [
81+
[
82+
'name' => 'Deployed release 1.2.4',
83+
'x_axis' => 1756395957,
84+
'app_name' => 'johndoe',
85+
],
86+
],
87+
])),
88+
);
89+
90+
$result = $this->client->insightsAnnotation->getList();
91+
92+
$this->assertCount(2, $result);
93+
$this->assertEquals('Deployed release 1.2.3', $result[0]['name']);
94+
$this->assertEquals('Deployed release 1.2.4', $result[1]['name']);
95+
96+
$request = $this->responses->getLastRequest();
97+
$this->assertEquals('GET', $request->getMethod());
98+
$this->assertEquals(
99+
'https://api.hypernode.com/v2/insights-annotation/?limit=1&offset=1',
100+
$request->getUri()
101+
);
102+
}
103+
104+
public function testGetListRaisesClientExceptions()
105+
{
106+
$badRequestResponse = new Response(400, [], json_encode([
107+
'non_field_errors' => ['Your request was invalid.']
108+
]));
109+
$this->responses->append($badRequestResponse);
110+
111+
$this->expectExceptionObject(new HypernodeApiClientException($badRequestResponse));
112+
113+
$this->client->insightsAnnotation->getList();
114+
}
115+
116+
public function testGetListRaisesServerExceptions()
117+
{
118+
$badRequestResponse = new Response(500, [], json_encode([
119+
'non_field_errors' => ['Something went wrong processing your request.']
120+
]));
121+
$this->responses->append($badRequestResponse);
122+
123+
$this->expectExceptionObject(new HypernodeApiServerException($badRequestResponse));
124+
125+
$this->client->insightsAnnotation->getList();
126+
}
127+
}

0 commit comments

Comments
 (0)