Skip to content

Commit bdb3879

Browse files
committed
Implement Examples & Bugfixing
1 parent e9ab838 commit bdb3879

25 files changed

+185
-102
lines changed

examples/bootstrap.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
require_once __DIR__.'/../vendor/autoload.php';
3+
$apiKey = '{InsertYourAPIKeyHear}';
4+
5+
$hetznerClient = new \LKDev\HetznerCloud\HetznerAPIClient($apiKey);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
require_once __DIR__.'/../bootstrap.php';
3+
4+
$servers = new \LKDev\HetznerCloud\Models\Servers\Servers();
5+
$serverId = 494200;
6+
$server = $servers->get($serverId);
7+
var_dump($server);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
require_once __DIR__.'/../bootstrap.php';
3+
4+
$servers = new \LKDev\HetznerCloud\Models\Servers\Servers();
5+
$serverTypes = new \LKDev\HetznerCloud\Models\Servers\Types\ServerTypes();
6+
foreach ($serverTypes->all() as $serverType) {
7+
echo $serverType->name.PHP_EOL;
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
require_once __DIR__.'/../bootstrap.php';
3+
4+
$servers = new \LKDev\HetznerCloud\Models\Servers\Servers();
5+
foreach ($servers->all() as $server) {
6+
echo 'ID: '.$server->id.' Name:'.$server->name.' Status: '.$server->status.PHP_EOL;
7+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
require_once __DIR__.'/../bootstrap.php';
3+
4+
$servers = new \LKDev\HetznerCloud\Models\Servers\Servers();
5+
$serverId = 494200;
6+
$server = $servers->get($serverId);
7+
echo 'Server: '.$server->name.PHP_EOL;
8+
echo "Perform Shutdown now:".PHP_EOL;
9+
/**
10+
* @var \LKDev\HetznerCloud\Models\Servers\Server $server
11+
*/
12+
$action = $server->shutdown();
13+
14+
echo "Reply from API: Action ID: ".$action->id.' '.$action->command.' '.$action->started.PHP_EOL;
15+
16+
echo 'Wait some seconds that the server could shutdown.'.PHP_EOL;
17+
sleep(5);
18+
echo "Get the Server from the API:".PHP_EOL;
19+
$server = $servers->get($serverId);
20+
echo "Server status: ".$server->status.PHP_EOL;
21+
echo "Let's start it again!";
22+
$server->powerOn();
23+
echo 'Wait some seconds that the server could startup.'.PHP_EOL;
24+
sleep(5);
25+
echo "Get the Server from the API:".PHP_EOL;
26+
$server = $servers->get($serverId);
27+
echo "Server status: ".$server->status.PHP_EOL;

src/HetznerAPIClient.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class HetznerAPIClient
3939
public function __construct(string $apiToken, $baseUrl = 'https://api.hetzner.cloud/v1/')
4040
{
4141
$this->apiToken = $apiToken;
42+
$this->baseUrl = $baseUrl;
4243
self::$hetznerApiClient = $this;
4344
self::$httpClient = new GuzzleClient($this);
4445
}
@@ -65,7 +66,9 @@ public function getBaseUrl(): string
6566
*/
6667
public static function throwError(ResponseInterface $response)
6768
{
68-
throw new APIException($response);
69+
var_dump(json_decode((string) $response->getBody()));
70+
die();
71+
// throw new APIException($response, ->error->code);
6972
}
7073

7174
/**
@@ -75,8 +78,9 @@ public static function throwError(ResponseInterface $response)
7578
*/
7679
public static function hasError(ResponseInterface $response)
7780
{
78-
if (property_exists($response, 'error') || $response->getStatusCode() !== 200) {
81+
if ((property_exists($response, 'error')) || ($response->getStatusCode() <= 200 && $response->getStatusCode() >= 300)) {
7982
self::throwError($response);
83+
8084
return true;
8185
}
8286

src/Models/Actions/Action.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Action extends Model
4040
public $resources;
4141

4242
/**
43-
* @var object|null
43+
* @var |null
4444
*/
4545
public $error;
4646

@@ -58,16 +58,16 @@ class Action extends Model
5858
* @param string $started
5959
* @param string $finished
6060
* @param array $resources
61-
* @param null|object $error
61+
* @param null| $error
6262
*/
6363
public function __construct(
6464
int $id,
6565
string $command,
6666
int $progress,
6767
string $started,
6868
string $finished,
69-
array $resources,
70-
object $error,
69+
array $resources = null,
70+
$error = null,
7171
string $root_password = null
7272
) {
7373
$this->id = $id;
@@ -82,11 +82,15 @@ public function __construct(
8282
}
8383

8484
/**
85-
* @param object $input
85+
* @param $input
8686
* @return \LKDev\HetznerCloud\Models\Actions\Action|static
8787
*/
88-
public static function parse(object $input)
88+
public static function parse($input)
8989
{
90-
return new self($input->id, $input->command, $input->status, $input->started, $input->finished, $input->resources, $input->error, $input->root_password);
90+
if ($input == null) {
91+
return null;
92+
}
93+
94+
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));
9195
}
9296
}

src/Models/Actions/Actions.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ public function get($actionId): Action
5858
}
5959

6060
/**
61-
* @param object $input
61+
* @param $input
6262
* @return $this
6363
*/
64-
public function setAdditionalData(object $input)
64+
public function setAdditionalData( $input)
6565
{
6666
$this->actions = collect($input->actions)->map(function ($action, $key) {
6767
return Action::parse($action);
@@ -71,10 +71,10 @@ public function setAdditionalData(object $input)
7171
}
7272

7373
/**
74-
* @param object $input
74+
* @param $input
7575
* @return $this|static
7676
*/
77-
public static function parse(object $input)
77+
public static function parse($input)
7878
{
7979
return (new self())->setAdditionalData($input);
8080
}

src/Models/Datacenters/Datacenter.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public function __construct(
6161
string $name,
6262
string $description,
6363
Location $location,
64-
array $server_types,
65-
bool $recommendation
64+
array $server_types = null,
65+
bool $recommendation = null
6666
) {
6767
$this->id = $id;
6868
$this->name = $name;
@@ -74,11 +74,14 @@ public function __construct(
7474
}
7575

7676
/**
77-
* @param object $input
77+
* @param $input
7878
* @return \LKDev\HetznerCloud\Models\Datacenters\Datacenter|static
7979
*/
80-
public static function parse(object $input)
80+
public static function parse($input)
8181
{
82+
if ($input == null) {
83+
return null;
84+
}
8285
return new self($input->id,$input->name,$input->description,Location::parse($input->location),$input->server_types,$input->recommendation);
8386
}
8487
}

src/Models/Datacenters/Datacenters.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public function get(int $datacenterId): Datacenter
5050
}
5151

5252
/**
53-
* @param object $input
53+
* @param $input
5454
* @return $this
5555
*/
56-
public function setAdditionalData(object $input)
56+
public function setAdditionalData( $input)
5757
{
5858
$this->locations = collect($input->datacenters)->map(function ($datacenter, $key) {
5959
return Datacenter::parse($datacenter);
@@ -63,10 +63,10 @@ public function setAdditionalData(object $input)
6363
}
6464

6565
/**
66-
* @param object $input
66+
* @param $input
6767
* @return $this|static
6868
*/
69-
public static function parse(object $input)
69+
public static function parse($input)
7070
{
7171
return (new self())->setAdditionalData($input);
7272
}

0 commit comments

Comments
 (0)