Skip to content

Commit f9b7f10

Browse files
author
Grzegorz Rajchman
authored
PLAT-3484 update guzzle (#19)
1 parent 574dbfc commit f9b7f10

File tree

18 files changed

+967
-1536
lines changed

18 files changed

+967
-1536
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ changes.
1010

1111
Before releasing for general usage there will be major changes to the API. The library will move
1212
away from the use of internal Talis project names like Persona, Critic, Babel and Echo etc.
13-
Instead it will use more externally relavant names like ```ListReviews``` and ```Files```.
13+
Instead it will use more externally relevant names like ```ListReviews``` and ```Files```.
1414
The API will also move to a more domain driven design rather than the service driven design
1515
of the individual libraries
1616

@@ -44,7 +44,7 @@ Manually run persona locally:
4444

4545
# Create an OAuth Client and Secret
4646

47-
To build the talis-php docker container, you need to specify an oauth client and secret to use. This client should have `su` scope. It's not possibe to create a client with `su` scope via the API.
47+
To build the talis-php docker container, you need to specify an oauth client and secret to use. This client should have `su` scope. It's not possible to create a client with `su` scope via the API.
4848

4949
First - create a client:
5050

@@ -103,7 +103,7 @@ service redis-server start
103103
source /etc/profile.d/*
104104
```
105105

106-
You can the bun ant commands individually or run individual tests:
106+
You can then run ant commands individually or run individual tests:
107107

108108
```bash
109109
/vendor/bin/phpunit --filter testCreateUserThenPatchOAuthClientAddScope test/integration/

composer.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "talis/talis-php",
3-
"description": "This is a php client library for talis api's",
4-
"version": "0.3.1",
3+
"description": "This is a php client library for talis APIs",
4+
"version": "0.5.0",
55
"keywords": [
66
"persona",
77
"echo",
@@ -15,13 +15,13 @@
1515
"type": "library",
1616
"license": "MIT",
1717
"require": {
18-
"php": ">=5.3.0",
18+
"php": ">=5.5.9",
1919
"monolog/monolog": ">=1.13.1",
2020
"psr/log": ">=1.0.0",
2121
"firebase/php-jwt": "^3.0",
2222
"doctrine/common": "^2.5",
23-
"predis/predis" : "v0.8.5",
24-
"guzzle/guzzle": "^3.8"
23+
"predis/predis" : ">=0.8.5",
24+
"guzzlehttp/guzzle": "^6.0"
2525
},
2626
"require-dev": {
2727
"phpunit/phpunit": "4.8.36",

src/Talis/Babel/Client.php

+41-48
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Talis\Babel;
44

5-
use Guzzle\Http\Message\Header;
6-
use Guzzle\Http\Message\Header\HeaderCollection;
75
use Monolog\Handler\StreamHandler;
86
use Monolog\Logger;
97

@@ -27,7 +25,7 @@ class Client
2725
private $babelPort;
2826

2927
/**
30-
* @var \Guzzle\Http\Client
28+
* @var \GuzzleHttp\Client
3129
*/
3230
private $httpClient = null;
3331

@@ -124,8 +122,8 @@ public function getTargetFeedCount($target, $token, $deltaToken = 0)
124122
$queryParams = http_build_query(['delta_token' => $deltaToken]);
125123
$url = "/feeds/targets/$hash/activity/annotations?$queryParams";
126124

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');
129127

130128
if (count($newItemsHeader) !== 1) {
131129
throw new \Talis\Babel\ClientException(
@@ -258,7 +256,7 @@ public function createAnnotation($token, array $arrData, $bCreateSynchronously =
258256
if ($bCreateSynchronously) {
259257
// Specific header that Babel server accepts to not return until the
260258
// feed has also been created for the annotation.
261-
$requestOptions = ['headers' => ['X-Ingest-Synchronously' => 'true']];
259+
$requestOptions = [\GuzzleHttp\RequestOptions::HEADERS => ['X-Ingest-Synchronously' => 'true']];
262260
}
263261

264262
$url = '/annotations';
@@ -283,13 +281,12 @@ protected function performBabelGet($url, $token)
283281
];
284282

285283
$this->getLogger()->debug("Babel GET: $url", $headers);
286-
$httpClient = $this->getHttpClient();
287284

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();
293290
$arrResponse = json_decode($responseBody, true);
294291

295292
if ($arrResponse == null) {
@@ -299,14 +296,9 @@ protected function performBabelGet($url, $token)
299296
}
300297

301298
return $arrResponse;
299+
} catch (\GuzzleHttp\Exception\RequestException $exception) {
300+
$this->handleBabelError($url, $exception);
302301
}
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);
310302
}
311303

