11<?php
2- namespace EventFarm \Restforce \Rest ;
32
4- use Psr \ Http \ Message \ ResponseInterface ;
3+ namespace EventFarm \ Restforce \ Rest ;
54
5+ /**
6+ * Class GuzzleRestClient
7+ *
8+ * @package EventFarm\Restforce\Rest
9+ */
610class GuzzleRestClient implements RestClientInterface
711{
812 const DEFAULT_TIMEOUT_SECONDS = 20.0 ;
@@ -12,6 +16,11 @@ class GuzzleRestClient implements RestClientInterface
1216 /** @var bool */
1317 private $ enableDebugging ;
1418
19+ /**
20+ * GuzzleRestClient constructor.
21+ * @param string $baseUri base uri
22+ * @param bool $enableDebugging debugging status
23+ */
1524 public function __construct (
1625 string $ baseUri ,
1726 bool $ enableDebugging = false
@@ -20,7 +29,14 @@ public function __construct(
2029 $ this ->setBaseUriForRestClient ($ baseUri );
2130 }
2231
23- public function setBaseUriForRestClient (string $ baseUri ): void
32+ /**
33+ * Set base uri for client
34+ *
35+ * @param string $baseUri base uri
36+ *
37+ * @return void
38+ */
39+ public function setBaseUriForRestClient (string $ baseUri )
2440 {
2541 if (!$ this ->containsTrailingSlash ($ baseUri )) {
2642 $ baseUri .= '/ ' ;
@@ -34,68 +50,108 @@ public function setBaseUriForRestClient(string $baseUri): void
3450 $ this ->client = new \GuzzleHttp \Client ($ config );
3551 }
3652
53+ /**
54+ * Get method
55+ *
56+ * @param string $path path
57+ * @param array $queryParameters parameters
58+ * @param array $headers headers
59+ * @param float|null $timeoutSeconds timeout
60+ *
61+ * @return mixed
62+ * @throws \GuzzleHttp\Exception\GuzzleException
63+ */
3764 public function get (
3865 string $ path ,
3966 array $ queryParameters = [],
4067 array $ headers = [],
41- ? float $ timeoutSeconds = null
42- ): ResponseInterface {
68+ float $ timeoutSeconds = null
69+ ) {
4370 return $ this ->client ->request (
4471 'GET ' ,
4572 $ path ,
4673 [
4774 'timeout ' => $ timeoutSeconds ,
4875 'headers ' => $ headers ,
49- 'query ' => $ queryParameters ,
50- 'http_errors ' => false ,
76+ 'query ' => $ queryParameters
5177 ]
5278 );
5379 }
5480
81+ /**
82+ * Post method
83+ *
84+ * @param string $path path
85+ * @param array $formParameters parameters
86+ * @param array $headers headers
87+ * @param float|null $timeoutSeconds timeout
88+ *
89+ * @return mixed
90+ * @throws \GuzzleHttp\Exception\GuzzleException
91+ */
5592 public function post (
5693 string $ path ,
5794 array $ formParameters = [],
5895 array $ headers = [],
59- ? float $ timeoutSeconds = null
60- ): ResponseInterface {
96+ float $ timeoutSeconds = null
97+ ) {
6198 return $ this ->client ->request (
6299 'POST ' ,
63100 $ path ,
64101 [
65102 'timeout ' => $ timeoutSeconds ,
66103 'headers ' => $ headers ,
67- 'form_params ' => $ formParameters ,
68- 'http_errors ' => false ,
104+ 'form_params ' => $ formParameters
69105 ]
70106 );
71107 }
72108
109+ /**
110+ * Post method JSON formatted
111+ *
112+ * @param string $path path
113+ * @param array $jsonArray parameters
114+ * @param array $headers headers
115+ * @param float|null $timeoutSeconds timeout
116+ *
117+ * @return mixed
118+ * @throws \GuzzleHttp\Exception\GuzzleException
119+ */
73120 public function postJson (
74121 string $ path ,
75122 array $ jsonArray = [],
76123 array $ headers = [],
77- ? float $ timeoutSeconds = null
78- ): ResponseInterface {
124+ float $ timeoutSeconds = null
125+ ) {
79126 $ headers ['Content-Type ' ] = 'application/json ' ;
80-
81127 return $ this ->client ->request (
82128 'POST ' ,
83129 $ path ,
84130 [
85131 'timeout ' => $ timeoutSeconds ,
86132 'headers ' => $ headers ,
87- 'json ' => $ jsonArray ,
88- 'http_errors ' => false ,
133+ 'json ' => $ jsonArray
89134 ]
90135 );
91136 }
92137
138+ /**
139+ * Patch method JSON formatted
140+ *
141+ * @param string $path path
142+ * @param array $jsonArray parameters
143+ * @param array $headers headers
144+ * @param float|null $timeoutSeconds timeout
145+ *
146+ * @return mixed
147+ * @throws \GuzzleHttp\Exception\GuzzleException
148+ */
93149 public function patchJson (
94150 string $ path ,
95151 array $ jsonArray = [],
96152 array $ headers = [],
97- ? float $ timeoutSeconds = null
98- ): ResponseInterface {
153+ float $ timeoutSeconds = null
154+ ) {
99155 $ headers ['Content-Type ' ] = 'application/json ' ;
100156
101157 return $ this ->client ->request (
@@ -104,13 +160,49 @@ public function patchJson(
104160 [
105161 'timeout ' => $ timeoutSeconds ,
106162 'headers ' => $ headers ,
107- 'json ' => $ jsonArray ,
108- 'http_errors ' => false ,
163+ 'json ' => $ jsonArray
164+ ]
165+ );
166+ }
167+
168+ /**
169+ * Put method CSV formatted
170+ *
171+ * @param string $path path
172+ * @param string $filePath file path
173+ * @param array $headers headers
174+ * @param float|null $timeoutSeconds timeout
175+ *
176+ * @return mixed
177+ * @throws \GuzzleHttp\Exception\GuzzleException
178+ */
179+ public function putCsv (
180+ string $ path ,
181+ string $ filePath ,
182+ array $ headers = [],
183+ float $ timeoutSeconds = null
184+ ) {
185+ $ headers ['Content-Type ' ] = 'text/csv ' ;
186+
187+ return $ this ->client ->request (
188+ 'PUT ' ,
189+ $ path ,
190+ [
191+ 'timeout ' => $ timeoutSeconds ,
192+ 'headers ' => $ headers ,
193+ 'body ' => fopen ($ filePath , 'r ' )
109194 ]
110195 );
111196 }
112197
113- private function containsTrailingSlash (string $ baseUri ): bool
198+ /**
199+ * Check if contains trailing slash
200+ *
201+ * @param string $baseUri base uri
202+ *
203+ * @return bool
204+ */
205+ private function containsTrailingSlash (string $ baseUri )
114206 {
115207 return substr ($ baseUri , -1 ) === '/ ' ;
116208 }
0 commit comments