4
4
5
5
use GuzzleHttp \Client as GuzzleClient ;
6
6
use GuzzleHttp \Exception \RequestException ;
7
- use GuzzleHttp \Message \Response ;
7
+ use GuzzleHttp \Psr7 \Response ;
8
8
use Nitrapi \Common \Exceptions \NitrapiConcurrencyException ;
9
9
use Nitrapi \Common \Exceptions \NitrapiException ;
10
10
use Nitrapi \Common \Exceptions \NitrapiHttpErrorException ;
11
11
use Nitrapi \Common \Exceptions \NitrapiMaintenanceException ;
12
12
13
13
class Client extends GuzzleClient
14
14
{
15
- const MINIMUM_PHP_VERSION = '5.3.0 ' ;
15
+ const MINIMUM_PHP_VERSION = '5.4.0 ' ;
16
+
17
+ protected $ defaultQuery = [];
16
18
17
19
public function __construct ($ baseUrl = '' , $ config = null ) {
18
20
if (PHP_VERSION < self ::MINIMUM_PHP_VERSION ) {
@@ -22,7 +24,10 @@ public function __construct($baseUrl = '', $config = null) {
22
24
));
23
25
}
24
26
25
- $ config ['base_url ' ] = $ baseUrl ;
27
+ if (isset ($ config ['query ' ])) {
28
+ $ this ->defaultQuery = $ config ['query ' ];
29
+ }
30
+ $ config ['base_uri ' ] = $ baseUrl ;
26
31
parent ::__construct ($ config );
27
32
}
28
33
@@ -37,15 +42,16 @@ public function dataGet($url, $headers = null, $options = array()) {
37
42
if (is_array ($ headers )) {
38
43
$ options ['headers ' ] = $ headers ;
39
44
}
45
+ if (is_array ($ options ) && isset ($ options ['query ' ])) {
46
+ $ options ['query ' ] = array_merge ($ options ['query ' ], $ this ->defaultQuery );
47
+ }
40
48
41
- $ request = $ this ->createRequest ('GET ' , $ url , $ options );
42
-
43
- $ response = $ this ->send ($ request );
49
+ $ response = $ this ->request ('GET ' , $ url , $ options );
44
50
$ this ->checkErrors ($ response );
45
- $ json = $ response ->json ( );
51
+ $ json = json_decode ( $ response ->getBody (), true );
46
52
} catch (RequestException $ e ) {
47
53
if ($ e ->hasResponse ()) {
48
- $ response = $ e ->getResponse ()->json ( );
54
+ $ response = json_decode ( $ e ->getResponse ()->getBody (), true );
49
55
$ msg = isset ($ response ['message ' ]) ? $ response ['message ' ] : 'Unknown error ' ;
50
56
if ($ e ->getResponse ()->getStatusCode () == 503 ) {
51
57
throw new NitrapiMaintenanceException ();
@@ -71,19 +77,21 @@ public function dataGet($url, $headers = null, $options = array()) {
71
77
public function dataPost ($ url , $ body = null , $ headers = null , $ options = array ()) {
72
78
try {
73
79
if (is_array ($ body )) {
74
- $ options ['body ' ] = $ body ;
80
+ $ options ['form_params ' ] = $ body ;
75
81
}
76
82
if (is_array ($ headers )) {
77
83
$ options ['headers ' ] = $ headers ;
78
84
}
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
+ }
80
88
81
- $ response = $ this ->send ( $ request );
89
+ $ response = $ this ->request ( ' POST ' , $ url , $ options );
82
90
$ this ->checkErrors ($ response );
83
- $ json = $ response ->json ( );
91
+ $ json = json_decode ( $ response ->getBody (), true );
84
92
} catch (RequestException $ e ) {
85
93
if ($ e ->hasResponse ()) {
86
- $ response = $ e ->getResponse ()->json ( );
94
+ $ response = json_decode ( $ e ->getResponse ()->getBody (), true );
87
95
$ msg = isset ($ response ['message ' ]) ? $ response ['message ' ] : 'Unknown error ' ;
88
96
if ($ e ->getResponse ()->getStatusCode () == 503 ) {
89
97
throw new NitrapiMaintenanceException ();
@@ -117,18 +125,19 @@ public function dataPost($url, $body = null, $headers = null, $options = array()
117
125
public function dataDelete ($ url , $ body = null , $ headers = null , $ options = array ()) {
118
126
try {
119
127
if (is_array ($ body )) {
120
- $ options ['body ' ] = $ body ;
128
+ $ options ['form_params ' ] = $ body ;
121
129
}
122
130
if (is_array ($ headers )) {
123
131
$ options ['headers ' ] = $ headers ;
124
132
}
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 );
128
137
$ this ->checkErrors ($ response );
129
138
} catch (RequestException $ e ) {
130
139
if ($ e ->hasResponse ()) {
131
- $ response = $ e ->getResponse ()->json ( );
140
+ $ response = json_decode ( $ e ->getResponse ()->getBody (), true );
132
141
$ msg = isset ($ response ['message ' ]) ? $ response ['message ' ] : 'Unknown error ' ;
133
142
if ($ e ->getResponse ()->getStatusCode () == 503 ) {
134
143
throw new NitrapiMaintenanceException ();
@@ -145,7 +154,7 @@ public function dataDelete($url, $body = null, $headers = null, $options = array
145
154
}
146
155
147
156
protected function checkErrors (Response $ response , $ responseCode = 200 ) {
148
- $ json = $ response ->json ( );
157
+ $ json = json_decode ( $ response ->getBody (), true );
149
158
150
159
$ allowedPorts = array ();
151
160
$ allowedPorts [] = $ responseCode ;
0 commit comments