Skip to content

Commit 5f104d1

Browse files
author
Alexander Birkner
committed
Update Thu Nov 19 17:30:59 CET 2015
1 parent 74833fc commit 5f104d1

17 files changed

+324
-52
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
vendor/
44
composer.phar
55
composer.lock
6-
/examples/
6+
/examples/
7+
.DS_Store

composer.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
},
2323
"require": {
2424
"php" : ">=5.4",
25-
"guzzlehttp/guzzle": "~5.0"
25+
"guzzlehttp/guzzle": "~6.0"
26+
},
27+
"require-dev": {
28+
"phpunit/phpunit": "~5.0"
2629
}
27-
}
30+
}

lib/Nitrapi/Common/Http/Client.php

+28-19
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44

55
use GuzzleHttp\Client as GuzzleClient;
66
use GuzzleHttp\Exception\RequestException;
7-
use GuzzleHttp\Message\Response;
7+
use GuzzleHttp\Psr7\Response;
88
use Nitrapi\Common\Exceptions\NitrapiConcurrencyException;
99
use Nitrapi\Common\Exceptions\NitrapiException;
1010
use Nitrapi\Common\Exceptions\NitrapiHttpErrorException;
1111
use Nitrapi\Common\Exceptions\NitrapiMaintenanceException;
1212

1313
class Client extends GuzzleClient
1414
{
15-
const MINIMUM_PHP_VERSION = '5.3.0';
15+
const MINIMUM_PHP_VERSION = '5.4.0';
16+
17+
protected $defaultQuery = [];
1618

1719
public function __construct($baseUrl = '', $config = null) {
1820
if (PHP_VERSION < self::MINIMUM_PHP_VERSION) {
@@ -22,7 +24,10 @@ public function __construct($baseUrl = '', $config = null) {
2224
));
2325
}
2426

25-
$config['base_url'] = $baseUrl;
27+
if (isset($config['query'])) {
28+
$this->defaultQuery = $config['query'];
29+
}
30+
$config['base_uri'] = $baseUrl;
2631
parent::__construct($config);
2732
}
2833

