Skip to content

Add list method to API #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Here's a list of Hypernode API features implemented in the client.
- Listing Hypernodes related to your API key
- Updating one or multiple Hypernode settings at once.
- Querying/polling the logbook for the status of a job.
- Creating and cancelling Brancher Hypernode instances.
- Listing, creating and cancelling Brancher Hypernode instances.

## Related projects

Expand Down
22 changes: 22 additions & 0 deletions src/Service/BrancherApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@

class BrancherApp extends AbstractService
{
/**
* List all brancher nodes for given parent app.
*
* @param string $app Name of the parent app
* @param array|null $data Extra data to be provided
* @return array Array containing brancher nodes
* @throws HypernodeApiClientException
* @throws HypernodeApiServerException
*/
public function list(string $app, ?array $data = null): array
{
$url = sprintf(App::V2_BRANCHER_APP_URL, $app);

$response = $this->client->api->get($url, [], $data ? json_encode($data) : null);

$this->client->maybeThrowApiExceptions($response);

$data = $this->client->getJsonFromResponse($response);

return $data['branchers'];
}

/**
* Create a brancher app for given parent app.
*
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/Service/BrancherAppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,49 @@

class BrancherAppTest extends HypernodeClientTestCase
{
public function testListBrancherApp()
{
$this->responses->append(
new Response(200, [], json_encode([
'name' => 'johndoe-eph123456',
'parent' => 'johndoe',
'type' => 'brancher',
'branchers' => []
])),
);

$branchers = $this->client->brancherApp->list('johndoe');

$request = $this->responses->getLastRequest();
$this->assertEquals('GET', $request->getMethod());
$this->assertEquals('/v2/brancher/app/johndoe/', $request->getUri());
$this->assertEquals([], $branchers);
}

public function testListBrancherAppRaisesClientExceptions()
{
$badRequestResponse = new Response(400, [], json_encode([
'non_field_errors' => ['Your request was invalid.']
]));
$this->responses->append($badRequestResponse);

$this->expectExceptionObject(new HypernodeApiClientException($badRequestResponse));

$this->client->brancherApp->list('johndoe');
}

public function testListBrancherAppRaisesServerExceptions()
{
$badRequestResponse = new Response(500, [], json_encode([
'non_field_errors' => ['Something went wrong processing your request.']
]));
$this->responses->append($badRequestResponse);

$this->expectExceptionObject(new HypernodeApiServerException($badRequestResponse));

$this->client->brancherApp->list('johndoe');
}

public function testCreateBrancherApp()
{
$this->responses->append(
Expand Down