312304
/**
@@ -315,7 +307,7 @@ protected function performBabelGet($url, $token)
315307
*
316308
* @param string $url babel url
317309
* @param string $token persona oauth token
318-
* @return HeaderCollection
310+
* @return \Psr\Http\Message\ResponseInterface
319311
* @throws InvalidPersonaTokenException Invalid Persona oauth token
320312
* @throws NotFoundException Babel feed not found
321313
* @throws \Talis\Babel\ClientException Could not communicate with Babel
@@ -329,20 +321,15 @@ protected function performBabelHead($url, $token)
329321

330322
$this->getLogger()->debug('Babel HEAD: ' . $url, $headers);
331323

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+
]);
335328

336-
if ($response->isSuccessful()) {
337-
return $response->getHeaders();
329+
return $response;
330+
} catch (\GuzzleHttp\Exception\RequestException $exception) {
331+
$this->handleBabelError($url, $exception);
338332
}
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);
346333
}
347334

348335
/**
@@ -367,18 +354,18 @@ protected function performBabelPost($url, $token, array $arrData, array $request
367354
'Authorization' => "Bearer $token",
368355
];
369356

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]);
372359
}
373360

374361
$this->getLogger()->debug("Babel POST: $url", $arrData);
375362

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();
382369
$arrResponse = json_decode($responseBody, true);
383370

384371
if ($arrResponse == null) {
@@ -388,9 +375,9 @@ protected function performBabelPost($url, $token, array $arrData, array $request
388375
}
389376

390377
return $arrResponse;
378+
} catch (\GuzzleHttp\Exception\RequestException $exception) {
379+
$this->handleBabelError($url, $exception);
391380
}
392-
393-
$this->handleBabelError($url, $response);
394381
}
395382

396383
/**
@@ -428,7 +415,7 @@ protected function getBabelPort()
428415
/**
429416
* Get an instance of the Guzzle HTTP client.
430417
*
431-
* @return \Guzzle\Http\Client
418+
* @return \GuzzleHttp\Client
432419
*/
433420
protected function getHttpClient()
434421
{
@@ -440,7 +427,7 @@ protected function getHttpClient()
440427
$baseUrl .= ":$port";
441428
}
442429

443-
$this->httpClient = new \Guzzle\Http\Client($baseUrl);
430+
$this->httpClient = new \GuzzleHttp\Client(['base_uri' => $baseUrl]);
444431
}
445432

