Skip to content

Commit 8611051

Browse files
author
Alexander Birkner
committed
Update Tue Jun 14 18:16:07 CEST 2016
1 parent 5ac19dd commit 8611051

32 files changed

+250
-624
lines changed

lib/Nitrapi/Common/Http/Client.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ public function dataGet($url, $headers = null, $options = array()) {
5454
$response = json_decode($e->getResponse()->getBody(), true);
5555
$msg = isset($response['message']) ? $response['message'] : 'Unknown error';
5656
if ($e->getResponse()->getStatusCode() == 503) {
57-
throw new NitrapiMaintenanceException($msg);
57+
throw new NitrapiMaintenanceException();
5858
}
5959
if ($e->getResponse()->getStatusCode() == 428) {
60-
throw new NitrapiConcurrencyException($msg);
60+
throw new NitrapiConcurrencyException();
6161
}
6262
throw new NitrapiHttpErrorException($msg);
6363
}
@@ -94,10 +94,10 @@ public function dataPost($url, $body = null, $headers = null, $options = array()
9494
$response = json_decode($e->getResponse()->getBody(), true);
9595
$msg = isset($response['message']) ? $response['message'] : 'Unknown error';
9696
if ($e->getResponse()->getStatusCode() == 503) {
97-
throw new NitrapiMaintenanceException($msg);
97+
throw new NitrapiMaintenanceException();
9898
}
9999
if ($e->getResponse()->getStatusCode() == 428) {
100-
throw new NitrapiConcurrencyException($msg);
100+
throw new NitrapiConcurrencyException();
101101
}
102102
throw new NitrapiHttpErrorException($msg);
103103
}
@@ -140,10 +140,10 @@ public function dataDelete($url, $body = null, $headers = null, $options = array
140140
$response = json_decode($e->getResponse()->getBody(), true);
141141
$msg = isset($response['message']) ? $response['message'] : 'Unknown error';
142142
if ($e->getResponse()->getStatusCode() == 503) {
143-
throw new NitrapiMaintenanceException($msg);
143+
throw new NitrapiMaintenanceException();
144144
}
145145
if ($e->getResponse()->getStatusCode() == 428) {
146-
throw new NitrapiConcurrencyException($msg);
146+
throw new NitrapiConcurrencyException();
147147
}
148148
throw new NitrapiHttpErrorException($msg);
149149
}
@@ -154,11 +154,7 @@ public function dataDelete($url, $body = null, $headers = null, $options = array
154154
}
155155

156156
protected function checkErrors(Response $response, $responseCode = 200) {
157-
$json = @json_decode($response->getBody(), true);
158-
159-
if (is_array($json) && isset($json['status']) && $json['status'] == "error") {
160-
throw new NitrapiHttpErrorException($json["message"]);
161-
}
157+
$json = json_decode($response->getBody(), true);
162158

163159
$allowedPorts = array();
164160
$allowedPorts[] = $responseCode;
@@ -169,5 +165,9 @@ protected function checkErrors(Response $response, $responseCode = 200) {
169165
if (!in_array($response->getStatusCode(), $allowedPorts)) {
170166
throw new NitrapiHttpErrorException("Invalid http status code " . $response->getStatusCode());
171167
}
168+
169+
if (isset($json['status']) && $json['status'] == "error") {
170+
throw new NitrapiHttpErrorException("Got Error from API " . $json["message"]);
171+
}
172172
}
173173
}

lib/Nitrapi/Customer/Customer.php

-26
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Nitrapi\Customer;
44

5-
use Nitrapi\Common\Exceptions\NitrapiHttpErrorException;
65
use Nitrapi\Nitrapi;
76

87
class Customer {
@@ -62,29 +61,4 @@ public function get($field = null) {
6261
if($field === null) return $this->data['user'];
6362
return $this->data['user'][$field];
6463
}
65-
66-
/**
67-
* Returns a webinterface token
68-
*
69-
* @return string
70-
*/
71-
public function getWebinterfaceToken() {
72-
$token = $this->api->dataGet('user/webinterface_token');
73-
return $token['token']['token'];
74-
}
75-
76-
/**
77-
* Deletes all webinterface tokens. This logs out all users from your webinterface.
78-
*
79-
* @return string
80-
*/
81-
public function deleteWebinterfaceTokens() {
82-
try {
83-
$this->api->dataDelete('user/webinterface_token');
84-
} catch (NitrapiHttpErrorException $e) {
85-
return false;
86-
}
87-
88-
return true;
89-
}
9064
}

