Skip to content

Commit 4f4408d

Browse files
author
Jonathan Visser
committed
Add support for retrieving the current product (GET /v2/product/app/{app}/current/)
1 parent 251739c commit 4f4408d

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

src/Service/Product.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
class Product extends AbstractService
1111
{
1212
public const V2_PRODUCT_LIST_URL = "/v2/product/";
13+
public const V2_PRODUCT_APP_CURRENT_URL = "/v2/product/app/%s/current/";
1314

1415
/**
1516
* Show all Hypernode products.
@@ -26,4 +27,23 @@ public function getList(): array
2627

2728
return $this->client->getJsonFromResponse($response);
2829
}
30+
31+
/**
32+
* Show the current product for the Hypernode.
33+
*
34+
* @param string $app Name of the Hypernode
35+
* @return array The current product
36+
* @throws HypernodeApiClientException
37+
* @throws HypernodeApiServerException
38+
*/
39+
public function getCurrent(string $app): array
40+
{
41+
$url = sprintf(self::V2_PRODUCT_APP_CURRENT_URL, $app);
42+
43+
$response = $this->client->api->get($url);
44+
45+
$this->client->maybeThrowApiExceptions($response);
46+
47+
return $this->client->getJsonFromResponse($response);
48+
}
2949
}

tests/unit/Service/ProductTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,56 @@ public function testGetListRaisesServerExceptions()
7070

7171
$this->client->product->getList();
7272
}
73+
74+
public function testGetCurrent()
75+
{
76+
$product = [
77+
'code' => 'FALCON_S_202203',
78+
'name' => 'Falcon S',
79+
'backups_enabled' => true,
80+
'is_development' => false,
81+
'varnish_supported' => true,
82+
'supports_sla' => true,
83+
'price' => 1234,
84+
'currency' => 'EUR',
85+
'flavor' => [
86+
'name' => '2CPU/8GB/60GB (Falcon S 202202)',
87+
'redis_size' => '1024',
88+
],
89+
];
90+
$this->responses->append(
91+
new Response(200, [], json_encode($product)),
92+
);
93+
94+
$result = $this->client->product->getCurrent('johndoe');
95+
96+
$request = $this->responses->getLastRequest();
97+
$this->assertEquals('GET', $request->getMethod());
98+
$this->assertEquals('/v2/product/app/johndoe/current/', $request->getUri());
99+
$this->assertEquals($product, $result);
100+
}
101+
102+
public function testGetCurrentRaisesClientExceptions()
103+
{
104+
$badRequestResponse = new Response(400, [], json_encode([
105+
'non_field_errors' => ['Your request was invalid.']
106+
]));
107+
$this->responses->append($badRequestResponse);
108+
109+
$this->expectExceptionObject(new HypernodeApiClientException($badRequestResponse));
110+
111+
$this->client->product->getCurrent('johndoe');
112+
}
113+
114+
public function testGetCurrentRaisesServerExceptions()
115+
{
116+
$badRequestResponse = new Response(500, [], json_encode([
117+
'non_field_errors' => ['Something went wrong processing your request.']
118+
]));
119+
$this->responses->append($badRequestResponse);
120+
121+
$this->expectExceptionObject(new HypernodeApiServerException($badRequestResponse));
122+
123+
$this->client->product->getCurrent('johndoe');
124+
}
73125
}

0 commit comments

Comments
 (0)