22
33namespace ChartBlocks ;
44
5- use Guzzle \Http \Client as HttpClient ;
6- use Guzzle \Http \Message \Request as HttpRequest ;
5+ use ChartBlocks \Repository \Chart ;
6+ use ChartBlocks \Repository \ChartData ;
7+ use ChartBlocks \Repository \DataSet ;
8+ use ChartBlocks \Repository \Profile ;
9+ use ChartBlocks \Repository \RepositoryInterface ;
10+ use ChartBlocks \Repository \SessionToken ;
11+ use ChartBlocks \Repository \Statistics ;
12+ use ChartBlocks \Repository \User ;
13+ use Closure ;
14+ use GuzzleHttp \Client as HttpClient ;
15+ use GuzzleHttp \HandlerStack ;
16+ use GuzzleHttp \Psr7 ;
17+ use InvalidArgumentException ;
18+ use Psr \Http \Message \RequestInterface ;
19+ use RuntimeException ;
20+ use function GuzzleHttp \choose_handler ;
721
822/**
923 * Client for managing connection to ChartBlocks REST API
1024 *
11- * @param \ChartBlocks\Repository\ Chart $chart
12- * @param \ChartBlocks\Repository\ DataSet $dataSet
13- * @param \ChartBlocks\Repository\ ChartData $chartData
14- * @param \ChartBlocks\Repository\ Profile $profile
15- * @param \ChartBlocks\Repository\ SessionToken $sessionToken
16- * @param \ChartBlocks\Repository\ Statistics $statistics
17- * @param \ChartBlocks\Repository\ User $user
25+ * @param Chart $chart
26+ * @param DataSet $dataSet
27+ * @param ChartData $chartData
28+ * @param Profile $profile
29+ * @param SessionToken $sessionToken
30+ * @param Statistics $statistics
31+ * @param User $user
1832 *
1933 */
2034class Client {
@@ -30,7 +44,6 @@ class Client {
3044
3145 protected $ config ;
3246 protected $ signature ;
33- protected $ exceptionHandler ;
3447 protected $ httpClient ;
3548 protected $ defaultApiUrl = 'https://api.chartblocks.com/v1/ ' ;
3649 protected $ repositories = array (
@@ -55,13 +68,12 @@ public function __construct(array $config = array()) {
5568 /**
5669 *
5770 * @param string $name
58- * @return \ChartBlocks\Repository\RepositoryInterface
59- * @throws Exception
71+ * @return RepositoryInterface
6072 */
6173 public function getRepository ($ name ) {
6274 $ repo = lcfirst (trim ($ name ));
6375 if (false === array_key_exists ($ repo , $ this ->repositories )) {
64- throw new \ InvalidArgumentException ("Repository $ repo does not exist " );
76+ throw new InvalidArgumentException ("Repository $ repo does not exist " );
6577 }
6678
6779 if (is_string ($ this ->repositories [$ repo ])) {
@@ -76,7 +88,7 @@ public function getRepository($name) {
7688 * Tries to load a repository using the syntax $this->chart
7789 *
7890 * @param string $name
79- * @return \ChartBlocks\Repository\ RepositoryInterface
91+ * @return RepositoryInterface
8092 * @throws Exception
8193 */
8294 public function __get ($ name ) {
@@ -91,7 +103,8 @@ public function __get($name) {
91103 *
92104 * @return string
93105 */
94- public function getApiUrl () {
106+ public function getApiUrl (): string
107+ {
95108 if (array_key_exists ('api_url ' , $ this ->config )) {
96109 return $ this ->parseApiUrl ($ this ->config ['api_url ' ]);
97110 }
@@ -104,11 +117,13 @@ public function getApiUrl() {
104117 return $ this ->defaultApiUrl ;
105118 }
106119
107- protected function parseApiUrl ($ url ) {
120+ protected function parseApiUrl ($ url ): string
121+ {
108122 return rtrim ($ url , '/ ' ) . '/ ' ;
109123 }
110124
111- protected function parseApiPath ($ path ) {
125+ protected function parseApiPath ($ path ): string
126+ {
112127 return ltrim ($ path , '/ ' );
113128 }
114129
@@ -148,86 +163,64 @@ public function getAuthSecret() {
148163
149164 public function get ($ uri , array $ params = array ()) {
150165 $ path = $ this ->parseApiPath ($ uri );
151- $ request = $ this ->getHttpClient ()->get ($ path );
152- foreach ($ params as $ key => $ value ) {
153- $ request ->getQuery ()->set ($ key , $ value );
154- }
166+ $ response = $ this ->getHttpClient ()->get ($ path , [
167+ 'query ' => $ params
168+ ]);
155169
156- $ response = $ request ->send ();
157- return $ response ->json ();
170+ return json_decode ($ response ->getBody (), true );
158171 }
159172
160173 public function put ($ uri , $ data = array ()) {
161174 $ path = $ this ->parseApiPath ($ uri );
162175 $ json = empty ($ data ) ? null : json_encode ($ data );
163176
164- $ request = $ this ->getHttpClient ()->put ($ path , null , $ json );
165- $ response = $ request ->send ();
166- return $ response ->json ();
177+ $ response = $ this ->getHttpClient ()->put ($ path , [
178+ 'body ' => $ json
179+ ]);
180+
181+ return json_decode ($ response ->getBody (), true );
167182 }
168183
169184 public function post ($ uri , $ data = array ()) {
170185 $ path = $ this ->parseApiPath ($ uri );
171186 $ json = empty ($ data ) ? null : json_encode ($ data );
172187
173- $ request = $ this ->getHttpClient ()->post ($ path , null , $ json );
174- $ response = $ request ->send ();
188+ $ response = $ this ->getHttpClient ()->post ($ path , [
189+ 'body ' => $ json
190+ ]);
175191
176- return $ response ->json ( );
192+ return json_decode ( $ response ->getBody (), true );
177193 }
178194
179195 public function delete ($ uri , $ data = array ()) {
180196 $ json = empty ($ data ) ? null : json_encode ($ data );
181197
182198 $ path = $ this ->parseApiPath ($ uri );
183- $ request = $ this ->getHttpClient ()->delete ($ path , null , $ json );
199+ $ response = $ this ->getHttpClient ()->delete ($ path , [
200+ 'body ' => $ json
201+ ]);
184202
185- $ response = $ request ->send ();
186- return $ response ->json ();
203+ return json_decode ($ response ->getBody (), true );
187204 }
188205
189206 public function postFile ($ uri , $ file , $ contentType = null ) {
190207 $ path = $ this ->parseApiPath ($ uri );
191- $ request = $ this ->getHttpClient ()->post ($ path );
192-
193- $ request ->addPostFile ('upload ' , $ file , $ contentType );
194-
195- $ response = $ request ->send ();
196- return $ response ->json ();
197- }
198-
199- /**
200- *
201- * @param \Guzzle\Http\Message\Request $request
202- * @throws Exception
203- */
204- public function bindAuth (HttpRequest $ request ) {
205- $ token = $ this ->getAuthToken ();
206- $ secret = $ this ->getAuthSecret ();
207-
208- if ($ token && $ secret ) {
209- $ signature = $ this ->getSignature ()->fromRequest ($ request , $ secret );
210- $ request ->setHeader ('Authorization ' , 'Basic ' . base64_encode ($ token . ': ' . $ signature ));
211- } elseif ($ token xor $ secret ) {
212- throw new \RuntimeException ('Both token and secret must be set ' );
213- }
208+ $ body = Psr7 \Utils::streamFor (fopen ($ file , 'r ' ));
209+ $ response = $ this ->getHttpClient ()->post ($ path , [
210+ 'body ' => $ body ,
211+ 'headers ' => [
212+ 'Content-Type ' => $ contentType ?: 'application/octet-stream '
213+ ]
214+ ]);
215+ return json_decode ($ response ->getBody (), true );
214216 }
215217
216218 /**
217219 *
218- * @param \Guzzle\Http\Message\Request $request
219- * @return \ChartBlocks\Client
220+ * @return Signature
220221 */
221- public function bindAccept (HttpRequest $ request ) {
222- $ request ->setHeader ('Accept ' , 'application/json ' );
223- return $ this ;
224- }
225-
226- /**
227- *
228- * @return \ChartBlocks\Signature
229- */
230- public function getSignature () {
222+ public function getSignature (): Signature
223+ {
231224 if ($ this ->signature === null ) {
232225 $ this ->signature = new Signature ();
233226 }
@@ -236,10 +229,11 @@ public function getSignature() {
236229 }
237230
238231 /**
239- *
240- * @return \Guzzle\Http\Client
232+ *
233+ * @return HttpClient
241234 */
242- public function getHttpClient () {
235+ public function getHttpClient (): HttpClient
236+ {
243237 if ($ this ->httpClient === null ) {
244238 $ this ->httpClient = $ this ->createHttpClient ();
245239 }
@@ -250,27 +244,54 @@ public function getHttpClient() {
250244 /**
251245 *
252246 * @param array $config
253- * @return \ChartBlocks\ Client
247+ * @return Client
254248 */
255- protected function setConfig (array $ config ) {
249+ protected function setConfig (array $ config ): Client
250+ {
256251 $ this ->config = $ config ;
257252 return $ this ;
258253 }
259254
260255 /**
261- *
262- * @return \Guzzle\Http\Client
256+ *
257+ * @return HttpClient
263258 */
264- protected function createHttpClient () {
265- $ client = new HttpClient ($ this ->getApiUrl (), array ());
266-
267- $ that = $ this ;
268- $ client ->getEventDispatcher ()->addListener ('request.before_send ' , function ($ event ) use ($ that ) {
269- $ that ->bindAccept ($ event ['request ' ]);
270- $ that ->bindAuth ($ event ['request ' ]);
271- });
259+ protected function createHttpClient (): HttpClient
260+ {
261+ $ stack = new HandlerStack ();
262+ $ stack ->setHandler (choose_handler ());
263+ $ stack ->push ($ this ->handleHeaders ());
264+
265+ return new HttpClient ([
266+ 'base_uri ' => $ this ->getApiUrl (),
267+ 'handler ' => $ stack ,
268+ 'headers ' => [
269+ 'Accept ' => 'application/json '
270+ ]
271+ ]);
272+ }
272273
273- return $ client ;
274+ /**
275+ * @return Closure
276+ * @throws RuntimeException
277+ */
278+ public function handleHeaders (): Closure
279+ {
280+ return function (callable $ handler ) {
281+ return function (RequestInterface $ request , array $ options ) use ($ handler ) {
282+ $ token = $ this ->getAuthToken ();
283+ $ secret = $ this ->getAuthSecret ();
284+
285+ if ($ token && $ secret ) {
286+ $ signature = $ this ->getSignature ()->fromRequest ($ request , $ secret );
287+ $ request = $ request ->withHeader ('Authorization ' , 'Basic ' . base64_encode ($ token . ': ' . $ signature ));
288+ } elseif ($ token xor $ secret ) {
289+ throw new RuntimeException ('Both token and secret must be set ' );
290+ }
291+
292+ return $ handler ($ request , $ options );
293+ };
294+ };
274295 }
275296
276297}
0 commit comments