lib/Nitrapi/Nitrapi.php

-6
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ public function __construct($accessToken, $options = array(), $url = NITRAPI_LIV
2323
if (isset($options['user_id']) && !empty($options['user_id']))
2424
$query['user_id'] = (int)$options['user_id'];
2525

26-
if (isset($options['user_ip']) && filter_var($options['user_ip'], FILTER_VALIDATE_IP))
27-
$query['user_ip'] = $options['user_ip'];
28-
29-
if (isset($options['locale']) && !empty($options['locale']))
30-
$query['locale'] = (string)$options['locale'];
31-
3226
$options['query'] = $query;
3327
parent::__construct($url, $options);
3428
}

lib/Nitrapi/Order/Order.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Nitrapi\Payment;
4+
5+
use Nitrapi\Nitrapi;
6+
7+
class Order
8+
{
9+
protected $nitrapi = null;
10+
11+
public function __construct(Nitrapi &$nitrapi) {
12+
$this->nitrapi = $nitrapi;
13+
}
14+
15+
public function process($rentalTime, array $parts, $imageId, $locationId=2, $type='cloud_server') {
16+
$price = Price::getPriceStructure($this->nitrapi, $type);
17+
$this->nitrapi->dataPost("order/order/$type", [
18+
'price' => $price->getBestPrice($rentalTime, $parts),
19+
'rental_time' => $rentalTime,
20+
'parts' => $parts,
21+
'image_id' => $imageId,
22+
'location' => $locationId
23+
]);
24+
25+
// TODO: Proper errors/success on NitrAPI side
26+
return true;
27+
}
28+
}

lib/Nitrapi/Payment/Price.php renamed to lib/Nitrapi/Order/Price.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Nitrapi\Payment;
44

5-
use Nitrapi\Payment\PricePart;
5+
use Nitrapi\Nitrapi;
66
use Nitrapi\Common\Exceptions\NitrapiPaymentException;
77

88
class Price
@@ -14,9 +14,8 @@ public function __construct($rentalTimes) {
1414
$this->rentalTimes = $rentalTimes;
1515
}
1616

