Commit ceea61d2 authored by Ludwig Ruderstaller's avatar Ludwig Ruderstaller
Browse files

Initial Commit

parents
<?php
/*
* This file is part of powerdns client.
*
* (c) 2018 cwd.at GmbH <office@cwd.at>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Cwd\PowerDNSClient;
use GuzzleHttp\Psr7\Request;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use GuzzleHttp\Client as GuzzleClient;
class Client
{
private $basePath = 'api/v1';
private $serverId = 'localhost';
private $apiKey;
/** @var HttpClient */
private $client;
/** @var Serializer */
private $serializer;
public function __construct($apiHost, $apiKey, ?GuzzleClient $client = null)
{
if (null === $client) {
$this->client = HttpClientDiscovery::find();
}
$this->apiKey = $apiKey;
$this->apiUri = sprintf('%s/%s', $apiHost, $this->basePath);
$normalizer = new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter(), null, new ReflectionExtractor());
$this->serializer = new Serializer([new DateTimeNormalizer(), $normalizer], ['json' => new JsonEncoder()]);
}
/**
* @param string|null $payload
* @param int|string|null $id
* @param string $endpoint
* @param string|null $hydrationClass
* @param bool $isList
* @param string $method
*
* @throws \Http\Client\Exception
* @throws \LogicException
*
* @return mixed
*/
public function call($payload = null, $uri, $hydrationClass = null, $isList = false, $method = 'GET')
{
$uri = sprintf('%s/%s', $this->apiUri, $uri);
$uri = rtrim($uri, '/');
dump($uri);
$request = new Request($method, $uri, [
'X-API-Key' => $this->apiKey,
'Content-Type' => 'application/json',
], $payload);
$response = $this->client->sendRequest($request);
$responseBody = $response->getBody()->getContents();
$responseData = json_decode($responseBody);
dump($responseData);
if ($response->getStatusCode() >= 300) {
$message = isset($responseData->message) ?? 'Unknown';
throw new \Exception(sprintf('Error on request %s: %s', $response->getStatusCode(), $message));
}
if (null !== $hydrationClass && class_exists($hydrationClass)) {
return $this->denormalizeObject($hydrationClass, $responseData, $isList);
} elseif (null !== $hydrationClass && !class_exists($hydrationClass)) {
throw new \Exception(sprintf('HydrationClass (%s) does not exist', $hydrationClass));
}
return $responseData;
}
public function denormalizeObject($hydrationClass, $dataObject, $isList = false)
{
if (!$isList) {
$dataObject = [$dataObject];
}
foreach ($dataObject as $data) {
$result[] = $this->serializer->denormalize($data, $hydrationClass, null, [
ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true,
]);
}
if ($isList) {
return $result;
}
return current($result);
}
protected function getClient(): HttpClient
{
return $this->client;
}
/**
* @return Serializer
*/
public function getSerializer(): Serializer
{
return $this->serializer;
}
}
<?php
/*
* This file is part of datamolino client.
*
* (c) 2018 cwd.at GmbH <office@cwd.at>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Cwd\PowerDNSClient\Endpoints;
use Cwd\PowerDNSClient\Client;
abstract class AbstractEndpoint
{
private $client;
protected $defaultServerId;
public function __construct(Client $client, $defaultServerId)
{
$this->client = $client;
$this->defaultServerId = $defaultServerId;
}
public function getClient(): Client
{
return $this->client;
}
}
<?php
/*
* This file is part of datamolino client.
*
* (c) 2018 cwd.at GmbH <office@cwd.at>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Cwd\PowerDNSClient\Endpoints;
use Cwd\PowerDNSClient\Model\Server;
class ServersEndpoint extends AbstractEndpoint
{
const ENDPOINT = 'servers/%s';
public function get(?string $serverId = null): Server
{
if ($serverId === null) {
$serverId = $this->defaultServerId;
}
return $this->getClient()->call(null, sprintf('servers/%s', $serverId), Server::class, false, 'GET');
}
/**
* @return Servers[]
* @throws \Http\Client\Exception
*/
public function all(): array
{
return $this->getClient()->call(null, 'servers', Server::class, true, 'GET');
}
/**
* @return array
*
* @throws \Http\Client\Exception
*/
public function statistics(): array
{
// Result is different - denormalize by hand
return $this->getClient()->call(null, sprintf( self::ENDPOINT, $this->defaultServerId).'/statistics', null, false, 'GET');
}
}
<?php
namespace Cwd\PowerDNSClient\Model;
class CacheFlushResult
{
/** @var integer */
private $count;
/** @var string */
private $result;
/**
* @return int
*/
public function getCount(): int
{
return $this->count;
}
/**
* @param int $count
* @return CacheFlushResult
*/
public function setCount(int $count): CacheFlushResult
{
$this->count = $count;
return $this;
}
/**
* @return string
*/
public function getResult(): string
{
return $this->result;
}
/**
* @param string $result
* @return CacheFlushResult
*/
public function setResult(string $result): CacheFlushResult
{
$this->result = $result;
return $this;
}
}
\ No newline at end of file
<?php
namespace Cwd\PowerDNSClient\Model;
class Cryptokey
{
/** @var string */
private $type;
/** @var string */
private $id;
/** @var string */
private $keytype;
/** @var bool */
private $active = false;
/** @var string */
private $dnskey;
/** @var string[] */
private $ds = [];
/** @var string */
private $privatekey;
/** @var string */
private $algorithm;
/** @var integer */
private $bits;
/**
* @return string
*/
public function getType(): string
{
return $this->type;
}
/**
* @param string $type
* @return Cryptokey
*/
public function setType(string $type): Cryptokey
{
$this->type = $type;
return $this;
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @param string $id
* @return Cryptokey
*/
public function setId(string $id): Cryptokey
{
$this->id = $id;
return $this;
}
/**
* @return string
*/
public function getKeytype(): string
{
return $this->keytype;
}
/**
* @param string $keytype
* @return Cryptokey
*/
public function setKeytype(string $keytype): Cryptokey
{
$this->keytype = $keytype;
return $this;
}
/**
* @return bool
*/
public function isActive(): bool
{
return $this->active;
}
/**
* @param bool $active
* @return Cryptokey
*/
public function setActive(bool $active): Cryptokey
{
$this->active = $active;
return $this;
}
/**
* @return string
*/
public function getDnskey(): string
{
return $this->dnskey;
}
/**
* @param string $dnskey
* @return Cryptokey
*/
public function setDnskey(string $dnskey): Cryptokey
{
$this->dnskey = $dnskey;
return $this;
}
/**
* @return string[]
*/
public function getDs(): array
{
return $this->ds;
}
/**
* @param string[] $ds
* @return Cryptokey
*/
public function setDs(array $ds): Cryptokey
{
$this->ds = $ds;
return $this;
}
/**
* @return string
*/
public function getPrivatekey(): string
{
return $this->privatekey;
}
/**
* @param string $privatekey
* @return Cryptokey
*/
public function setPrivatekey(string $privatekey): Cryptokey
{
$this->privatekey = $privatekey;
return $this;
}
/**
* @return string
*/
public function getAlgorithm(): string
{
return $this->algorithm;
}
/**
* @param string $algorithm
* @return Cryptokey
*/
public function setAlgorithm(string $algorithm): Cryptokey
{
$this->algorithm = $algorithm;
return $this;
}
/**
* @return int
*/
public function getBits(): int
{
return $this->bits;
}
/**
* @param int $bits
* @return Cryptokey
*/
public function setBits(int $bits): Cryptokey
{
$this->bits = $bits;
return $this;
}
}
\ No newline at end of file
<?php
namespace Cwd\PowerDNSClient\Model;
class Metadata
{
/** @var string */
private $kind;
/** @var string[] */
private $metadata = [];
/**
* @return string
*/
public function getKind(): string
{
return $this->kind;
}
/**
* @param string $kind
* @return Metadata
*/
public function setKind(string $kind): Metadata
{
$this->kind = $kind;
return $this;
}
/**
* @return string[]
*/
public function getMetadata(): array
{
return $this->metadata;
}
/**
* @param string[] $metadata
* @return Metadata
*/
public function setMetadata(array $metadata): Metadata
{
$this->metadata = $metadata;
return $this;
}
}
\ No newline at end of file
<?php
namespace Cwd\PowerDNSClient\Model;
class SearchResult
{
/** @var string */
private $content;
/** @var bool */
private $disabled = false;
/** @var string */
private $name;
/** @var string */
private $objectType;
/** @var string */
private $zoneId;
/** @var string */
private $zone;
/** @var string */
private $type;
/** @var integer */
private $ttl;
/**
* @return string
*/
public function getContent(): string
{
return $this->content;
}
/**
* @param string $content
* @return SearchResult
*/
public function setContent(string $content): SearchResult
{
$this->content = $content;
return $this;
}
/**
* @return bool
*/
public function isDisabled(): bool
{
return $this->disabled;
}
/**
* @param bool $disabled
* @return SearchResult
*/
public function setDisabled(bool $disabled): SearchResult
{
$this->disabled = $disabled;
return $this;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
* @return SearchResult
*/
public function setName(string $name): SearchResult
{
$this->name = $name;
return $this;
}
/**
* @return string
*/
public function getObjectType(): string
{
return $this->objectType;
}
/**
* @param string $objectType
* @return SearchResult
*/
public function setObjectType(string $objectType): SearchResult
{
$this->objectType = $objectType;
return $this;
}
/**
* @return string
*/
public function getZoneId(): string
{
return $this->zoneId;
}
/**
* @param string $zoneId
* @return SearchResult
*/
public function setZoneId(string $zoneId): SearchResult
{
$this->zoneId = $zoneId;
return $this;
}
/**
* @return string
*/
public function getZone(): string
{
return $this->zone;
}
/**
* @param string $zone
* @return SearchResult
*/
public function setZone(string $zone): SearchResult
{
$this->zone = $zone;
return $this;
}
/**