446433
return $this->httpClient;
@@ -464,10 +451,16 @@ protected function checkKeysExist(array $keys, array $array)
464451
/**
465452
* Handle a babel error response
466453
* @param string $url babel url which was called
467-
* @param mixed $response http response
454+
* @param \GuzzleHttp\Exception\RequestException $exception request exception
468455
*/
469-
protected function handleBabelError($url, $response)
456+
protected function handleBabelError($url, \GuzzleHttp\Exception\RequestException $exception)
470457
{
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();
471464
$statusCode = $response->getStatusCode();
472465

473466
switch ($statusCode) {
@@ -479,7 +472,7 @@ protected function handleBabelError($url, $response)
479472
throw new NotFoundException("Nothing found for request: $url");
480473
default:
481474
$errorMessage = 'Unknown error';
482-
$responseBody = $response->getBody(true);
475+
$responseBody = (string) $response->getBody();
483476

484477
if ($responseBody) {
485478
$arrResponse = json_decode($responseBody, true);
@@ -493,7 +486,7 @@ protected function handleBabelError($url, $response)
493486
"Babel failed for request: $url",
494487
[
495488
'statusCode' => $statusCode,
496-
'message' => $response->getMessage(),
489+
'message' => $exception->getMessage(),
497490
'body' => $responseBody,
498491
]
499492
);

src/Talis/Critic/Client.php

+15-15
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Client
2323
protected $criticBaseUrl;
2424

2525
/**
26-
* @var \Guzzle\Http\Client
26+
* @var \GuzzleHttp\Client
2727
*/
2828
protected $httpClient;
2929

@@ -72,12 +72,12 @@ public function setTokenClient(\Talis\Persona\Client\Tokens $personaClient)
7272

7373
/**
7474
* For mocking
75-
* @return \Guzzle\Http\Client
75+
* @return \GuzzleHttp\Client
7676
*/
7777
protected function getHTTPClient()
7878
{
7979
if (!$this->httpClient) {
80-
$this->httpClient = new \Guzzle\Http\Client();
80+
$this->httpClient = new \GuzzleHttp\Client();
8181
}
8282

8383
return $this->httpClient;
@@ -94,40 +94,40 @@ protected function getHTTPClient()
9494
* for pre-existing access_token (and setting a new cookie
9595
* with the resultant token)
9696
* use_cache: (boolean) use cached called (defaults to true) </pre>
97-
* @throws \Exception|\Guzzle\Http\Exception\ClientErrorResponseException Http communication error
97+
* @throws \Exception|\GuzzleHttp\Exception\RequestException Http communication error
9898
* @throws Exceptions\UnauthorisedAccessException Authorisation error
9999
*/
100100
public function createReview(array $postFields, $clientId, $clientSecret, array $headerParams = [])
101101
{
102-
103102
try {
104-
$client = $this->getHTTPClient();
105103
$headers = $this->getHeaders($clientId, $clientSecret, $headerParams);
106-
107-
$request = $client->post($this->criticBaseUrl, $headers, $postFields);
108-
$response = $request->send();
104+
$response = $this->getHttpClient()->post($this->criticBaseUrl, [
105+
\GuzzleHttp\RequestOptions::HEADERS => $headers,
106+
\GuzzleHttp\RequestOptions::FORM_PARAMS => $postFields,
107+
]);
109108

110109
if ($response->getStatusCode() == 201) {
111-
$body = json_decode($response->getBody(true));
110+
$responseBody = (string) $response->getBody();
111+
$body = json_decode($responseBody);
112112
return $body->id;
113113
}
114114

115115
throw new \Talis\Critic\Exceptions\ReviewException();
116-
} catch (\Guzzle\Http\Exception\ClientErrorResponseException $e) {
117-
$response = $e->getResponse();
118-
$error = $this->processErrorResponseBody($response->getBody(true));
116+
} catch (\GuzzleHttp\Exception\RequestException $exception) {
117+
$response = $exception->getResponse();
118+
$error = $this->processErrorResponseBody((string) $response->getBody());
119119

120120
switch ($response->getStatusCode()) {
121121
case 403:
122122
case 401:
123123
throw new \Talis\Critic\Exceptions\UnauthorisedAccessException(
124124
$error['message'],
125125
$error['error_code'],
126-
$e
126+
$exception
127127
);
128128
break;
129129
default:
130-
throw $e;
130+
throw $exception;
131131
}
132132
}
133133
}

src/Talis/EchoClient/Base.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ protected function getStringSizeInBytes($input)
7777
/**
7878
* To allow mocking of the Guzzle client for testing.
7979
*
80-
* @return Client
80+
* @return \GuzzleHttp\Client
8181
*/
8282
protected function getHttpClient()
8383
{
84-
return new \Guzzle\Http\Client();
84+
return new \GuzzleHttp\Client();
8585
}
8686

8787
/**

0 commit comments

Comments
 (0)