-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the hosts api ( https://dm.realtimeregister.com/docs/api/hosts )
- Loading branch information
Showing
14 changed files
with
458 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
], | ||
]) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
Oops, something went wrong.