@@ -37,15 +42,16 @@ public function dataGet($url, $headers = null, $options = array()) {
3742
if (is_array($headers)) {
3843
$options['headers'] = $headers;
3944
}
45+
if (is_array($options) && isset($options['query'])) {
46+
$options['query'] = array_merge($options['query'], $this->defaultQuery);
47+
}
4048

41-
$request = $this->createRequest('GET', $url, $options);
42-
43-
$response = $this->send($request);
49+
$response = $this->request('GET', $url, $options);
4450
$this->checkErrors($response);
45-
$json = $response->json();
51+
$json = json_decode($response->getBody(), true);
4652
} catch (RequestException $e) {
4753
if ($e->hasResponse()) {
48-
$response = $e->getResponse()->json();
54+
$response = json_decode($e->getResponse()->getBody(), true);
4955
$msg = isset($response['message']) ? $response['message'] : 'Unknown error';
5056
if ($e->getResponse()->getStatusCode() == 503) {
5157
throw new NitrapiMaintenanceException();
@@ -71,19 +77,21 @@ public function dataGet($url, $headers = null, $options = array()) {
7177
public function dataPost($url, $body = null, $headers = null, $options = array()) {
7278
try {
7379
if (is_array($body)) {
74-
$options['body'] = $body;
80+
$options['form_params'] = $body;
7581
}
7682
if (is_array($headers)) {
7783
$options['headers'] = $headers;
7884
}
79-
$request = $this->createRequest('POST', $url, $options);
85+
if (is_array($options) && isset($options['query'])) {
86+
$options['query'] = array_merge($options['query'], $this->defaultQuery);
87+
}
8088

81-
$response = $this->send($request);
89+
$response = $this->request('POST', $url, $options);
8290
$this->checkErrors($response);
83-
$json = $response->json();
91+
$json = json_decode($response->getBody(), true);
8492
} catch (RequestException $e) {
8593
if ($e->hasResponse()) {
86-
$response = $e->getResponse()->json();
94+
$response = json_decode($e->getResponse()->getBody(), true);
8795
$msg = isset($response['message']) ? $response['message'] : 'Unknown error';
8896
if ($e->getResponse()->getStatusCode() == 503) {
8997
throw new NitrapiMaintenanceException();
@@ -117,18 +125,19 @@ public function dataPost($url, $body = null, $headers = null, $options = array()
117125
public function dataDelete($url, $body = null, $headers = null, $options = array()) {
118126
try {
119127
if (is_array($body)) {
120-
$options['body'] = $body;
128+
$options['form_params'] = $body;
121129
}
122130
if (is_array($headers)) {
123131
$options['headers'] = $headers;
124132
}
125-
$request = $this->createRequest('DELETE', $url, $options);
126-
127-
$response = $this->send($request);
133+
if (is_array($options) && isset($options['query'])) {
134+
$options['query'] = array_merge($options['query'], $this->defaultQuery);
135+
}
136+
$response = $this->request('DELETE', $url, $options);
128137
$this->checkErrors($response);
129138
} catch (RequestException $e) {
130139
if ($e->hasResponse()) {
131-
$response = $e->getResponse()->json();
140+
$response = json_decode($e->getResponse()->getBody(), true);
132141
$msg = isset($response['message']) ? $response['message'] : 'Unknown error';
133142
if ($e->getResponse()->getStatusCode() == 503) {
134143
throw new NitrapiMaintenanceException();
@@ -145,7 +154,7 @@ public function dataDelete($url, $body = null, $headers = null, $options = array
145154
}
146155

147156
protected function checkErrors(Response $response, $responseCode = 200) {
148-
$json = $response->json();
157+
$json = json_decode($response->getBody(), true);
149158

150159
$allowedPorts = array();
151160
$allowedPorts[] = $responseCode;

lib/Nitrapi/Customer/Customer.php

+1-22
Original file line numberDiff line numberDiff line change
@@ -5,81 +5,60 @@
55
use Nitrapi\Nitrapi;
66

77
class Customer {
8-
98
private $api;
109
private $data;
1110

1211
public function __construct(Nitrapi $api, $token) {
13-
1412
$this->api = $api;
15-
1613
$this->data = $api->dataGet("user?access_token=" . $token, null);
17-
1814
}
1915

2016
/**
2117
* Returns the user id.
2218
* @return string
2319
*/
2420
public function getUserId() {
25-
2621
return $this->get('user_id');
27-
2822
}
2923

3024
/**
3125
* Returns the username.
3226
* @return string username
3327
*/
3428
public function getUsername() {
35-
3629
return $this->get('username');
37-
3830
}
3931

4032
/**
4133
* Returns credit.
4234
* @return int
4335
*/
4436
public function getCredit() {
45-
4637
return $this->get('credit');
47-
4838
}
4939

5040
/**
5141
* Returns email address of the user.
5242
* @return string
5343
*/
5444
public function getEmail() {
55-
5645
return $this->get('email');
57-
5846
}
5947

6048
/**
6149
* Returns the personal details of the user.
6250
* @return array
6351
*/
6452
public function getPersonalData() {
65-
6653
return $this->get('profile');
67-
6854
}
6955

7056
/**
7157
* This function returns the whole data-set
7258
* @return mixed
7359
*/
7460
public function get($field = null) {
75-
76-
if($field === null) {
77-
return $this->data['user'];
78-
}
79-
61+
if($field === null) return $this->data['user'];
8062
return $this->data['user'][$field];
81-
8263
}
83-
84-
8564
}

lib/Nitrapi/Nitrapi.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@ class Nitrapi extends Client
1717
public function __construct($accessToken, $options = array(), $url = NITRAPI_LIVE_URL) {
1818
$this->setAccessToken($accessToken);
1919

20-
parent::__construct($url, $options);
21-
2220
$query = array();
23-
if (!empty($accessToken)) {
24-
$query['access_token'] = $accessToken;
25-
}
26-
if (isset($options['user_id']) && !empty($options['user_id'])) {
21+
if (!empty($accessToken)) $query['access_token'] = $accessToken;
22+
if (isset($options['user_id']) && !empty($options['user_id']))
2723
$query['user_id'] = (int)$options['user_id'];
28-
}
29-
$this->setDefaultOption('query', $query);
24+
25+
$options['query'] = $query;
26+
parent::__construct($url, $options);
3027
}
3128

3229
/**

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

+2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ public function downloadFile($file, $path, $name) {
194194
public function readFile($file) {
195195
$download = $this->downloadToken($file);
196196

197+
// Here we use the GuzzleClient API directly. This is intended, but
198+
// should remain a special case. Don't copy this code.
197199
$request = $this->service->getApi()->createRequest('GET', $download['token']['url']);
198200
$response = $this->service->getApi()->send($request);
199201

phpunit.xml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="./test/bootstrap.php" colors="true">
3+
<testsuites>
4+
<testsuite name="importer-tests">
5+
<directory>./test/tests/</directory>
6+
</testsuite>
7+
</testsuites>
8+
</phpunit>

test/NitrapiTestCase.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Nitrapi\Tests;
4+
5+
use Nitrapi\Nitrapi;
6+
use GuzzleHttp\Client;
7+
use GuzzleHttp\Handler\MockHandler;
8+
use GuzzleHttp\HandlerStack;
9+
use GuzzleHttp\Psr7\Response;
10+
11+
abstract class NitrapiTestCase extends \PHPUnit_Framework_TestCase {
12+
function nitrapiMock($urls) {
13+
$responses = [];
14+
foreach ($urls as $url => $params)
15+
$responses[] = new Response(200, $params, file_get_contents(__DIR__ . '/fixtures/' . $url . '.response'));
16+
$mock = new MockHandler($responses);
17+
$handler = HandlerStack::create($mock);
18+
return new Nitrapi(null, ['handler' => $handler], null);
19+
}
20+
}

test/bootstrap.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
error_reporting(E_ALL | E_STRICT);
4+
require dirname(__DIR__) . '/vendor/autoload.php';
5+
require dirname(__DIR__) . '/test/NitrapiTestCase.php';
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"status": "success",
3+
"data": {
4+
"current_page": 1,
5+
"logs_per_page": 40,
6+
"page_count": 1,
7+
"log_count": 5,
8+
"logs": [
9+
{
10+
"user": "Tyrola",
11+
"category": "server",
12+
"message": "Restart",
13+
"created_at": "2015-10-07T00:31:23.000+02:00",
14+
"ip": "46.237.218.140",
15+
"admin": true
16+
},
17+
{
18+
"user": "Tyrola",
19+
"category": "filebrowser",
20+
"message": "Has edited the file server.properties",
21+
"created_at": "2015-10-07T00:31:19.000+02:00",
22+
"ip": "46.237.218.140",
23+
"admin": true
24+
},
25+
{
26+
"user": "Tyrola",
27+
"category": "server",
28+
"message": "Restart",
29+
"created_at": "2015-09-28T14:17:07.000+02:00",
30+
"ip": "46.237.218.140",
31+
"admin": true
32+
},
33+
{
34+
"user": "Tyrola",
35+
"category": "server",
36+
"message": "Restart",
37+
"created_at": "2015-09-23T01:18:42.000+02:00",
38+
"ip": "46.237.218.140",
39+
"admin": true
40+
},
41+
{
42+
"user": "Tyrola",
43+
"category": "server",
44+
"message": "Restart",
45+
"created_at": "2015-09-22T23:29:15.000+02:00",
46+
"ip": "46.237.200.179",
47+
"admin": true
48+
}]
49+
}
50+
}

0 commit comments

Comments
 (0)