From 3cf3954fd3e2cae879eedc28ac6ad90a45132bc1 Mon Sep 17 00:00:00 2001 From: Rick van de Loo Date: Wed, 7 Dec 2022 11:38:18 +0100 Subject: [PATCH 1/8] link related projects in readme Convenient if we point to https://github.com/byteinternet/hypernode-api-python as well in this project in case people are looking for the Python client instead of the PHP client. --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 5886086..baa8c83 100644 --- a/README.md +++ b/README.md @@ -50,3 +50,9 @@ Here's a list of Hypernode API features implemented in the client. - Updating one or multiple Hypernode settings at once. - Querying/polling the logbook for the status of a job. - Creating and cancelling Brancher Hypernode instances. + +## Related projects + +- The official [Hypernode API Python Client](https://github.com/byteinternet/hypernode-api-python) +- The official [Hypernode Deploy](https://github.com/byteinternet/hypernode-deploy-configuration) tool +- The official [Hypernode Docker](https://github.com/byteinternet/hypernode-docker) image From 256a237d63d51e919991ed78db964700f8234549 Mon Sep 17 00:00:00 2001 From: Timon de Groot Date: Tue, 6 Dec 2022 15:10:54 +0100 Subject: [PATCH 2/8] HypernodeClientFactory: Use composer runtime API to get client version --- composer.json | 1 + src/HypernodeClient.php | 2 -- src/HypernodeClientFactory.php | 6 +++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 9a0ef24..ff00b0a 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ } ], "require": { + "composer-runtime-api": "^2.0", "ext-curl": "*", "ext-json": "*", "nesbot/carbon": "^2.0", diff --git a/src/HypernodeClient.php b/src/HypernodeClient.php index 629cb56..0774f76 100644 --- a/src/HypernodeClient.php +++ b/src/HypernodeClient.php @@ -15,8 +15,6 @@ class HypernodeClient { - public const VERSION = '0.1.0'; - public HttpMethodsClientInterface $api; public App $app; public BrancherApp $brancherApp; diff --git a/src/HypernodeClientFactory.php b/src/HypernodeClientFactory.php index dfda8ca..e25be4b 100644 --- a/src/HypernodeClientFactory.php +++ b/src/HypernodeClientFactory.php @@ -4,6 +4,7 @@ namespace Hypernode\Api; +use Composer\InstalledVersions; use Http\Client\Common\HttpMethodsClient; use Http\Client\Common\Plugin\AddHostPlugin; use Http\Client\Common\Plugin\AddPathPlugin; @@ -21,7 +22,10 @@ public static function create(string $authToken, ?ClientInterface $httpClient = { $httpHeaders = [ 'Authorization' => sprintf('Token %s', $authToken), - 'User-Agent' => sprintf('Hypernode API PHP Client v%s', HypernodeClient::VERSION), + 'User-Agent' => sprintf( + 'Hypernode API PHP Client v%s', + InstalledVersions::getVersion('hypernode/api-client'), + ), 'Accept' => 'application/json', 'Content-Type' => 'application/json', ]; From 93254bc61219324a8e73f817d86538fb9592bf37 Mon Sep 17 00:00:00 2001 From: Timon de Groot Date: Fri, 23 Dec 2022 12:49:38 +0100 Subject: [PATCH 3/8] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index baa8c83..a5f9893 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ while ($job && !$job->completed()) { 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. From f26de76f38f4e710547213e0176c3aebc9efe440 Mon Sep 17 00:00:00 2001 From: "mark.dentoom" Date: Thu, 23 Feb 2023 12:02:51 +0100 Subject: [PATCH 4/8] use-new-brancher-app-endpoint --- src/Service/App.php | 2 +- src/Service/BrancherApp.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Service/App.php b/src/Service/App.php index ad34de8..29dedfe 100644 --- a/src/Service/App.php +++ b/src/Service/App.php @@ -9,7 +9,7 @@ class App extends AbstractService public const V2_APP_LIST_URL = "/v2/app/"; public const V2_APP_DETAIL_URL = "/v2/app/%s/"; public const V2_APP_CANCEL_URL = "/v2/app/%s/cancel/"; - public const V2_APP_BRANCHER_URL = "/v2/app/%s/brancher/"; + public const V2_BRANCHER_APP_URL = "/v2/brancher/app/%s/"; public const V1_APP_FLOWS_URL = "/logbook/v1/logbooks/%s/flows/"; /** diff --git a/src/Service/BrancherApp.php b/src/Service/BrancherApp.php index 94a238e..5e85108 100644 --- a/src/Service/BrancherApp.php +++ b/src/Service/BrancherApp.php @@ -20,7 +20,7 @@ class BrancherApp extends AbstractService */ public function create(string $app, ?array $data = null): string { - $url = sprintf(App::V2_APP_BRANCHER_URL, $app); + $url = sprintf(App::V2_BRANCHER_APP_URL, $app); $response = $this->client->api->post($url, [], $data ? json_encode($data) : null); From 65e6778928a585da114fbea9dfaf5ace8353a4eb Mon Sep 17 00:00:00 2001 From: "mark.dentoom" Date: Thu, 23 Feb 2023 12:05:44 +0100 Subject: [PATCH 5/8] update tests --- tests/unit/Service/BrancherAppTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/Service/BrancherAppTest.php b/tests/unit/Service/BrancherAppTest.php index 36d9461..b2020c6 100644 --- a/tests/unit/Service/BrancherAppTest.php +++ b/tests/unit/Service/BrancherAppTest.php @@ -25,7 +25,7 @@ public function testCreateBrancherApp() $request = $this->responses->getLastRequest(); $this->assertEquals('POST', $request->getMethod()); - $this->assertEquals('/v2/app/johndoe/brancher/', $request->getUri()); + $this->assertEquals('/v2/brancher/app/johndoe/', $request->getUri()); $this->assertEquals('johndoe-eph123456', $brancherAppName); } @@ -46,7 +46,7 @@ public function testCreateBrancherAppWithData() $request = $this->responses->getLastRequest(); $this->assertEquals('POST', $request->getMethod()); - $this->assertEquals('/v2/app/johndoe/brancher/', $request->getUri()); + $this->assertEquals('/v2/brancher/app/johndoe/', $request->getUri()); $this->assertEquals('johndoe-eph123456', $brancherAppName); $this->assertJson((string)$request->getBody()); $this->assertEquals( From e36cc4f271cb516ac08a8c8ae5b15f578ef7f9c0 Mon Sep 17 00:00:00 2001 From: Peter Jaap Blaakmeer Date: Mon, 27 Mar 2023 11:51:21 +0200 Subject: [PATCH 6/8] Add list method to API --- src/Service/BrancherApp.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Service/BrancherApp.php b/src/Service/BrancherApp.php index 5e85108..5a50159 100644 --- a/src/Service/BrancherApp.php +++ b/src/Service/BrancherApp.php @@ -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_APP_BRANCHER_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. * @@ -20,7 +42,7 @@ class BrancherApp extends AbstractService */ public function create(string $app, ?array $data = null): string { - $url = sprintf(App::V2_BRANCHER_APP_URL, $app); + $url = sprintf(App::V2_APP_BRANCHER_URL, $app); $response = $this->client->api->post($url, [], $data ? json_encode($data) : null); From 8776b586c600dfe77d7dcec9023c944ed7705c2b Mon Sep 17 00:00:00 2001 From: Peter Jaap Blaakmeer Date: Mon, 27 Mar 2023 11:52:59 +0200 Subject: [PATCH 7/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a5f9893..cc7b8a7 100644 --- a/README.md +++ b/README.md @@ -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 From 0e0a902fea77517aa11740f5b2f7af8d8d28a379 Mon Sep 17 00:00:00 2001 From: Peter Jaap Blaakmeer Date: Mon, 27 Mar 2023 15:52:53 +0200 Subject: [PATCH 8/8] Fix endpoint, add unit test --- src/Service/BrancherApp.php | 4 +-- tests/unit/Service/BrancherAppTest.php | 43 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/Service/BrancherApp.php b/src/Service/BrancherApp.php index 5a50159..aca7489 100644 --- a/src/Service/BrancherApp.php +++ b/src/Service/BrancherApp.php @@ -20,7 +20,7 @@ class BrancherApp extends AbstractService */ public function list(string $app, ?array $data = null): array { - $url = sprintf(App::V2_APP_BRANCHER_URL, $app); + $url = sprintf(App::V2_BRANCHER_APP_URL, $app); $response = $this->client->api->get($url, [], $data ? json_encode($data) : null); @@ -42,7 +42,7 @@ public function list(string $app, ?array $data = null): array */ public function create(string $app, ?array $data = null): string { - $url = sprintf(App::V2_APP_BRANCHER_URL, $app); + $url = sprintf(App::V2_BRANCHER_APP_URL, $app); $response = $this->client->api->post($url, [], $data ? json_encode($data) : null); diff --git a/tests/unit/Service/BrancherAppTest.php b/tests/unit/Service/BrancherAppTest.php index b2020c6..80f55c8 100644 --- a/tests/unit/Service/BrancherAppTest.php +++ b/tests/unit/Service/BrancherAppTest.php @@ -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(