diff --git a/composer.json b/composer.json index a4cd16a..ff0711f 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ }, "require": { "php": ">=5.4.0", - "chuyskywalker/rolling-curl": "~3.1" + "guzzlehttp/guzzle": "~5.2" }, "require-dev": { "phpunit/phpunit": "~4.5" diff --git a/composer.lock b/composer.lock index 9fde641..af9d668 100644 --- a/composer.lock +++ b/composer.lock @@ -4,105 +4,101 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "d5b71f8bcfd1b3afdbb75cb09669c46a", + "hash": "5b62a4b12ebf9025763ac709f4cc57eb", "packages": [ { - "name": "chuyskywalker/rolling-curl", - "version": "3.1.0", + "name": "guzzlehttp/guzzle", + "version": "5.2.0", "source": { "type": "git", - "url": "https://github.com/chuyskywalker/rolling-curl.git", - "reference": "fc1d28c221962f25e9615bd3991dd82732420c8c" + "url": "https://github.com/guzzle/guzzle.git", + "reference": "475b29ccd411f2fa8a408e64576418728c032cfa" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/chuyskywalker/rolling-curl/zipball/fc1d28c221962f25e9615bd3991dd82732420c8c", - "reference": "fc1d28c221962f25e9615bd3991dd82732420c8c", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/475b29ccd411f2fa8a408e64576418728c032cfa", + "reference": "475b29ccd411f2fa8a408e64576418728c032cfa", "shasum": "" }, "require": { + "guzzlehttp/ringphp": "~1.0", + "php": ">=5.4.0" + }, + "require-dev": { "ext-curl": "*", - "lib-curl": "*", - "php": ">=5.3.0" + "phpunit/phpunit": "~4.0", + "psr/log": "~1.0" }, "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.0-dev" + } + }, "autoload": { - "psr-0": { - "RollingCurl": "src/" + "psr-4": { + "GuzzleHttp\\": "src/" } }, "notification-url": "https://packagist.org/downloads/", "license": [ - "Apache-2.0" + "MIT" ], "authors": [ { - "name": "Jeff Minard", - "homepage": "http://jrm.cc/", - "role": "Developer" - }, - { - "name": "Josh Fraser", - "email": "joshfraz@gmail.com", - "homepage": "http://joshfraser.com/", - "role": "Developer" - }, - { - "name": "Alexander Makarov", - "email": "sam@rmcreative.ru", - "homepage": "http://rmcreative.ru/", - "role": "Developer" + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" } ], - "description": "Rolling-Curl: A non-blocking, non-dos multi-curl library for PHP", - "homepage": "https://github.com/chuyskywalker/rolling-curl", + "description": "Guzzle is a PHP HTTP client library and framework for building RESTful web service clients", + "homepage": "http://guzzlephp.org/", "keywords": [ - "asynchronous", + "client", "curl", + "framework", "http", - "multi", - "parallel", - "requests" + "http client", + "rest", + "web service" ], - "time": "2014-08-30 03:39:25" + "time": "2015-01-28 01:03:29" }, { - "name": "lusitanian/oauth", - "version": "v0.3.5", + "name": "guzzlehttp/ringphp", + "version": "1.0.6", "source": { "type": "git", - "url": "https://github.com/Lusitanian/PHPoAuthLib.git", - "reference": "ac5a1cd5a4519143728dce2213936eea302edf8a" + "url": "https://github.com/guzzle/RingPHP.git", + "reference": "f43ab34aad69ca0ba04172cf2c3cd5c12fc0e5a4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Lusitanian/PHPoAuthLib/zipball/ac5a1cd5a4519143728dce2213936eea302edf8a", - "reference": "ac5a1cd5a4519143728dce2213936eea302edf8a", + "url": "https://api.github.com/repos/guzzle/RingPHP/zipball/f43ab34aad69ca0ba04172cf2c3cd5c12fc0e5a4", + "reference": "f43ab34aad69ca0ba04172cf2c3cd5c12fc0e5a4", "shasum": "" }, "require": { - "php": ">=5.3.0" + "guzzlehttp/streams": "~3.0", + "php": ">=5.4.0", + "react/promise": "~2.0" }, "require-dev": { - "phpunit/phpunit": "3.7.*", - "predis/predis": "0.8.*@dev", - "symfony/http-foundation": "~2.1" + "ext-curl": "*", + "phpunit/phpunit": "~4.0" }, "suggest": { - "ext-openssl": "Allows for usage of secure connections with the stream-based HTTP client.", - "predis/predis": "Allows using the Redis storage backend.", - "symfony/http-foundation": "Allows using the Symfony Session storage backend." + "ext-curl": "Guzzle will use specific adapters if cURL is present" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "0.1-dev" + "dev-master": "1.0-dev" } }, "autoload": { - "psr-0": { - "OAuth": "src", - "OAuth\\Unit": "tests" + "psr-4": { + "GuzzleHttp\\Ring\\": "src/" } }, "notification-url": "https://packagist.org/downloads/", @@ -111,22 +107,107 @@ ], "authors": [ { - "name": "David Desberg", - "email": "david@daviddesberg.com" - }, + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Provides a simple API and specification that abstracts away the details of HTTP into a single PHP function.", + "time": "2015-02-26 20:43:09" + }, + { + "name": "guzzlehttp/streams", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/guzzle/streams.git", + "reference": "47aaa48e27dae43d39fc1cea0ccf0d84ac1a2ba5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/streams/zipball/47aaa48e27dae43d39fc1cea0ccf0d84ac1a2ba5", + "reference": "47aaa48e27dae43d39fc1cea0ccf0d84ac1a2ba5", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Stream\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ { - "name": "Pieter Hordijk", - "email": "info@pieterhordijk.com" + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" } ], - "description": "PHP 5.3+ oAuth 1/2 Library", + "description": "Provides a simple abstraction over streams of data", + "homepage": "http://guzzlephp.org/", "keywords": [ - "Authentication", - "authorization", - "oauth", - "security" + "Guzzle", + "stream" + ], + "time": "2014-10-12 19:18:40" + }, + { + "name": "react/promise", + "version": "v2.2.0", + "source": { + "type": "git", + "url": "https://github.com/reactphp/promise.git", + "reference": "365fcee430dfa4ace1fbc75737ca60ceea7eeeef" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/reactphp/promise/zipball/365fcee430dfa4ace1fbc75737ca60ceea7eeeef", + "reference": "365fcee430dfa4ace1fbc75737ca60ceea7eeeef", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + } + }, + "autoload": { + "psr-4": { + "React\\Promise\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" ], - "time": "2014-09-05 15:19:58" + "authors": [ + { + "name": "Jan Sorgalla", + "email": "jsorgalla@googlemail.com" + } + ], + "description": "A lightweight implementation of CommonJS Promises/A for PHP", + "time": "2014-12-30 13:32:42" } ], "packages-dev": [ @@ -1036,22 +1117,25 @@ }, { "name": "symfony/yaml", - "version": "v2.6.4", + "version": "v2.6.5", "target-dir": "Symfony/Component/Yaml", "source": { "type": "git", "url": "https://github.com/symfony/Yaml.git", - "reference": "60ed7751671113cf1ee7d7778e691642c2e9acd8" + "reference": "0cd8e72071e46e15fc072270ae39ea1b66b10a9d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Yaml/zipball/60ed7751671113cf1ee7d7778e691642c2e9acd8", - "reference": "60ed7751671113cf1ee7d7778e691642c2e9acd8", + "url": "https://api.github.com/repos/symfony/Yaml/zipball/0cd8e72071e46e15fc072270ae39ea1b66b10a9d", + "reference": "0cd8e72071e46e15fc072270ae39ea1b66b10a9d", "shasum": "" }, "require": { "php": ">=5.3.3" }, + "require-dev": { + "symfony/phpunit-bridge": "~2.7" + }, "type": "library", "extra": { "branch-alias": { @@ -1079,7 +1163,7 @@ ], "description": "Symfony Yaml Component", "homepage": "http://symfony.com", - "time": "2015-01-25 04:39:26" + "time": "2015-03-12 10:28:44" } ], "aliases": [], diff --git a/src/Endpoint/AuthenticatedEndpoint.php b/src/Endpoint/AuthenticatedEndpoint.php index 4f1842f..2fe4fad 100644 --- a/src/Endpoint/AuthenticatedEndpoint.php +++ b/src/Endpoint/AuthenticatedEndpoint.php @@ -2,27 +2,21 @@ namespace GW2Treasures\GW2Api\Endpoint; -use GW2Treasures\GW2Api\Request\RequestManager; +use GuzzleHttp\Client; abstract class AuthenticatedEndpoint extends Endpoint { protected $_token; - public function __construct( RequestManager $requestManager, $token ) { - parent::__construct( $requestManager ); + public function __construct( Client $client, $token, array $options = [] ) { + parent::__construct( $client, $options ); $this->_token = $token; } /** * {@inheritdoc} */ - protected function request( array $query = [] ) { - return $this->requestManager->request( $this->url(), $query, ['Authorization' => 'Bearer ' . $this->_token ]); - } - - /** - * {@inheritdoc} - */ - protected function requestMany( array $queries ) { - return $this->requestManager->request( $this->url(), $queries, ['Authorization' => 'Bearer ' . $this->_token ]); + protected function createRequest( array $query = [], $url = null, $method = 'GET', $options = [] ) { + $options = [ 'headers' => [ 'Authorization' => 'Bearer ' . $this->_token ]] + $options; + return parent::createRequest( $query, $url, $method, $options ); } } diff --git a/src/Endpoint/BulkEndpoint.php b/src/Endpoint/BulkEndpoint.php index a3ac53f..704531c 100644 --- a/src/Endpoint/BulkEndpoint.php +++ b/src/Endpoint/BulkEndpoint.php @@ -2,7 +2,8 @@ namespace GW2Treasures\GW2Api\Endpoint; -use GW2Treasures\GW2Api\Request\Response; +use GuzzleHttp\Client; +use GuzzleHttp\Pool; trait BulkEndpoint { @@ -35,7 +36,7 @@ protected function supportsIdsAll() { * @return string[]|int[] */ public function ids() { - return $this->request()->json(); + return $this->getClient()->send( $this->createRequest() )->json(); } /** @@ -45,7 +46,9 @@ public function ids() { * @return mixed */ public function get( $id ) { - return $this->request([ 'id' => $id ])->json(); + $request = $this->createRequest([ 'id' => $id ]); + $response = $this->getClient()->send( $request ); + return $response->json(['object' => true]); } /** @@ -61,15 +64,17 @@ public function many( array $ids = [] ) { $pages = array_chunk( $ids, $this->maxPageSize() ); - $queries = []; + $requests = []; foreach( $pages as $page ) { - $queries[] = ['ids' => implode( ',', $page )]; + $requests[] = $this->createRequest( ['ids' => implode( ',', $page )] ); } + $responses = Pool::batch( $this->getClient(), $requests, ['pool_size' => 128] ); + $result = []; - $responses = $this->requestMany( $queries ); + /** @var \GuzzleHttp\Message\Response $response */ foreach( $responses as $response ) { - $result = array_merge( $result, $response->json() ); + $result = array_merge( $result, $response->json(['object' => true]) ); } return $result; @@ -85,7 +90,7 @@ public function many( array $ids = [] ) { */ public function all() { if( $this->supportsIdsAll() ) { - return $this->request(['ids' => 'all'])->json(); + return $this->getClient()->send( $this->createRequest([ 'ids' => 'all' ]))->json(['object' => true]); } else { $ids = $this->ids(); return $this->many( $ids ); @@ -93,18 +98,18 @@ public function all() { } /** - * Implemented by GW2Treasures\GW2Api\Endpoint\Endpoint. - * - * @param string[] $query - * @return Response + * @return Client */ - protected abstract function request( array $query = [] ); + protected abstract function getClient(); /** - * Implemented by GW2Treasures\GW2Api\Endpoint\Endpoint. + * Creates a new Request to this Endpoint. * - * @param string[][] $queries - * @return \GW2Treasures\GW2Api\Request\Response[] + * @param string[] $query + * @param null $url + * @param string $method + * @param array $options + * @return \GuzzleHttp\Message\Request|\GuzzleHttp\Message\RequestInterface */ - protected abstract function requestMany( array $queries ); + protected abstract function createRequest( array $query = [], $url = null, $method = 'GET', $options = [] ); } diff --git a/src/Endpoint/Endpoint.php b/src/Endpoint/Endpoint.php index 0665d38..de19c31 100644 --- a/src/Endpoint/Endpoint.php +++ b/src/Endpoint/Endpoint.php @@ -2,39 +2,44 @@ namespace GW2Treasures\GW2Api\Endpoint; -use GW2Treasures\GW2Api\Request\RequestManager; +use GuzzleHttp\Client; +use GuzzleHttp\Query; abstract class Endpoint { + /** @var Client $client */ + protected $client; - /** @var RequestManager requestManager */ - protected $requestManager; + /** @var array $options */ + private $options; /** - * @param RequestManager $requestManager + * @param Client $client + * @param array $options */ - public function __construct( RequestManager $requestManager ) { - $this->requestManager = $requestManager; + public function __construct( Client $client, array $options = [] ) { + $this->client = $client; + $this->options = $options; } /** - * Make a request. - * - * @param string[] $query - * @return \GW2Treasures\GW2Api\Request\Response + * @return Client */ - protected function request( array $query = [] ) { - return $this->requestManager->request( $this->url(), $query ); + protected function getClient() { + return $this->client; } /** - * Make multiple requests at once. If the RequestManager supports parallel requests, this will be way faster. - * Otherwise the RequestManager will fallback to multiple simple requests. + * Creates a new Request to this Endpoint. * - * @param string[][] $queries - * @return \GW2Treasures\GW2Api\Request\Response[] + * @param string[] $query + * @param null $url + * @param string $method + * @param array $options + * @return \GuzzleHttp\Message\Request|\GuzzleHttp\Message\RequestInterface */ - protected function requestMany( array $queries ) { - return $this->requestManager->requestMany( $this->url(), $queries ); + protected function createRequest( array $query = [], $url = null, $method = 'GET', $options = [] ) { + $url = !is_null( $url ) ? $url : $this->url(); + return $this->client->createRequest( $method, $url, $options + [ 'query' => $query ]); } /** diff --git a/src/Endpoint/LocalizedEndpointProxy.php b/src/Endpoint/LocalizedEndpointProxy.php index fa94433..755c91d 100644 --- a/src/Endpoint/LocalizedEndpointProxy.php +++ b/src/Endpoint/LocalizedEndpointProxy.php @@ -11,7 +11,7 @@ class LocalizedEndpointProxy extends Endpoint { /** * @param Endpoint $origin - * @param string $lang + * @param string $lang */ public function __construct( Endpoint $origin, $lang ) { $this->origin = $origin; @@ -41,28 +41,26 @@ public function __call( $name, $arguments ) { /** * {@inheritdoc} */ - protected function request( array $query = [] ) { - return $this->origin->request( [ 'lang' => $this->lang ] + $query ); + protected function createRequest( array $query = [], $url = null, $method = 'GET', $options = [] ) { + $query = [ 'lang' => $this->lang ] + $query; + return $this->origin->createRequest( $query, $url, $method, $options ); } /** * {@inheritdoc} */ - protected function requestMany( array $queries ) { - $queries = array_map( function( $query ) { - return [ 'lang' => $this->lang ] + $query; - }, $queries ); - return $this->origin->requestMany( $queries ); + protected function url() { + return $this->origin->url(); } - /** * {@inheritdoc} */ - protected function url() { - return $this->origin->url(); + protected function getClient() { + return $this->origin->getClient(); } + /** * String representation of this localized endpoint. * diff --git a/src/Endpoint/V2/AccountEndpoint.php b/src/Endpoint/V2/AccountEndpoint.php index e6cf115..431ceab 100644 --- a/src/Endpoint/V2/AccountEndpoint.php +++ b/src/Endpoint/V2/AccountEndpoint.php @@ -19,6 +19,6 @@ protected function url() { * @return mixed */ public function info() { - return $this->request()->json(); + return $this->getClient()->send( $this->createRequest() )->json([ 'object' => true ]); } } diff --git a/src/Endpoint/V2/BuildEndpoint.php b/src/Endpoint/V2/BuildEndpoint.php index 9f2f4a1..b5d2686 100644 --- a/src/Endpoint/V2/BuildEndpoint.php +++ b/src/Endpoint/V2/BuildEndpoint.php @@ -19,6 +19,6 @@ protected function url() { * @return int */ public function get() { - return $this->request()->json()->id; + return $this->getClient()->send( $this->createRequest() )->json()['id']; } } diff --git a/src/Endpoint/V2/CommerceEndpoint.php b/src/Endpoint/V2/CommerceEndpoint.php index 0b8ea29..ff50f97 100644 --- a/src/Endpoint/V2/CommerceEndpoint.php +++ b/src/Endpoint/V2/CommerceEndpoint.php @@ -17,20 +17,20 @@ protected function url() { * @return CommerceExchangeEndpoint */ public function exchange() { - return new CommerceExchangeEndpoint( $this->requestManager ); + return new CommerceExchangeEndpoint( $this->client ); } /** * @return CommerceListingEndpoint */ public function listings() { - return new CommerceListingEndpoint( $this->requestManager ); + return new CommerceListingEndpoint( $this->client ); } /** * @return CommercePriceEndpoint */ public function prices() { - return new CommercePriceEndpoint( $this->requestManager ); + return new CommercePriceEndpoint( $this->client ); } } diff --git a/src/Endpoint/V2/CommerceExchangeEndpoint.php b/src/Endpoint/V2/CommerceExchangeEndpoint.php index 577c5d4..15facbd 100644 --- a/src/Endpoint/V2/CommerceExchangeEndpoint.php +++ b/src/Endpoint/V2/CommerceExchangeEndpoint.php @@ -20,7 +20,8 @@ protected function url() { * @return mixed */ public function gems( $quantity ) { - return $this->requestManager->request( $this->url() . '/gems', [ 'quantity' => $quantity ])->json(); + $request = $this->createRequest([ 'quantity' => $quantity ], $this->url() . '/gems' ); + return $this->getClient()->send( $request )->json([ 'object' => true ]); } /** @@ -30,6 +31,7 @@ public function gems( $quantity ) { * @return mixed */ public function coins( $quantity ) { - return $this->requestManager->request( $this->url() . '/coins', [ 'quantity' => $quantity ])->json(); + $request = $this->createRequest([ 'quantity' => $quantity ], $this->url() . '/coins' ); + return $this->getClient()->send( $request )->json([ 'object' => true ]); } } diff --git a/src/Endpoint/V2/RecipeEndpoint.php b/src/Endpoint/V2/RecipeEndpoint.php index 5ae9474..666f0a2 100644 --- a/src/Endpoint/V2/RecipeEndpoint.php +++ b/src/Endpoint/V2/RecipeEndpoint.php @@ -22,6 +22,6 @@ protected function url() { * @return RecipeSearchEndpoint */ public function search() { - return new RecipeSearchEndpoint( $this->requestManager ); + return new RecipeSearchEndpoint( $this->client ); } } diff --git a/src/Endpoint/V2/RecipeSearchEndpoint.php b/src/Endpoint/V2/RecipeSearchEndpoint.php index 75c607b..aad1e81 100644 --- a/src/Endpoint/V2/RecipeSearchEndpoint.php +++ b/src/Endpoint/V2/RecipeSearchEndpoint.php @@ -19,7 +19,8 @@ protected function url() { * @return mixed */ public function input( $id ) { - return $this->request([ 'input' => $id ])->json(); + $request = $this->createRequest([ 'input' => $id ]); + return $this->getClient()->send( $request )->json([ 'object' => true ]); } /** @@ -27,6 +28,7 @@ public function input( $id ) { * @return mixed */ public function output( $id ) { - return $this->request([ 'output' => $id ])->json(); + $request = $this->createRequest([ 'output' => $id ]); + return $this->getClient()->send( $request )->json([ 'object' => true ]); } } diff --git a/src/GW2Api.php b/src/GW2Api.php index 1e0601f..a93cb52 100644 --- a/src/GW2Api.php +++ b/src/GW2Api.php @@ -2,6 +2,8 @@ namespace GW2Treasures\GW2Api; +use GuzzleHttp\Client; +use GuzzleHttp\Ring\Client\CurlMultiHandler; use GW2Treasures\GW2Api\Endpoint\V2\AccountEndpoint; use GW2Treasures\GW2Api\Endpoint\V2\BuildEndpoint; use GW2Treasures\GW2Api\Endpoint\V2\ColorEndpoint; @@ -14,65 +16,78 @@ use GW2Treasures\GW2Api\Endpoint\V2\RecipeEndpoint; use GW2Treasures\GW2Api\Endpoint\V2\SkinEndpoint; use GW2Treasures\GW2Api\Endpoint\V2\WorldEndpoint; -use GW2Treasures\GW2Api\Request\RequestManager; -use GW2Treasures\GW2Api\Request\RollingCurl\RequestManager as RollingCurlRequestManager; class GW2Api { - /** @var RequestManager $requestManager */ - protected $requestManager; - /** @var string $apiUrl */ protected $apiUrl = 'https://api.guildwars2.com/'; - function __construct( RequestManager $requestManager = null ) { - $this->requestManager = $requestManager ?: new RollingCurlRequestManager( $this->apiUrl ); + /** @var Client $client */ + protected $client; + + /** @var array $options */ + protected $options; + + function __construct( array $options = [] ) { + $this->options = $this->getOptions( $options ); + + $this->client = new Client( $this->options ); + $this->options = $options; + } + + protected function getOptions( array $options = [] ) { + return [ + 'base_url' => $this->apiUrl, + 'defaults' => [ + 'verify' => __DIR__ . DIRECTORY_SEPARATOR . 'cacert.pem' + ] + ] + $options; } public function account( $access_token ) { - return new AccountEndpoint( $this->requestManager, $access_token ); + return new AccountEndpoint( $this->client, $access_token ); } public function build() { - return new BuildEndpoint( $this->requestManager ); + return new BuildEndpoint( $this->client ); } public function colors() { - return new ColorEndpoint( $this->requestManager ); + return new ColorEndpoint( $this->client ); } public function commerce() { - return new CommerceEndpoint( $this->requestManager ); + return new CommerceEndpoint( $this->client ); } public function continents() { - return new ContinentEndpoint( $this->requestManager ); + return new ContinentEndpoint( $this->client ); } public function files() { - return new FileEndpoint( $this->requestManager ); + return new FileEndpoint( $this->client ); } public function items() { - return new ItemEndpoint( $this->requestManager ); + return new ItemEndpoint( $this->client ); } public function maps() { - return new MapEndpoint( $this->requestManager ); + return new MapEndpoint( $this->client ); } public function quaggans() { - return new QuagganEndpoint( $this->requestManager ); + return new QuagganEndpoint( $this->client ); } public function recipes() { - return new RecipeEndpoint( $this->requestManager ); + return new RecipeEndpoint( $this->client ); } public function skins() { - return new SkinEndpoint( $this->requestManager ); + return new SkinEndpoint( $this->client ); } public function worlds() { - return new WorldEndpoint( $this->requestManager ); + return new WorldEndpoint( $this->client ); } } diff --git a/src/Request/RequestManager.php b/src/Request/RequestManager.php deleted file mode 100644 index bb9ee1a..0000000 --- a/src/Request/RequestManager.php +++ /dev/null @@ -1,41 +0,0 @@ -baseUrl = $baseUrl; - } - - public function getCertPath() { - return __DIR__ . DIRECTORY_SEPARATOR . 'cacert.pem'; - } - - /** - * @param string $url - * @param string[] $query - * @param string[] $header - * @return Response - */ - public abstract function request( $url, array $query = [], array $header = [] ); - - /** - * @param string $url - * @param string[][] $queries - * @param string[] $header - */ - public abstract function requestMany( $url, array $queries, array $header = [] ); - - /** - * @param string $useragent - */ - public function setUseragent( $useragent) { - $this->useragent = $useragent; - } -} diff --git a/src/Request/Response.php b/src/Request/Response.php deleted file mode 100644 index ac0c84a..0000000 --- a/src/Request/Response.php +++ /dev/null @@ -1,50 +0,0 @@ -headers(); - return array_key_exists( $name, $headers ) - ? $headers[ $name ] - : $default; - } - - /** - * Content of this Response parsed as json. - * - * @return mixed - */ - public function json() { - return json_decode( $this->body() ); - } -} diff --git a/src/Request/RollingCurl/RequestManager.php b/src/Request/RollingCurl/RequestManager.php deleted file mode 100644 index a216380..0000000 --- a/src/Request/RollingCurl/RequestManager.php +++ /dev/null @@ -1,71 +0,0 @@ -requestMany( $url, [ $query ], $header )[0]; - } - - /** - * @param string $url - * @param string[][] $queries - * @param array $header - * @return Response[] - * @throws \Exception - */ - public function requestMany( $url, array $queries, array $header = [] ) { - $curl = $this->getCurl(); - - // build curl header array from associative array - $header = array_map( function( $v, $k ) { - return $k . ': ' . $v; - }, $header, array_keys($header) ); - - $responses = []; - - //echo '[GET] --- start ---' . PHP_EOL; - //$start = -microtime(true); - - foreach( $queries as $query ) { - $queryUrl = $url; - if( count( $query ) > 0 ) { - $queryUrl .= '?' . http_build_query( $query ); - } - //echo '[GET] ' . $queryUrl . PHP_EOL; - $curl->get( $this->baseUrl . $queryUrl, null, [ CURLOPT_HTTPHEADER => $header ] ); - } - - $curl->setCallback( function( Request $request ) use ( &$responses ) { - $responses[] = new Response( $request ); - }); - - $curl->execute(); - //echo '[GET] --- end: ' . ( ($start + microtime( true )) * 1000 ) . 'ms ---' . PHP_EOL; - - return $responses; - } - - protected function getCurl() { - $curl = new RollingCurl(); - $curl->addOptions([ - CURLOPT_CAINFO => $this->getCertPath(), - CURLOPT_HEADER => true, - CURLOPT_FAILONERROR => false, -// CURLOPT_USERAGENT => 'GW2Treasures Bot/1.0 (+https://gw2treasures.com/contact)' - ]); - return $curl; - } - -} diff --git a/src/Request/RollingCurl/Response.php b/src/Request/RollingCurl/Response.php deleted file mode 100644 index 4b5f700..0000000 --- a/src/Request/RollingCurl/Response.php +++ /dev/null @@ -1,66 +0,0 @@ -parseResponse( $request ); - } - - /** - * @param Request $request - */ - protected function parseResponse( Request $request ) { - $info = $request->getResponseInfo(); - $text = $request->getResponseText(); - $header_size = $info['header_size']; - - $headerRaw = trim( substr( $text, 0, $header_size )); - $headers = array(); - foreach( explode( "\n", $headerRaw ) as $i => $line ) { - if( $i === 0 ) { - $headers['http_code'] = trim($line); - } else { - list( $key, $value ) = explode( ': ', $line, 2 ); - $headers[ trim( $key ) ] = trim( $value ); - } - } - - $this->status = $info['http_code']; - $this->headers = $headers; - $this->body = substr( $text, $header_size ); - } - - /** - * {@inheritdoc} - */ - public function headers() { - return $this->headers; - } - - /** - * {@inheritdoc} - */ - public function body() { - return $this->body; - } - - /** - * {@inheritdoc} - */ - public function status() { - return $this->status; - } -} diff --git a/src/Request/cacert.pem b/src/cacert.pem similarity index 100% rename from src/Request/cacert.pem rename to src/cacert.pem