Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zbrag committed Sep 9, 2024
1 parent 11dd0ea commit 276f55a
Show file tree
Hide file tree
Showing 14 changed files with 458 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
109 changes: 109 additions & 0 deletions src/Api/HostsApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php declare(strict_types = 1);

namespace SandwaveIo\RealtimeRegister\Api;

use SandwaveIo\RealtimeRegister\Domain\DnsHost;
use SandwaveIo\RealtimeRegister\Domain\DnsHostAddressCollection;
use SandwaveIo\RealtimeRegister\Domain\DnsHostCollection;
use SandwaveIo\RealtimeRegister\Exceptions\InvalidArgumentException;

final class HostsApi extends AbstractApi
{
/**
* @see https://dm.realtimeregister.com/docs/api/hosts/list
*
* @param int|null $limit
* @param int|null $offset
* @param string|null $search
* @param array|null $parameters
*
* @throws InvalidArgumentException
*
* @return DnsHostCollection
*/
public function list(
?int $limit = null,
?int $offset = null,
?string $search = null,
?array $parameters = null
): DnsHostCollection {
$query = [];
if ($limit !== null) {
$query['limit'] = $limit;
}
if ($offset !== null) {
$query['offset'] = $offset;
}
if ($search !== null) {
$query['q'] = $search;
}
if ($parameters !== null) {
$query = array_merge($parameters, $query);
}

$response = $this->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)
);
}
}
51 changes: 51 additions & 0 deletions src/Domain/DnsHost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types = 1);

namespace SandwaveIo\RealtimeRegister\Domain;

use DateTime;

class DnsHost implements DomainObjectInterface
{
public string $hostName;

public Datetime $createdDate;

public ?Datetime $updatedDate;

public ?array $addresses;

private function __construct(
string $hostname,
DateTime $createdDate,
?DateTime $updatedDate,
?array $addresses
) {
$this->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
);
}
}
36 changes: 36 additions & 0 deletions src/Domain/DnsHostAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

namespace SandwaveIo\RealtimeRegister\Domain;

use SandwaveIo\RealtimeRegister\Domain\Enum\IpVersion;

class DnsHostAddress implements DomainObjectInterface
{
public IpVersion $ipVersion;

public string $address;

private function __construct(IpVersion $ipVersion, string $address)
{
$this->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'],
);
}
}
24 changes: 24 additions & 0 deletions src/Domain/DnsHostAddressCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);

namespace SandwaveIo\RealtimeRegister\Domain;

class DnsHostAddressCollection extends AbstractCollection
{
/** @var DnsHostAddress[] */
public array $entities;

public static function fromArray(array $json): DnsHostAddressCollection
{
return parent::fromArray($json);
}

public function offsetGet($offset): ?DnsHostAddress
{
return $this->entities[$offset] ?? null;
}

public static function parseChild(array $json): DnsHostAddress
{
return DnsHostAddress::fromArray($json);
}
}
24 changes: 24 additions & 0 deletions src/Domain/DnsHostCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);

namespace SandwaveIo\RealtimeRegister\Domain;

class DnsHostCollection extends AbstractCollection
{
/** @var DnsHost[] */
public array $entities;

public static function fromArray(array $json): DnsHostCollection
{
return parent::fromArray($json);
}

public function offsetGet($offset): ?DnsHost
{
return $this->entities[$offset] ?? null;
}

public static function parseChild(array $json): DnsHost
{
return DnsHost::fromArray($json);
}
}
11 changes: 11 additions & 0 deletions src/Domain/Enum/IpVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types = 1);

namespace SandwaveIo\RealtimeRegister\Domain\Enum;

enum IpVersion: string
{
case V4 = 'V4';
case V6 = 'V6';
}
4 changes: 4 additions & 0 deletions src/RealtimeRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use SandwaveIo\RealtimeRegister\Api\DnsZonesApi;
use SandwaveIo\RealtimeRegister\Api\DomainsApi;
use SandwaveIo\RealtimeRegister\Api\FinancialApi;
use SandwaveIo\RealtimeRegister\Api\HostsApi;
use SandwaveIo\RealtimeRegister\Api\NotificationsApi;
use SandwaveIo\RealtimeRegister\Api\ProcessesApi;
use SandwaveIo\RealtimeRegister\Api\ProvidersApi;
Expand All @@ -31,6 +32,8 @@ final class RealtimeRegister

public DomainsApi $domains;

public HostsApi $hosts;

public NotificationsApi $notifications;

public ProcessesApi $processes;
Expand Down Expand Up @@ -61,6 +64,7 @@ public function setClient(AuthorizedClient $client): void
$this->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);
Expand Down
65 changes: 65 additions & 0 deletions tests/Clients/DnsHostsApiCreateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php declare(strict_types = 1);

namespace SandwaveIo\RealtimeRegister\Tests\Clients;

use PHPUnit\Framework\TestCase;
use SandwaveIo\RealtimeRegister\Domain\DnsHostAddressCollection;
use SandwaveIo\RealtimeRegister\Tests\Helpers\MockedClientFactory;

class DnsHostsApiCreateTest extends TestCase
{
public function test_create(): void
{
$sdk = MockedClientFactory::makeSdk(
200,
'',
MockedClientFactory::assertRoute('POST', 'v2/hosts/ns1.example.com', $this)
);

$sdk->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',
],
])
);
}
}
20 changes: 20 additions & 0 deletions tests/Clients/DnsHostsApiDeleteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types = 1);

namespace SandwaveIo\RealtimeRegister\Tests\Clients;

use PHPUnit\Framework\TestCase;
use SandwaveIo\RealtimeRegister\Tests\Helpers\MockedClientFactory;

class DnsHostsApiDeleteTest extends TestCase
{
public function test_delete(): void
{
$sdk = MockedClientFactory::makeSdk(
200,
'',
MockedClientFactory::assertRoute('DELETE', 'v2/hosts/ns1.example.com', $this)
);

$sdk->hosts->delete('ns1.example.com');
}
}
Loading

0 comments on commit 276f55a

Please sign in to comment.