Skip to content

Commit

Permalink
Implement Examples & Bugfixing
Browse files Browse the repository at this point in the history
  • Loading branch information
LKaemmerling committed Jan 29, 2018
1 parent e9ab838 commit bdb3879
Show file tree
Hide file tree
Showing 25 changed files with 185 additions and 102 deletions.
5 changes: 5 additions & 0 deletions examples/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
require_once __DIR__.'/../vendor/autoload.php';
$apiKey = '{InsertYourAPIKeyHear}';

$hetznerClient = new \LKDev\HetznerCloud\HetznerAPIClient($apiKey);
7 changes: 7 additions & 0 deletions examples/servers/get_a_specifc_server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
require_once __DIR__.'/../bootstrap.php';

$servers = new \LKDev\HetznerCloud\Models\Servers\Servers();
$serverId = 494200;
$server = $servers->get($serverId);
var_dump($server);
8 changes: 8 additions & 0 deletions examples/servers/get_all_server_types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
require_once __DIR__.'/../bootstrap.php';

$servers = new \LKDev\HetznerCloud\Models\Servers\Servers();
$serverTypes = new \LKDev\HetznerCloud\Models\Servers\Types\ServerTypes();
foreach ($serverTypes->all() as $serverType) {
echo $serverType->name.PHP_EOL;
}
7 changes: 7 additions & 0 deletions examples/servers/get_all_servers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
require_once __DIR__.'/../bootstrap.php';

$servers = new \LKDev\HetznerCloud\Models\Servers\Servers();
foreach ($servers->all() as $server) {
echo 'ID: '.$server->id.' Name:'.$server->name.' Status: '.$server->status.PHP_EOL;
}
27 changes: 27 additions & 0 deletions examples/servers/toggle_a_server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
require_once __DIR__.'/../bootstrap.php';

$servers = new \LKDev\HetznerCloud\Models\Servers\Servers();
$serverId = 494200;
$server = $servers->get($serverId);
echo 'Server: '.$server->name.PHP_EOL;
echo "Perform Shutdown now:".PHP_EOL;
/**
* @var \LKDev\HetznerCloud\Models\Servers\Server $server
*/
$action = $server->shutdown();

echo "Reply from API: Action ID: ".$action->id.' '.$action->command.' '.$action->started.PHP_EOL;

echo 'Wait some seconds that the server could shutdown.'.PHP_EOL;
sleep(5);
echo "Get the Server from the API:".PHP_EOL;
$server = $servers->get($serverId);
echo "Server status: ".$server->status.PHP_EOL;
echo "Let's start it again!";
$server->powerOn();
echo 'Wait some seconds that the server could startup.'.PHP_EOL;
sleep(5);
echo "Get the Server from the API:".PHP_EOL;
$server = $servers->get($serverId);
echo "Server status: ".$server->status.PHP_EOL;
8 changes: 6 additions & 2 deletions src/HetznerAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class HetznerAPIClient
public function __construct(string $apiToken, $baseUrl = 'https://api.hetzner.cloud/v1/')
{
$this->apiToken = $apiToken;
$this->baseUrl = $baseUrl;
self::$hetznerApiClient = $this;
self::$httpClient = new GuzzleClient($this);
}
Expand All @@ -65,7 +66,9 @@ public function getBaseUrl(): string
*/
public static function throwError(ResponseInterface $response)
{
throw new APIException($response);
var_dump(json_decode((string) $response->getBody()));
die();
// throw new APIException($response, ->error->code);
}

