Skip to content

Commit 251739c

Browse files
author
Jonathan Visser
committed
Add support for listing products (GET /v2/product/)
1 parent 5f297e4 commit 251739c

3 files changed

Lines changed: 105 additions & 0 deletions

File tree

src/HypernodeClient.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Hypernode\Api\Service\Configuration;
1515
use Hypernode\Api\Service\InsightsAnnotation;
1616
use Hypernode\Api\Service\Logbook;
17+
use Hypernode\Api\Service\Product;
1718
use Hypernode\Api\Service\Settings;
1819
use Psr\Http\Message\ResponseInterface;
1920

@@ -26,6 +27,7 @@ class HypernodeClient
2627
public BrancherApp $brancherApp;
2728
public Configuration $configuration;
2829
public InsightsAnnotation $insightsAnnotation;
30+
public Product $product;
2931
public Settings $settings;
3032
public Logbook $logbook;
3133

@@ -38,6 +40,7 @@ public function __construct(HttpMethodsClientInterface $apiClient)
3840
$this->brancherApp = new BrancherApp($this);
3941
$this->configuration = new Configuration($this);
4042
$this->insightsAnnotation = new InsightsAnnotation($this);
43+
$this->product = new Product($this);
4144
$this->settings = new Settings($this);
4245
$this->logbook = new Logbook($this);
4346
}

src/Service/Product.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 Product extends AbstractService
11+
{
12+
public const V2_PRODUCT_LIST_URL = "/v2/product/";
13+
14+
/**
15+
* Show all Hypernode products.
16+
*
17+
* @return array List of products
18+
* @throws HypernodeApiClientException
19+
* @throws HypernodeApiServerException
20+
*/
21+
public function getList(): array
22+
{
23+
$response = $this->client->api->get(self::V2_PRODUCT_LIST_URL);
24+
25+
$this->client->maybeThrowApiExceptions($response);
26+
27+
return $this->client->getJsonFromResponse($response);
28+
}
29+
}

tests/unit/Service/ProductTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 ProductTest extends HypernodeClientTestCase
13+
{
14+
public function testGetList()
15+
{
16+
$products = [
17+
[
18+
'code' => 'FALCON_XS_202203',
19+
'name' => 'Falcon XS',
20+
'backups_enabled' => true,
21+
'is_development' => false,
22+
'varnish_supported' => false,
23+
'supports_sla' => true,
24+
'price' => 1234,
25+
'currency' => 'EUR',
26+
],
27+
[
28+
'code' => 'FALCON_M_202203',
29+
'name' => 'Falcon M',
30+
'backups_enabled' => true,
31+
'is_development' => false,
32+
'varnish_supported' => true,
33+
'supports_sla' => true,
34+
'price' => 2345,
35+
'currency' => 'EUR',
36+
],
37+
];
38+
$this->responses->append(
39+
new Response(200, [], json_encode($products)),
40+
);
41+
42+
$result = $this->client->product->getList();
43+
44+
$request = $this->responses->getLastRequest();
45+
$this->assertEquals('GET', $request->getMethod());
46+
$this->assertEquals('/v2/product/', $request->getUri());
47+
$this->assertEquals($products, $result);
48+
}
49+
50+
public function testGetListRaisesClientExceptions()
51+
{
52+
$badRequestResponse = new Response(400, [], json_encode([
53+
'non_field_errors' => ['Your request was invalid.']
54+
]));
55+
$this->responses->append($badRequestResponse);
56+
57+
$this->expectExceptionObject(new HypernodeApiClientException($badRequestResponse));
58+
59+
$this->client->product->getList();
60+
}
61+
62+
public function testGetListRaisesServerExceptions()
63+
{
64+
$badRequestResponse = new Response(500, [], json_encode([
65+
'non_field_errors' => ['Something went wrong processing your request.']
66+
]));
67+
$this->responses->append($badRequestResponse);
68+
69+
$this->expectExceptionObject(new HypernodeApiServerException($badRequestResponse));
70+
71+
$this->client->product->getList();
72+
}
73+
}

0 commit comments

Comments
 (0)