17-
public static function getPriceStructure($nitrapi, $type='cloud_server') {
17+
public static function getPriceStructure(Nitrapi $nitrapi, $type='cloud_server') {
1818
$priceStructure = $nitrapi->dataGet("order/pricing/$type");
19-
2019
$price = new Price($priceStructure['prices']['rental_times']);
2120
foreach ($priceStructure['prices']['parts'] as $pricePart)
2221
$price->addPart(new PricePart($pricePart));

lib/Nitrapi/Payment/PricePart.php renamed to lib/Nitrapi/Order/PricePart.php

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Nitrapi\Payment;
44

5+
56
use Nitrapi\Nitrapi;
67
use Nitrapi\Common\Exceptions\NitrapiPaymentException;
78

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Nitrapi\Order\Pricing;
4+
5+
abstract class DimensionPricing extends Pricing {
6+
7+
public function getPrices($location_id) {
8+
return $this->nitrapi->dataGet("/order/pricing/" . $this->type, null, [
9+
'query' => [
10+
'location' => $location_id
11+
]
12+
]);
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Nitrapi\Order\Pricing;
4+
5+
abstract class PartPricing extends Pricing{
6+
7+
8+
9+
}

lib/Nitrapi/Order/Pricing/Pricing.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Nitrapi\Order\Pricing;
4+
5+
use Nitrapi\Nitrapi;
6+
7+
abstract class Pricing {
8+
9+
/**
10+
* @var Nitrapi
11+
*/
12+
protected $nitrapi;
13+
14+
public function __construct(Nitrapi $nitrapi)
15+
{
16+
$this->nitrapi = $nitrapi;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Nitrapi\Order\Pricing\Products;
4+
5+
use Nitrapi\Order\Pricing\DimensionPricing;
6+
7+
class Gameserver extends DimensionPricing {
8+
9+
protected $type = "gameserver";
10+
11+
}

lib/Nitrapi/Payment/Order.php

-13
This file was deleted.

lib/Nitrapi/Services/CloudServers/CloudServer.php

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Nitrapi\Nitrapi;
66
use Nitrapi\Services\Service;
7+
use Nitrapi\Services\CloudServers\Iamge;
78

89
class CloudServer extends Service
910
{
@@ -28,4 +29,12 @@ public function refresh() {
2829
public function getDetails() {
2930
return new CloudServerDetails($this->info['cloud_server']);
3031
}
32+
33+
public static function getAvailableImages(Nitrapi &$nitrapi) {
34+
$images = $nitrapi->dataGet('/information/cloud_servers/images');
35+
$imgs = [];
36+
foreach ($images['images'] as $image)
37+
$imgs[] = new Image($image['id'], $image['name'], $image['is_windows']);
38+
return $imgs;
39+
}
3140
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Nitrapi\Services\CloudServers;
4+
5+
class Image
6+
{
7+
protected $id = null;
8+
protected $name = null;
9+
protected $isWindows = false;
10+
11+
public function __construct($id, $name, $isWindows) {
12+
$this->id = $id;
13+
$this->name = $name;
14+
$this->isWindows = $isWindows;
15+
}
16+
17+
public function getId() {
18+
return $this->id;
19+
}
20+
21+
public function getName() {
22+
return $this->name;
23+
}
24+
25+
public function isWindows() {
26+
return $this->isWindows;
27+
}
28+
}

lib/Nitrapi/Services/Gameservers/FileServer/FileServer.php

+3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ public function uploadFile($file, $path, $name) {
8787
* @throws \Nitrapi\Common\Exceptions\NitrapiHttpErrorException
8888
*/
8989
public function writeFile($path, $name, $content) {
90+
if (empty($content)) {
91+
throw new NitrapiErrorException('Not content provided.');
92+
}
9093
$upload = $this->uploadToken($path, $name);
9194

9295
try {

lib/Nitrapi/Services/Gameservers/Games/TS3Musicbot.php

-21
This file was deleted.

lib/Nitrapi/Services/Gameservers/Gameserver.php

+27-10
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,12 @@ public function refresh() {
3333
/**
3434
* Returns informations about the gameserver
3535
*
36-
* @return GameserverDetails
36+
* @return mixed
3737
*/
3838
public function getDetails() {
3939
return new GameserverDetails($this->info['gameserver']);
4040
}
4141

42-
/**
43-
* Returns available features from the gameserver
44-
*
45-
* @return GameserverFeatures
46-
*/
47-
public function getFeatures() {
48-
return new GameserverFeatures($this->info['gameserver']['game_specific']['features']);
49-
}
50-
5142
public function getCustomerSettings() {
5243
return new CustomerSettings($this, $this->info['gameserver']['settings']);
5344
}
@@ -308,6 +299,16 @@ public function getCallbackHandler() {
308299
return new CallbackHandler($this);
309300
}
310301

302+
/**
303+
* Returns the ddos history
304+
*
305+
* @return array
306+
*/
307+
public function getDDoSHistory() {
308+
$url = "services/" . $this->getId() . "/gameservers/ddos";
309+
return $this->getApi()->dataGet($url);
310+
}
311+
311312
/**
312313
* Returns the task manager
313314
*
@@ -343,6 +344,22 @@ public function getStats($hours = 24) {
343344
])['stats'];
344345
}
345346

347+
/**
348+
* Returns the last log entries. You can optionally
349+
* provide a page number.
350+
*
351+
* @param int $hours
352+
* @return array
353+
*/
354+
public function getLogs($page = 1) {
355+
$url = "services/" . $this->getId() . "/gameservers/logs";
356+
return $this->getApi()->dataGet($url, null, [
357+
'query' => [
358+
'page' => $page
359+
]
360+
]);
361+
}
362+
346363
/**
347364
* Sends a command directly into the game server
348365
*

0 commit comments

Comments
 (0)