/**
Expand All @@ -75,8 +78,9 @@ public static function throwError(ResponseInterface $response)
*/
public static function hasError(ResponseInterface $response)
{
if (property_exists($response, 'error') || $response->getStatusCode() !== 200) {
if ((property_exists($response, 'error')) || ($response->getStatusCode() <= 200 && $response->getStatusCode() >= 300)) {
self::throwError($response);

return true;
}

Expand Down
18 changes: 11 additions & 7 deletions src/Models/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Action extends Model
public $resources;

/**
* @var object|null
* @var |null
*/
public $error;

Expand All @@ -58,16 +58,16 @@ class Action extends Model
* @param string $started
* @param string $finished
* @param array $resources
* @param null|object $error
* @param null| $error
*/
public function __construct(
int $id,
string $command,
int $progress,
string $started,
string $finished,
array $resources,
object $error,
array $resources = null,
$error = null,
string $root_password = null
) {
$this->id = $id;
Expand All @@ -82,11 +82,15 @@ public function __construct(
}

/**
* @param object $input
* @param $input
* @return \LKDev\HetznerCloud\Models\Actions\Action|static
*/
public static function parse(object $input)
public static function parse($input)
{
return new self($input->id, $input->command, $input->status, $input->started, $input->finished, $input->resources, $input->error, $input->root_password);
if ($input == null) {
return null;
}

return new self($input->id, $input->command, $input->progress, $input->status, $input->started, $input->finished, $input->resources, $input->error, (property_exists($input, 'root_password') ? $input->root_password : null));
}
}
8 changes: 4 additions & 4 deletions src/Models/Actions/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public function get($actionId): Action
}

/**
* @param object $input
* @param $input
* @return $this
*/
public function setAdditionalData(object $input)
public function setAdditionalData( $input)
{
$this->actions = collect($input->actions)->map(function ($action, $key) {
return Action::parse($action);
Expand All @@ -71,10 +71,10 @@ public function setAdditionalData(object $input)
}

/**
* @param object $input
* @param $input
* @return $this|static
*/
public static function parse(object $input)
public static function parse($input)
{
return (new self())->setAdditionalData($input);
}
Expand Down
11 changes: 7 additions & 4 deletions src/Models/Datacenters/Datacenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public function __construct(
string $name,
string $description,
Location $location,
array $server_types,
bool $recommendation
array $server_types = null,
bool $recommendation = null
) {
$this->id = $id;
$this->name = $name;
Expand All @@ -74,11 +74,14 @@ public function __construct(
}

/**
* @param object $input
* @param $input
* @return \LKDev\HetznerCloud\Models\Datacenters\Datacenter|static
*/
public static function parse(object $input)
public static function parse($input)
{
if ($input == null) {
return null;
}
return new self($input->id,$input->name,$input->description,Location::parse($input->location),$input->server_types,$input->recommendation);
}
}
8 changes: 4 additions & 4 deletions src/Models/Datacenters/Datacenters.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public function get(int $datacenterId): Datacenter
}

/**
* @param object $input
* @param $input
* @return $this
*/
public function setAdditionalData(object $input)
public function setAdditionalData( $input)
{
$this->locations = collect($input->datacenters)->map(function ($datacenter, $key) {
return Datacenter::parse($datacenter);
Expand All @@ -63,10 +63,10 @@ public function setAdditionalData(object $input)
}

/**
* @param object $input
* @param $input
* @return $this|static
*/
public static function parse(object $input)
public static function parse($input)
{
return (new self())->setAdditionalData($input);
}
Expand Down
1 change: 1 addition & 0 deletions src/Models/FloatingIps/FloatingIp.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@

class FloatingIp
{
// ToDo
}
1 change: 1 addition & 0 deletions src/Models/FloatingIps/FloatingIps.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@

class FloatingIps
{
// ToDos
}
12 changes: 8 additions & 4 deletions src/Models/ISOs/ISO.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ISO extends Model
* @param string $description
* @param string $type
*/
public function __construct(int $id, string $name, string $description, string $type)
public function __construct(int $id = null, string $name = null, string $description = null, string $type = null)
{
$this->id = $id;
$this->name = $name;
Expand All @@ -47,11 +47,15 @@ public function __construct(int $id, string $name, string $description, string $
}

/**
* @param object $input
* @param $input
* @return \LKDev\HetznerCloud\Models\ISOs\ISO|static
*/
public static function parse(object $input)
public static function parse($input)
{
return new self($input->id,$input->name,$input->description,$input->type);
if ($input == null) {
return null;
}

return new self($input->id, $input->name, $input->description, $input->type);
}
}
8 changes: 4 additions & 4 deletions src/Models/ISOs/ISOs.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public function get(int $isoId): ISO
}

/**
* @param object $input
* @param $input
* @return $this
*/
public function setAdditionalData(object $input)
public function setAdditionalData( $input)
{
$this->locations = collect($input->isos)->map(function ($iso, $key) {
return ISO::parse($iso);
Expand All @@ -63,10 +63,10 @@ public function setAdditionalData(object $input)
}

/**
* @param object $input
* @param $input
* @return $this|static
*/
public static function parse(object $input)
public static function parse($input)
{
return (new self())->setAdditionalData($input);
}
Expand Down
33 changes: 18 additions & 15 deletions src/Models/Images/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,18 @@ class Image extends Model
*/
public function __construct(
int $id,
string $type,
string $status,
string $name,
string $description,
float $imageSize,
int $diskSize,
string $created,
Server $createdFrom,
int $boundTo,
string $osFlavor,
string $osVersion,
bool $rapidDeploy
string $type = null,
string $status = null,
string $name = null,
string $description = null,
float $imageSize = null,
int $diskSize = null,
string $created = null,
Server $createdFrom = null,
int $boundTo = null,
string $osFlavor = null,
string $osVersion = null,
bool $rapidDeploy = null
) {
$this->id = $id;
$this->type = $type;
Expand Down Expand Up @@ -168,11 +168,14 @@ public function delete(): bool
}

/**
* @param object $input
* @param $input
* @return \LKDev\HetznerCloud\Models\Images\Image|static
*/
public static function parse(object $input)
public static function parse($input)
{
return new self($input->id, $input->type, $input->status, $input->name, $input->description, $input->image_size, $input->disk_size, $input->created, Server::parse($input->created_from), $input->bound_to, $input->os_flavor, $input->os_version, $input->rapid_deploy);
if ($input == null) {
return null;
}
return new self($input->id, $input->type, $input->status, $input->name, $input->description, $input->image_size, $input->disk_size, $input->created, $input->created_from, $input->bound_to, $input->os_flavor, $input->os_version, $input->rapid_deploy);
}
}
8 changes: 4 additions & 4 deletions src/Models/Images/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public function get(int $imageId): Image
}

/**
* @param object $input
* @param $input
* @return $this
*/
public function setAdditionalData(object $input)
public function setAdditionalData( $input)
{
$this->images = collect($input->images)->map(function ($image, $key) {
return Image::parse($image);
Expand All @@ -62,10 +62,10 @@ public function setAdditionalData(object $input)
}

/**
* @param object $input
* @param $input
* @return $this|static
*/
public static function parse(object $input)
public static function parse($input)
{
return (new self())->setAdditionalData($input);
}
Expand Down
9 changes: 6 additions & 3 deletions src/Models/Locations/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,14 @@ public function __construct(
}

/**
* @param object $input
* @param $input
* @return \LKDev\HetznerCloud\Models\Locations\Location|static
*/
public static function parse(object $input)
public static function parse($input)
{
return new self($input->id, $input->name, $input->description, $input->city, $input->latitude, $input->longitude);
if ($input == null) {
return null;
}
return new self($input->id, $input->name, $input->description, $input->country, $input->city, $input->latitude, $input->longitude);
}
}
Loading

0 comments on commit bdb3879

Please sign in to comment.