From 276f55a2cc19a53e4419f8fe6d0fd0238653d948 Mon Sep 17 00:00:00 2001 From: Hylke Witjens Date: Mon, 9 Sep 2024 17:00:52 +0200 Subject: [PATCH] Added the hosts api ( https://dm.realtimeregister.com/docs/api/hosts ) --- README.md | 1 + src/Api/HostsApi.php | 109 ++++++++++++++++++++++++ src/Domain/DnsHost.php | 51 +++++++++++ src/Domain/DnsHostAddress.php | 36 ++++++++ src/Domain/DnsHostAddressCollection.php | 24 ++++++ src/Domain/DnsHostCollection.php | 24 ++++++ src/Domain/Enum/IpVersion.php | 11 +++ src/RealtimeRegister.php | 4 + tests/Clients/DnsHostsApiCreateTest.php | 65 ++++++++++++++ tests/Clients/DnsHostsApiDeleteTest.php | 20 +++++ tests/Clients/DnsHostsApiGetTest.php | 21 +++++ tests/Clients/DnsHostsApiListTest.php | 33 +++++++ tests/Clients/DnsHostsApiUpdateTest.php | 49 +++++++++++ tests/Domain/data/hosts.php | 10 +++ 14 files changed, 458 insertions(+) create mode 100644 src/Api/HostsApi.php create mode 100644 src/Domain/DnsHost.php create mode 100644 src/Domain/DnsHostAddress.php create mode 100644 src/Domain/DnsHostAddressCollection.php create mode 100644 src/Domain/DnsHostCollection.php create mode 100644 src/Domain/Enum/IpVersion.php create mode 100644 tests/Clients/DnsHostsApiCreateTest.php create mode 100644 tests/Clients/DnsHostsApiDeleteTest.php create mode 100644 tests/Clients/DnsHostsApiGetTest.php create mode 100644 tests/Clients/DnsHostsApiListTest.php create mode 100644 tests/Clients/DnsHostsApiUpdateTest.php create mode 100644 tests/Domain/data/hosts.php diff --git a/README.md b/README.md index e7411f8..39e34f5 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ This SDK currently supports these APIs: * [Domains API](https://dm.realtimeregister.com/docs/api/domains) +* [Hosts API](https://dm.realtimeregister.com/docs/api/hosts) * [Customers API](https://dm.realtimeregister.com/docs/api/customers) * [Contacts API](https://dm.realtimeregister.com/docs/api/contacts) * [Notifications API](https://dm.realtimeregister.com/docs/api/notifications) diff --git a/src/Api/HostsApi.php b/src/Api/HostsApi.php new file mode 100644 index 0000000..854938c --- /dev/null +++ b/src/Api/HostsApi.php @@ -0,0 +1,109 @@ +client->get('v2/hosts', $query); + return DnsHostCollection::fromArray($response->json()); + } + + /** + * @see https://dm.realtimeregister.com/docs/api/hosts/get + * + * @param string $hostName + * + * @return DnsHost + */ + public function get(string $hostName): DnsHost + { + $response = $this->client->get( + sprintf('v2/hosts/%s', $hostName) + ); + return DnsHost::fromArray($response->json()); + } + + /** + * @see https://dm.realtimeregister.com/docs/api/hosts/create + * + * @throws InvalidArgumentException + */ + public function create( + string $hostName, + ?DnsHostAddressCollection $addresses = null + ): void { + $payload = [ + 'hostName' => $hostName, + ]; + if ($addresses !== null) { + $payload['addresses'] = $addresses->toArray(); + } + + $this->client->post(sprintf('v2/hosts/%s', $hostName), $payload); + } + + /** + * @see https://dm.realtimeregister.com/docs/api/hosts/update + */ + public function update( + string $hostName, + ?DnsHostAddressCollection $addresses + ): void { + $payload = [ + 'hostName' => $hostName, + ]; + + if ($addresses instanceof DnsHostAddressCollection) { + $payload['billables'] = $addresses->toArray(); + } + + $this->client->post(sprintf('v2/hosts/%s/update', $hostName), $payload); + } + + /** + * @see https://dm.realtimeregister.com/docs/api/hosts/delete + */ + public function delete(string $hostName): void + { + $this->client->delete( + sprintf('v2/hosts/%s', $hostName) + ); + } +} diff --git a/src/Domain/DnsHost.php b/src/Domain/DnsHost.php new file mode 100644 index 0000000..e951a09 --- /dev/null +++ b/src/Domain/DnsHost.php @@ -0,0 +1,51 @@ +hostName = $hostname; + $this->createdDate = $createdDate; + $this->updatedDate = $updatedDate; + $this->addresses = $addresses; + } + + public function toArray(): array + { + return array_filter([ + 'hostName' => $this->hostName, + 'createdDate' => $this->createdDate->format('Y-m-d\TH:i:s\Z'), + 'updatedDate' => $this->updatedDate?->format('Y-m-d\TH:i:s\Z'), + 'addresses' => $this->addresses, + ], function ($x) { + return ! is_null($x); + }); + } + + public static function fromArray(array $json): DnsHost + { + $updatedDate = isset($json['updatedDate']) ? new DateTime($json['updatedDate']) : null; + return new DnsHost( + $json['hostName'], + new DateTime($json['createdDate']), + $updatedDate, + $json['addresses']??null + ); + } +} diff --git a/src/Domain/DnsHostAddress.php b/src/Domain/DnsHostAddress.php new file mode 100644 index 0000000..fe028e0 --- /dev/null +++ b/src/Domain/DnsHostAddress.php @@ -0,0 +1,36 @@ +ipVersion = $ipVersion; + $this->address = $address; + } + + public function toArray(): array + { + return array_filter([ + 'ipVersion' => $this->ipVersion->value, + 'address' => $this->address, + ], function ($x) { + return $x !== null; + }); + } + + public static function fromArray(array $json): DnsHostAddress + { + return new DnsHostAddress( + ipVersion: IpVersion::from($json['ipVersion']), + address: $json['address'], + ); + } +} diff --git a/src/Domain/DnsHostAddressCollection.php b/src/Domain/DnsHostAddressCollection.php new file mode 100644 index 0000000..7d0caf6 --- /dev/null +++ b/src/Domain/DnsHostAddressCollection.php @@ -0,0 +1,24 @@ +entities[$offset] ?? null; + } + + public static function parseChild(array $json): DnsHostAddress + { + return DnsHostAddress::fromArray($json); + } +} diff --git a/src/Domain/DnsHostCollection.php b/src/Domain/DnsHostCollection.php new file mode 100644 index 0000000..7299cbe --- /dev/null +++ b/src/Domain/DnsHostCollection.php @@ -0,0 +1,24 @@ +entities[$offset] ?? null; + } + + public static function parseChild(array $json): DnsHost + { + return DnsHost::fromArray($json); + } +} diff --git a/src/Domain/Enum/IpVersion.php b/src/Domain/Enum/IpVersion.php new file mode 100644 index 0000000..94b3bda --- /dev/null +++ b/src/Domain/Enum/IpVersion.php @@ -0,0 +1,11 @@ +contacts = new ContactsApi($client); $this->customers = new CustomersApi($client); $this->domains = new DomainsApi($client); + $this->hosts = new HostsApi($client); $this->notifications = new NotificationsApi($client); $this->processes = new ProcessesApi($client); $this->providers = new ProvidersApi($client); diff --git a/tests/Clients/DnsHostsApiCreateTest.php b/tests/Clients/DnsHostsApiCreateTest.php new file mode 100644 index 0000000..0688232 --- /dev/null +++ b/tests/Clients/DnsHostsApiCreateTest.php @@ -0,0 +1,65 @@ +hosts->create( + 'ns1.example.com', + ); + } + + public function test_create_with_only_ipv6(): void + { + $sdk = MockedClientFactory::makeSdk( + 200, + '', + MockedClientFactory::assertRoute('POST', 'v2/hosts/ns2.example.com', $this) + ); + + $sdk->hosts->create( + 'ns2.example.com', + DnsHostAddressCollection::fromArray([ + [ + 'ipVersion' => 'V6', + 'address' => '::1', + ], + ]) + ); + } + + public function test_create_with_details(): void + { + $sdk = MockedClientFactory::makeSdk( + 200, + '', + MockedClientFactory::assertRoute('POST', 'v2/hosts/ns3.example.com', $this) + ); + + $sdk->hosts->create( + 'ns3.example.com', + DnsHostAddressCollection::fromArray([ + [ + 'ipVersion' => 'V4', + 'address' => '127.0.0.1', + ], + [ + 'ipVersion' => 'V6', + 'address' => '::1', + ], + ]) + ); + } +} diff --git a/tests/Clients/DnsHostsApiDeleteTest.php b/tests/Clients/DnsHostsApiDeleteTest.php new file mode 100644 index 0000000..fb21a50 --- /dev/null +++ b/tests/Clients/DnsHostsApiDeleteTest.php @@ -0,0 +1,20 @@ +hosts->delete('ns1.example.com'); + } +} diff --git a/tests/Clients/DnsHostsApiGetTest.php b/tests/Clients/DnsHostsApiGetTest.php new file mode 100644 index 0000000..36055aa --- /dev/null +++ b/tests/Clients/DnsHostsApiGetTest.php @@ -0,0 +1,21 @@ +hosts->get('example.com'); + $this->assertSame('ns1.example.com', $response->hostName); + } +} diff --git a/tests/Clients/DnsHostsApiListTest.php b/tests/Clients/DnsHostsApiListTest.php new file mode 100644 index 0000000..895a9af --- /dev/null +++ b/tests/Clients/DnsHostsApiListTest.php @@ -0,0 +1,33 @@ + [ + include __DIR__ . '/../Domain/data/hosts.php', + ], + 'pagination' => [ + 'total' => 1, + 'offset' => 0, + 'limit' => 10, + ], + ]), + MockedClientFactory::assertRoute('GET', 'v2/hosts', $this) + ); + + $result = $sdk->hosts->list( + null, + null, + 'ns1.example.com', + ); + } +} diff --git a/tests/Clients/DnsHostsApiUpdateTest.php b/tests/Clients/DnsHostsApiUpdateTest.php new file mode 100644 index 0000000..644a522 --- /dev/null +++ b/tests/Clients/DnsHostsApiUpdateTest.php @@ -0,0 +1,49 @@ +hosts->update( + 'ns1.example.com', + null + ); + } + + public function test_update(): void + { + $sdk = MockedClientFactory::makeSdk( + 200, + '', + MockedClientFactory::assertRoute('POST', 'v2/hosts/ns1.example.com/update', $this) + ); + + $sdk->hosts->update( + 'ns1.example.com', + DnsHostAddressCollection::fromArray( + [ + [ + 'ipVersion' => 'V4', + 'address' => '127.0.0.1', + ], + [ + 'ipVersion' => 'V6', + 'address' => '::1', + ], + ] + ) + ); + } +} diff --git a/tests/Domain/data/hosts.php b/tests/Domain/data/hosts.php new file mode 100644 index 0000000..1c3e18f --- /dev/null +++ b/tests/Domain/data/hosts.php @@ -0,0 +1,10 @@ + 'ns1.example.com', + 'domain' => 'example.com', + 'createdDate' => '2021-03-04T17:00:00Z', + 'updatedDate' => '2021-03-04T17:00:00Z', +];