44
55use GuzzleHttp \Client as GuzzleClient ;
66use GuzzleHttp \Exception \RequestException ;
7- use GuzzleHttp \Message \Response ;
7+ use GuzzleHttp \Psr7 \Response ;
88use Nitrapi \Common \Exceptions \NitrapiConcurrencyException ;
99use Nitrapi \Common \Exceptions \NitrapiException ;
1010use Nitrapi \Common \Exceptions \NitrapiHttpErrorException ;
1111use Nitrapi \Common \Exceptions \NitrapiMaintenanceException ;
1212
1313class 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 ;
0 commit comments