Skip to content
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

Hosts api added #85

Closed
wants to merge 2 commits into from
Closed
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
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)
zbrag marked this conversation as resolved.
Show resolved Hide resolved
* [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;

Check warning on line 32 in src/Api/HostsApi.php

View check run for this annotation

Codecov / codecov/patch

src/Api/HostsApi.php#L32

Added line #L32 was not covered by tests
}
if ($offset !== null) {
$query['offset'] = $offset;

Check warning on line 35 in src/Api/HostsApi.php

View check run for this annotation

Codecov / codecov/patch

src/Api/HostsApi.php#L35

Added line #L35 was not covered by tests
}
if ($search !== null) {
$query['q'] = $search;
}
if ($parameters !== null) {
$query = array_merge($parameters, $query);

Check warning on line 41 in src/Api/HostsApi.php

View check run for this annotation

Codecov / codecov/patch

src/Api/HostsApi.php#L41

Added line #L41 was not covered by tests
}

$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['addresses'] = $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

Check warning on line 29 in src/Domain/DnsHost.php

View check run for this annotation

Codecov / codecov/patch

src/Domain/DnsHost.php#L29

Added line #L29 was not covered by tests
{
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);
});

Check warning on line 38 in src/Domain/DnsHost.php

View check run for this annotation

Codecov / codecov/patch

src/Domain/DnsHost.php#L31-L38

Added lines #L31 - L38 were not covered by tests
}

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

Check warning on line 15 in src/Domain/DnsHostAddressCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Domain/DnsHostAddressCollection.php#L15

Added line #L15 was not covered by tests
{
return $this->entities[$offset] ?? null;

Check warning on line 17 in src/Domain/DnsHostAddressCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Domain/DnsHostAddressCollection.php#L17

Added line #L17 was not covered by tests
}

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

Check warning on line 15 in src/Domain/DnsHostCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Domain/DnsHostCollection.php#L15

Added line #L15 was not covered by tests
{
return $this->entities[$offset] ?? null;

Check warning on line 17 in src/Domain/DnsHostCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Domain/DnsHostCollection.php#L17

Added line #L17 was not covered by tests
}

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
Loading