Skip to content

Commit c0d14d5

Browse files
committed
Migrate to Guzzle
1 parent bf82bc9 commit c0d14d5

File tree

6 files changed

+60
-161
lines changed

6 files changed

+60
-161
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"ext-curl": "*",
1616
"ext-json": "*",
1717
"ext-mbstring": "*",
18-
"egeloen/http-adapter": "~0.8.0",
19-
"symfony/event-dispatcher": "~2.0"
18+
"symfony/event-dispatcher": "~2.0",
19+
"guzzlehttp/guzzle": "^6.1"
2020
},
2121
"require-dev": {
2222
"codeclimate/php-test-reporter": "dev-master",

src/Prismic/Api.php

Lines changed: 33 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@
1010

1111
namespace Prismic;
1212

13-
use Ivory\HttpAdapter\ConfigurationInterface;
14-
use Ivory\HttpAdapter\Configuration;
15-
use Ivory\HttpAdapter\CurlHttpAdapter;
16-
use Ivory\HttpAdapter\EventDispatcherHttpAdapter;
17-
use Ivory\HttpAdapter\Event\Subscriber\StatusCodeSubscriber;
18-
use Ivory\HttpAdapter\HttpAdapterInterface;
19-
use Ivory\HttpAdapter\MultiHttpAdapterException;
13+
use GuzzleHttp\Client;
14+
use GuzzleHttp\Promise;
2015
use Symfony\Component\EventDispatcher\EventDispatcher;
2116
use \Prismic\Cache\CacheInterface;
2217
use \Prismic\Cache\ApcCache;
@@ -61,23 +56,23 @@ class Api
6156
*/
6257
private $cache;
6358
/**
64-
* @var HttpAdapterInterface
59+
* @var Client
6560
*/
66-
private $httpAdapter;
61+
private $httpClient;
6762

6863
/**
6964
* Private constructor, not be used outside of this class.
7065
*
7166
* @param string $data
7267
* @param string|null $accessToken
73-
* @param HttpAdapterInterface|null $httpAdapter
68+
* @param HttpClient|null $httpAdapter
7469
* @param CacheInterface|null $cache
7570
*/
76-
private function __construct($data, $accessToken = null, HttpAdapterInterface $httpAdapter = null, CacheInterface $cache = null)
71+
private function __construct($data, $accessToken = null, Client $httpClient = null, CacheInterface $cache = null)
7772
{
7873
$this->data = $data;
7974
$this->accessToken = $accessToken;
80-
$this->httpAdapter = is_null($httpAdapter) ? self::defaultHttpAdapter() : $httpAdapter;
75+
$this->httpClient = is_null($httpClient) ? new Client() : $httpClient;
8176
$this->cache = is_null($cache) ? self::defaultCache() : $cache;
8277
}
8378

@@ -229,14 +224,12 @@ public function getExperiments()
229224
*/
230225
public function previewSession($token, $linkResolver, $defaultUrl)
231226
{
232-
$response = $this->getHttpAdapter()->get($token);
227+
$response = $this->getHttpClient()->get($token);
233228
$response = json_decode($response->getBody(true));
234229
if (isset($response->mainDocument)) {
235-
$documents = $this->forms()->everything
236-
->query(Predicates::at("document.id", $response->mainDocument))
237-
->ref($token)
238-
->submit()
239-
->getResults();
230+
$documents = $this
231+
->query(Predicates::at("document.id", $response->mainDocument), ['ref' => $token])
232+
->getResults();
240233
if (count($documents) > 0) {
241234
if ($url = $linkResolver->resolveDocument($documents[0])) {
242235
return $url;
@@ -287,13 +280,13 @@ public function getCache()
287280
}
288281

289282
/**
290-
* Accessing the underlying HTTP adapter object responsible for the CURL requests
283+
* Accessing the underlying Guzzle HTTP client
291284
*
292-
* @return HttpAdapterInterface the HTTP adapter object itself
285+
* @return HttpAdapterInterface
293286
*/
294-
public function getHttpAdapter()
287+
public function getHttpClient()
295288
{
296-
return $this->httpAdapter;
289+
return $this->httpClient;
297290
}
298291

299292
/**
@@ -303,28 +296,28 @@ public function getHttpAdapter()
303296
*
304297
* @api
305298
*
306-
* @param string $action the URL of your repository API's endpoint
307-
* @param string $accessToken a permanent access token to use to access your content, for instance if your repository API is set to private
308-
* @param HttpAdapterInterface $httpAdapter by default, the HTTP adapter uses CURL with a certain configuration, but you can override it here
309-
* @param CacheInterface $cache Cache implementation
310-
* @param int $apiCacheTTL max time to keep the API object in cache (in seconds)
299+
* @param string $action the URL of your repository API's endpoint
300+
* @param string $accessToken a permanent access token to use to access your content, for instance if your repository API is set to private
301+
* @param Client $httpClient Custom Guzzle http client
302+
* @param CacheInterface $cache Cache implementation
303+
* @param int $apiCacheTTL max time to keep the API object in cache (in seconds)
311304
*
312305
* @throws \RuntimeException
313306
*
314307
* @return Api the Api object, usable to perform queries
315308
*/
316-
public static function get($action, $accessToken = null, HttpAdapterInterface $httpAdapter = null, CacheInterface $cache = null, $apiCacheTTL = 5)
309+
public static function get($action, $accessToken = null, $httpClient = null, CacheInterface $cache = null, $apiCacheTTL = 5)
317310
{
318311
$cache = is_null($cache) ? self::defaultCache() : $cache;
319312
$cacheKey = $action . (is_null($accessToken) ? "" : ("#" . $accessToken));
320313
$apiData = $cache->get($cacheKey);
321-
$api = $apiData ? new Api(unserialize($apiData), $accessToken, $httpAdapter, $cache) : null;
314+
$api = $apiData ? new Api(unserialize($apiData), $accessToken, $httpClient, $cache) : null;
322315
if ($api) {
323316
return $api;
324317
} else {
325318
$url = $action . ($accessToken ? '?access_token=' . $accessToken : '');
326-
$httpAdapter = is_null($httpAdapter) ? self::defaultHttpAdapter() : $httpAdapter;
327-
$response = $httpAdapter->get($url);
319+
$httpClient = is_null($httpClient) ? new Client() : $httpClient;
320+
$response = $httpClient->get($url);
328321
$response = json_decode($response->getBody(true));
329322
$experiments = isset($response->experiments)
330323
? Experiments::parse($response->experiments)
@@ -350,7 +343,7 @@ function ($ref) {
350343
$response->oauth_token
351344
);
352345

353-
$api = new Api($apiData, $accessToken, $httpAdapter, $cache);
346+
$api = new Api($apiData, $accessToken, $httpClient, $cache);
354347
$cache->set($cacheKey, serialize($apiData), $apiCacheTTL);
355348

356349
return $api;
@@ -373,8 +366,9 @@ public function submit()
373366
$responses = array();
374367

375368
// Get what we can from the cache
376-
$all_urls = array();
377-
$urls = array();
369+
$all_urls = [];
370+
$promises = [];
371+
$urls = [];
378372
foreach ($forms as $i => $form) {
379373
$url = $form->url();
380374
array_push($all_urls, $url);
@@ -384,20 +378,16 @@ public function submit()
384378
} else {
385379
$responses[$i] = null;
386380
array_push($urls, $url);
381+
$promises[$url] = $this->getHttpClient()->getAsync($url);
387382
}
388383
}
389384

390385
// Query the server for the rest
391-
if (count($urls) > 0) {
392-
try {
393-
$raw_responses = $this->getHttpAdapter()->sendRequests($urls);
394-
} catch (MultiHttpAdapterException $e) {
395-
$raw_responses = $e->getResponses();
396-
$exceptions = $e->getExceptions();
397-
}
386+
if (count($promises) > 0) {
387+
$raw_responses = Promise\unwrap($promises);
398388

399-
foreach ($raw_responses as $response) {
400-
$url = $response->getParameter('request')->getUri()->__toString();
389+
foreach ($urls as $url) {
390+
$response = $raw_responses[$url];
401391
$cacheControl = $response->getHeader('Cache-Control')[0];
402392
$cacheDuration = null;
403393
if (preg_match('/^max-age\s*=\s*(\d+)$/', $cacheControl, $groups) == 1) {
@@ -510,44 +500,4 @@ public static function defaultCache()
510500
return new NoCache();
511501
}
512502

513-
/**
514-
* Get the default HTTP adapter configuration object
515-
*
516-
* This can be used for example to modify but not completely replace the
517-
* default configuration (e.g. to prefix the user agent string), or to use
518-
* the default configuration for a non-default HTTP adapter.
519-
*
520-
* @return \Ivory\HttpAdapter\ConfigurationInterface Configuration object
521-
*/
522-
public static function defaultHttpAdapterConfiguration()
523-
{
524-
$configuration = new Configuration();
525-
$configuration->setUserAgent('Prismic-php-kit/' . self::VERSION . ' PHP/' . phpversion());
526-
527-
return $configuration;
528-
}
529-
530-
/**
531-
* Get the default HTTP adapter used in the kit; this is entirely
532-
* overridable by passing an instance of
533-
* Ivory\HttpAdapter\HttpAdapterInterface to Api.get
534-
*
535-
* @param ConfigurationInterface|null $configuration Configuration object; use default if null
536-
* @return HttpAdapterInterface HTTP adapter
537-
*/
538-
public static function defaultHttpAdapter(ConfigurationInterface $configuration = null)
539-
{
540-
if ($configuration === null) {
541-
$configuration = self::defaultHttpAdapterConfiguration();
542-
}
543-
$dispatcher = new EventDispatcher();
544-
$adapter = new EventDispatcherHttpAdapter(new CurlHttpAdapter($configuration), $dispatcher);
545-
546-
// We need to add the subscriber to have errors on 4.x.x and 5.x.x.
547-
$statusCodeSubscriber = new StatusCodeSubscriber();
548-
$dispatcher->addSubscriber($statusCodeSubscriber);
549-
550-
return $adapter;
551-
}
552-
553503
}

src/Prismic/SearchForm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ private function submit_raw()
317317
if ($response) {
318318
return $response;
319319
} else {
320-
$response = $this->api->getHttpAdapter()->get($url);
320+
$response = $this->api->getHttpClient()->get($url);
321321
$cacheControl = $response->getHeader('Cache-Control')[0];
322322
$cacheDuration = null;
323323
if (preg_match('/^max-age\s*=\s*(\d+)$/', $cacheControl, $groups) == 1) {

tests/Prismic/ApiTest.php

Lines changed: 0 additions & 64 deletions
This file was deleted.

tests/Prismic/DocTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Prismic\Test;
44

55
use DateTime;
6-
use Ivory\HttpAdapter\HttpAdapterException;
6+
use GuzzleHttp\Exception\ClientException;
77
use Prismic\Api;
88
use Prismic\Cache\ApcCache;
99
use Prismic\Document;
@@ -33,7 +33,7 @@ public function testApiPrivate()
3333
// This will fail because the token is invalid, but this is how to access a private API
3434
// endgist
3535
$this->fail('The API->get call should have thrown');
36-
} catch (HttpAdapterException $e) {
36+
} catch (ClientException $e) {
3737
$this->assertEquals($e->getResponse()->getStatusCode(), 401);
3838
}
3939
}

tests/Prismic/LinkResolverTest.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
namespace Prismic\Test;
44

5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Handler\MockHandler;
7+
use GuzzleHttp\HandlerStack;
8+
use GuzzleHttp\Psr7\Response;
9+
510
use Prismic\API;
611
use Prismic\Cache\ApcCache;
712
use Prismic\Document;
@@ -24,15 +29,23 @@ protected function setUp()
2429
$href = "http://myrepo.prismic.io/Ue0EDd_mqb8Dhk3j";
2530
$this->document = new Document($this->id, null, $type, $href, $tags, $slugs, array());
2631
$this->link = new DocumentLink($this->id, null, $type, $tags, $slugs[0], array(), $isBroken);
27-
$response = $this->getMockBuilder('Ivory\HttpAdapter\Message\Response')
28-
->disableOriginalConstructor()
29-
->getMock();
30-
$response->expects($this->once())->method('getBody')->will($this->returnValue(file_get_contents(__DIR__.'/../fixtures/data.json')));
31-
32-
$httpAdapter = $this->getMock('Ivory\HttpAdapter\HttpAdapterInterface');
33-
$httpAdapter->expects($this->any())->method('get')->will($this->returnValue($response));
34-
35-
$this->api = Api::get('don\'t care about this value', null, $httpAdapter, $cache);
32+
$response = file_get_contents(__DIR__.'/../fixtures/data.json');
33+
34+
$mock = new MockHandler([
35+
new Response(200, [], $response),
36+
new Response(200, [], $response),
37+
new Response(200, [], $response),
38+
new Response(200, [], $response),
39+
new Response(200, [], $response),
40+
new Response(200, [], $response),
41+
new Response(200, [], $response),
42+
new Response(200, [], $response),
43+
new Response(200, [], $response)
44+
]);
45+
$handler = HandlerStack::create($mock);
46+
$client = new Client(['handler' => $handler]);
47+
48+
$this->api = Api::get('dont care about this value', null, $client, $cache);
3649
}
3750

3851
public function testResolveDocumentLink()

0 commit comments

Comments
 (0)