Skip to content

Commit 93d8d97

Browse files
authored
Merge pull request #14 from peterjaap/master
Add list method to API
2 parents d0b79d0 + 0e0a902 commit 93d8d97

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Here's a list of Hypernode API features implemented in the client.
5050
- Listing Hypernodes related to your API key
5151
- Updating one or multiple Hypernode settings at once.
5252
- Querying/polling the logbook for the status of a job.
53-
- Creating and cancelling Brancher Hypernode instances.
53+
- Listing, creating and cancelling Brancher Hypernode instances.
5454

5555
## Related projects
5656

src/Service/BrancherApp.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@
99

1010
class BrancherApp extends AbstractService
1111
{
12+
/**
13+
* List all brancher nodes for given parent app.
14+
*
15+
* @param string $app Name of the parent app
16+
* @param array|null $data Extra data to be provided
17+
* @return array Array containing brancher nodes
18+
* @throws HypernodeApiClientException
19+
* @throws HypernodeApiServerException
20+
*/
21+
public function list(string $app, ?array $data = null): array
22+
{
23+
$url = sprintf(App::V2_BRANCHER_APP_URL, $app);
24+
25+
$response = $this->client->api->get($url, [], $data ? json_encode($data) : null);
26+
27+
$this->client->maybeThrowApiExceptions($response);
28+
29+
$data = $this->client->getJsonFromResponse($response);
30+
31+
return $data['branchers'];
32+
}
33+
1234
/**
1335
* Create a brancher app for given parent app.
1436
*

tests/unit/Service/BrancherAppTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,49 @@
1111

1212
class BrancherAppTest extends HypernodeClientTestCase
1313
{
14+
public function testListBrancherApp()
15+
{
16+
$this->responses->append(
17+
new Response(200, [], json_encode([
18+
'name' => 'johndoe-eph123456',
19+
'parent' => 'johndoe',
20+
'type' => 'brancher',
21+
'branchers' => []
22+
])),
23+
);
24+
25+
$branchers = $this->client->brancherApp->list('johndoe');
26+
27+
$request = $this->responses->getLastRequest();
28+
$this->assertEquals('GET', $request->getMethod());
29+
$this->assertEquals('/v2/brancher/app/johndoe/', $request->getUri());
30+
$this->assertEquals([], $branchers);
31+
}
32+
33+
public function testListBrancherAppRaisesClientExceptions()
34+
{
35+
$badRequestResponse = new Response(400, [], json_encode([
36+
'non_field_errors' => ['Your request was invalid.']
37+
]));
38+
$this->responses->append($badRequestResponse);
39+
40+
$this->expectExceptionObject(new HypernodeApiClientException($badRequestResponse));
41+
42+
$this->client->brancherApp->list('johndoe');
43+
}
44+
45+
public function testListBrancherAppRaisesServerExceptions()
46+
{
47+
$badRequestResponse = new Response(500, [], json_encode([
48+
'non_field_errors' => ['Something went wrong processing your request.']
49+
]));
50+
$this->responses->append($badRequestResponse);
51+
52+
$this->expectExceptionObject(new HypernodeApiServerException($badRequestResponse));
53+
54+
$this->client->brancherApp->list('johndoe');
55+
}
56+
1457
public function testCreateBrancherApp()
1558
{
1659
$this->responses->append(

0 commit comments

Comments
 (0)