2
2
3
3
namespace Talis \Babel ;
4
4
5
- use Guzzle \Http \Message \Header ;
6
- use Guzzle \Http \Message \Header \HeaderCollection ;
7
5
use Monolog \Handler \StreamHandler ;
8
6
use Monolog \Logger ;
9
7
@@ -27,7 +25,7 @@ class Client
27
25
private $ babelPort ;
28
26
29
27
/**
30
- * @var \Guzzle\Http \Client
28
+ * @var \GuzzleHttp \Client
31
29
*/
32
30
private $ httpClient = null ;
33
31
@@ -124,8 +122,8 @@ public function getTargetFeedCount($target, $token, $deltaToken = 0)
124
122
$ queryParams = http_build_query (['delta_token ' => $ deltaToken ]);
125
123
$ url = "/feeds/targets/ $ hash/activity/annotations? $ queryParams " ;
126
124
127
- $ headers = $ this ->performBabelHead ($ url , $ token );
128
- $ newItemsHeader = $ headers -> get ('X-Feed-New-Items ' )-> toArray ( );
125
+ $ response = $ this ->performBabelHead ($ url , $ token );
126
+ $ newItemsHeader = $ response -> getHeader ('X-Feed-New-Items ' );
129
127
130
128
if (count ($ newItemsHeader ) !== 1 ) {
131
129
throw new \Talis \Babel \ClientException (
@@ -258,7 +256,7 @@ public function createAnnotation($token, array $arrData, $bCreateSynchronously =
258
256
if ($ bCreateSynchronously ) {
259
257
// Specific header that Babel server accepts to not return until the
260
258
// feed has also been created for the annotation.
261
- $ requestOptions = [' headers ' => ['X-Ingest-Synchronously ' => 'true ' ]];
259
+ $ requestOptions = [\ GuzzleHttp \RequestOptions:: HEADERS => ['X-Ingest-Synchronously ' => 'true ' ]];
262
260
}
263
261
264
262
$ url = '/annotations ' ;
@@ -283,13 +281,12 @@ protected function performBabelGet($url, $token)
283
281
];
284
282
285
283
$ this ->getLogger ()->debug ("Babel GET: $ url " , $ headers );
286
- $ httpClient = $ this ->getHttpClient ();
287
284
288
- $ request = $ httpClient -> get ( $ url , $ headers , [ ' exceptions ' => false ]);
289
- $ response = $ request -> send ();
290
-
291
- if ( $ response -> isSuccessful ()) {
292
- $ responseBody = $ response ->getBody (true );
285
+ try {
286
+ $ response = $ this -> getHttpClient ()-> get ( $ url , [
287
+ \ GuzzleHttp \RequestOptions:: HEADERS => $ headers ,
288
+ ]);
289
+ $ responseBody = ( string ) $ response ->getBody ();
293
290
$ arrResponse = json_decode ($ responseBody , true );
294
291
295
292
if ($ arrResponse == null ) {
@@ -299,14 +296,9 @@ protected function performBabelGet($url, $token)
299
296
}
300
297
301
298
return $ arrResponse ;
299
+ } catch (\GuzzleHttp \Exception \RequestException $ exception ) {
300
+ $ this ->handleBabelError ($ url , $ exception );
302
301
}
303
-
304
- /*
305
- * For error scenarios we want to distinguish Persona problems and
306
- * instances where no data is found. Anything else raises a generic
307
- * \Talis\Babel\ClientException.
308
- */
309
- $ this ->handleBabelError ($ url , $ response );
310
302
}
311
303
312
304
/**
@@ -315,7 +307,7 @@ protected function performBabelGet($url, $token)
315
307
*
316
308
* @param string $url babel url
317
309
* @param string $token persona oauth token
318
- * @return HeaderCollection
310
+ * @return \Psr\Http\Message\ResponseInterface
319
311
* @throws InvalidPersonaTokenException Invalid Persona oauth token
320
312
* @throws NotFoundException Babel feed not found
321
313
* @throws \Talis\Babel\ClientException Could not communicate with Babel
@@ -329,20 +321,15 @@ protected function performBabelHead($url, $token)
329
321
330
322
$ this ->getLogger ()->debug ('Babel HEAD: ' . $ url , $ headers );
331
323
332
- $ httpClient = $ this ->getHttpClient ();
333
- $ request = $ httpClient ->head ($ url , $ headers , ['exceptions ' => false ]);
334
- $ response = $ request ->send ();
324
+ try {
325
+ $ response = $ this ->getHttpClient ()->head ($ url , [
326
+ \GuzzleHttp \RequestOptions::HEADERS => $ headers ,
327
+ ]);
335
328
336
- if ($ response ->isSuccessful ()) {
337
- return $ response ->getHeaders ();
329
+ return $ response ;
330
+ } catch (\GuzzleHttp \Exception \RequestException $ exception ) {
331
+ $ this ->handleBabelError ($ url , $ exception );
338
332
}
339
-
340
- /*
341
- * For error scenarios we want to distinguish Persona problems and
342
- * instances where no data is found. Anything else raises a generic
343
- * \Talis\Babel\ClientException.
344
- */
345
- $ this ->handleBabelError ($ url , $ response );
346
333
}
347
334
348
335
/**
@@ -367,18 +354,18 @@ protected function performBabelPost($url, $token, array $arrData, array $request
367
354
'Authorization ' => "Bearer $ token " ,
368
355
];
369
356
370
- if (isset ($ requestOptions [' headers ' ])) {
371
- $ headers = array_merge ($ headers , $ requestOptions [' headers ' ]);
357
+ if (isset ($ requestOptions [\ GuzzleHttp \RequestOptions:: HEADERS ])) {
358
+ $ headers = array_merge ($ headers , $ requestOptions [\ GuzzleHttp \RequestOptions:: HEADERS ]);
372
359
}
373
360
374
361
$ this ->getLogger ()->debug ("Babel POST: $ url " , $ arrData );
375
362
376
- $ httpClient = $ this -> getHttpClient ();
377
- $ request = $ httpClient -> post ($ url , $ headers , $ arrData , [ ' exceptions ' => false ]);
378
- $ response = $ request -> send ();
379
-
380
- if ( $ response -> isSuccessful ()) {
381
- $ responseBody = $ response ->getBody (true );
363
+ try {
364
+ $ response = $ this -> getHttpClient ()-> post ($ url , [
365
+ \ GuzzleHttp \RequestOptions:: HEADERS => $ headers ,
366
+ \ GuzzleHttp \RequestOptions:: FORM_PARAMS => $ arrData ,
367
+ ]);
368
+ $ responseBody = ( string ) $ response ->getBody ();
382
369
$ arrResponse = json_decode ($ responseBody , true );
383
370
384
371
if ($ arrResponse == null ) {
@@ -388,9 +375,9 @@ protected function performBabelPost($url, $token, array $arrData, array $request
388
375
}
389
376
390
377
return $ arrResponse ;
378
+ } catch (\GuzzleHttp \Exception \RequestException $ exception ) {
379
+ $ this ->handleBabelError ($ url , $ exception );
391
380
}
392
-
393
- $ this ->handleBabelError ($ url , $ response );
394
381
}
395
382
396
383
/**
@@ -428,7 +415,7 @@ protected function getBabelPort()
428
415
/**
429
416
* Get an instance of the Guzzle HTTP client.
430
417
*
431
- * @return \Guzzle\Http \Client
418
+ * @return \GuzzleHttp \Client
432
419
*/
433
420
protected function getHttpClient ()
434
421
{
@@ -440,7 +427,7 @@ protected function getHttpClient()
440
427
$ baseUrl .= ": $ port " ;
441
428
}
442
429
443
- $ this ->httpClient = new \Guzzle \ Http \ Client ($ baseUrl );
430
+ $ this ->httpClient = new \GuzzleHttp \ Client ([ ' base_uri ' => $ baseUrl] );
444
431
}
445
432
446
433
return $ this ->httpClient ;
@@ -464,10 +451,16 @@ protected function checkKeysExist(array $keys, array $array)
464
451
/**
465
452
* Handle a babel error response
466
453
* @param string $url babel url which was called
467
- * @param mixed $response http response
454
+ * @param \GuzzleHttp\Exception\RequestException $exception request exception
468
455
*/
469
- protected function handleBabelError ($ url , $ response )
456
+ protected function handleBabelError ($ url , \ GuzzleHttp \ Exception \ RequestException $ exception )
470
457
{
458
+ // Re-throw exception if it occurred before the response was produced
459
+ if (!$ exception ->hasResponse ()) {
460
+ throw $ exception ;
461
+ }
462
+
463
+ $ response = $ exception ->getResponse ();
471
464
$ statusCode = $ response ->getStatusCode ();
472
465
473
466
switch ($ statusCode ) {
@@ -479,7 +472,7 @@ protected function handleBabelError($url, $response)
479
472
throw new NotFoundException ("Nothing found for request: $ url " );
480
473
default :
481
474
$ errorMessage = 'Unknown error ' ;
482
- $ responseBody = $ response ->getBody (true );
475
+ $ responseBody = ( string ) $ response ->getBody ();
483
476
484
477
if ($ responseBody ) {
485
478
$ arrResponse = json_decode ($ responseBody , true );
@@ -493,7 +486,7 @@ protected function handleBabelError($url, $response)
493
486
"Babel failed for request: $ url " ,
494
487
[
495
488
'statusCode ' => $ statusCode ,
496
- 'message ' => $ response ->getMessage (),
489
+ 'message ' => $ exception ->getMessage (),
497
490
'body ' => $ responseBody ,
498
491
]
499
492
);
0 commit comments