From e0f19f7bdb0ce8a2ca9933524b8936e8a6dd1af9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gildas=20Qu=C3=A9m=C3=A9ner?= Date: Fri, 22 Jan 2016 16:40:41 +0100 Subject: [PATCH] Abstract the HTTP Client by using HTTPlug --- README.md | 59 +++----- composer.json | 14 +- spec/Github/HttpClient/HttplugClientSpec.php | 59 ++++++++ {lib => src}/Github/Api/AbstractApi.php | 0 {lib => src}/Github/Api/ApiInterface.php | 0 {lib => src}/Github/Api/Authorizations.php | 0 {lib => src}/Github/Api/CurrentUser.php | 0 .../Github/Api/CurrentUser/DeployKeys.php | 0 .../Github/Api/CurrentUser/Emails.php | 0 .../Github/Api/CurrentUser/Followers.php | 0 .../Github/Api/CurrentUser/Memberships.php | 0 .../Github/Api/CurrentUser/Notifications.php | 0 .../Github/Api/CurrentUser/Starring.php | 0 .../Github/Api/CurrentUser/Watchers.php | 0 {lib => src}/Github/Api/Deployment.php | 0 {lib => src}/Github/Api/Enterprise.php | 0 .../Github/Api/Enterprise/License.php | 0 .../Api/Enterprise/ManagementConsole.php | 0 {lib => src}/Github/Api/Enterprise/Stats.php | 0 .../Github/Api/Enterprise/UserAdmin.php | 0 {lib => src}/Github/Api/Gist/Comments.php | 0 {lib => src}/Github/Api/Gists.php | 0 {lib => src}/Github/Api/GitData.php | 0 {lib => src}/Github/Api/GitData/Blobs.php | 0 {lib => src}/Github/Api/GitData/Commits.php | 0 .../Github/Api/GitData/References.php | 0 {lib => src}/Github/Api/GitData/Tags.php | 0 {lib => src}/Github/Api/GitData/Trees.php | 0 {lib => src}/Github/Api/Issue.php | 0 {lib => src}/Github/Api/Issue/Comments.php | 0 {lib => src}/Github/Api/Issue/Events.php | 0 {lib => src}/Github/Api/Issue/Labels.php | 0 {lib => src}/Github/Api/Issue/Milestones.php | 0 {lib => src}/Github/Api/Markdown.php | 0 {lib => src}/Github/Api/Meta.php | 0 {lib => src}/Github/Api/Notification.php | 0 {lib => src}/Github/Api/Organization.php | 0 .../Github/Api/Organization/Hooks.php | 0 .../Github/Api/Organization/Members.php | 0 .../Github/Api/Organization/Teams.php | 0 {lib => src}/Github/Api/PullRequest.php | 0 .../Github/Api/PullRequest/Comments.php | 0 {lib => src}/Github/Api/RateLimit.php | 0 {lib => src}/Github/Api/Repo.php | 0 {lib => src}/Github/Api/Repository/Assets.php | 0 .../Github/Api/Repository/Collaborators.php | 0 .../Github/Api/Repository/Comments.php | 0 .../Github/Api/Repository/Commits.php | 0 .../Github/Api/Repository/Contents.php | 0 .../Github/Api/Repository/DeployKeys.php | 0 .../Github/Api/Repository/Downloads.php | 0 {lib => src}/Github/Api/Repository/Forks.php | 0 {lib => src}/Github/Api/Repository/Hooks.php | 0 {lib => src}/Github/Api/Repository/Labels.php | 0 .../Github/Api/Repository/Releases.php | 0 .../Github/Api/Repository/Statuses.php | 0 {lib => src}/Github/Api/Search.php | 0 {lib => src}/Github/Api/User.php | 0 {lib => src}/Github/Client.php | 0 src/Github/Factory/RequestFactory.php | 13 ++ .../Github/HttpClient/HttpClientInterface.php | 0 src/Github/HttpClient/HttplugClient.php | 131 ++++++++++++++++++ .../HttpClient/Message/ResponseMediator.php | 12 +- 63 files changed, 234 insertions(+), 54 deletions(-) create mode 100644 spec/Github/HttpClient/HttplugClientSpec.php rename {lib => src}/Github/Api/AbstractApi.php (100%) rename {lib => src}/Github/Api/ApiInterface.php (100%) rename {lib => src}/Github/Api/Authorizations.php (100%) rename {lib => src}/Github/Api/CurrentUser.php (100%) rename {lib => src}/Github/Api/CurrentUser/DeployKeys.php (100%) rename {lib => src}/Github/Api/CurrentUser/Emails.php (100%) rename {lib => src}/Github/Api/CurrentUser/Followers.php (100%) rename {lib => src}/Github/Api/CurrentUser/Memberships.php (100%) rename {lib => src}/Github/Api/CurrentUser/Notifications.php (100%) rename {lib => src}/Github/Api/CurrentUser/Starring.php (100%) rename {lib => src}/Github/Api/CurrentUser/Watchers.php (100%) rename {lib => src}/Github/Api/Deployment.php (100%) rename {lib => src}/Github/Api/Enterprise.php (100%) rename {lib => src}/Github/Api/Enterprise/License.php (100%) rename {lib => src}/Github/Api/Enterprise/ManagementConsole.php (100%) rename {lib => src}/Github/Api/Enterprise/Stats.php (100%) rename {lib => src}/Github/Api/Enterprise/UserAdmin.php (100%) rename {lib => src}/Github/Api/Gist/Comments.php (100%) rename {lib => src}/Github/Api/Gists.php (100%) rename {lib => src}/Github/Api/GitData.php (100%) rename {lib => src}/Github/Api/GitData/Blobs.php (100%) rename {lib => src}/Github/Api/GitData/Commits.php (100%) rename {lib => src}/Github/Api/GitData/References.php (100%) rename {lib => src}/Github/Api/GitData/Tags.php (100%) rename {lib => src}/Github/Api/GitData/Trees.php (100%) rename {lib => src}/Github/Api/Issue.php (100%) rename {lib => src}/Github/Api/Issue/Comments.php (100%) rename {lib => src}/Github/Api/Issue/Events.php (100%) rename {lib => src}/Github/Api/Issue/Labels.php (100%) rename {lib => src}/Github/Api/Issue/Milestones.php (100%) rename {lib => src}/Github/Api/Markdown.php (100%) rename {lib => src}/Github/Api/Meta.php (100%) rename {lib => src}/Github/Api/Notification.php (100%) rename {lib => src}/Github/Api/Organization.php (100%) rename {lib => src}/Github/Api/Organization/Hooks.php (100%) rename {lib => src}/Github/Api/Organization/Members.php (100%) rename {lib => src}/Github/Api/Organization/Teams.php (100%) rename {lib => src}/Github/Api/PullRequest.php (100%) rename {lib => src}/Github/Api/PullRequest/Comments.php (100%) rename {lib => src}/Github/Api/RateLimit.php (100%) rename {lib => src}/Github/Api/Repo.php (100%) rename {lib => src}/Github/Api/Repository/Assets.php (100%) rename {lib => src}/Github/Api/Repository/Collaborators.php (100%) rename {lib => src}/Github/Api/Repository/Comments.php (100%) rename {lib => src}/Github/Api/Repository/Commits.php (100%) rename {lib => src}/Github/Api/Repository/Contents.php (100%) rename {lib => src}/Github/Api/Repository/DeployKeys.php (100%) rename {lib => src}/Github/Api/Repository/Downloads.php (100%) rename {lib => src}/Github/Api/Repository/Forks.php (100%) rename {lib => src}/Github/Api/Repository/Hooks.php (100%) rename {lib => src}/Github/Api/Repository/Labels.php (100%) rename {lib => src}/Github/Api/Repository/Releases.php (100%) rename {lib => src}/Github/Api/Repository/Statuses.php (100%) rename {lib => src}/Github/Api/Search.php (100%) rename {lib => src}/Github/Api/User.php (100%) rename {lib => src}/Github/Client.php (100%) create mode 100644 src/Github/Factory/RequestFactory.php rename {lib => src}/Github/HttpClient/HttpClientInterface.php (100%) create mode 100644 src/Github/HttpClient/HttplugClient.php rename {lib => src}/Github/HttpClient/Message/ResponseMediator.php (77%) diff --git a/README.md b/README.md index cfac351015f..6d9da101c7e 100644 --- a/README.md +++ b/README.md @@ -15,34 +15,24 @@ Uses [GitHub API v3](http://developer.github.com/v3/). The object API is very si ## Requirements -* PHP >= 5.3.2 with [cURL](http://php.net/manual/en/book.curl.php) extension, -* [Guzzle](https://github.com/guzzle/guzzle) library, -* (optional) PHPUnit to run tests. +* PHP >= 5.4 +* (optional) [php-httplug/guzzle5-adapter](https://github.com/php-http/guzzle5-adapter) to use with a guzzle 5 http client +* (optional) [php-httplug/guzzle6-adapter](https://github.com/php-http/guzzle6-adapter) to use with a guzzle 6 http client ## Autoload -The new version of `php-github-api` using [Composer](http://getcomposer.org). The first step to use `php-github-api` is to download composer: ```bash $ curl -s http://getcomposer.org/installer | php ``` -Then we have to install our dependencies using: +Then run the following command to require the library: ```bash -$ php composer.phar install -``` -Now we can use autoloader from Composer by: - -```json -{ - "require": { - "knplabs/github-api": "~1.4" - } -} +$ php composer.phar require knplabs/github-api ``` -> `php-github-api` follows the PSR-0 convention names for its classes, which means you can easily integrate `php-github-api` classes loading in your own autoloader. +> `php-github-api` follows the PSR-4 convention names for its classes, which means you can easily integrate `php-github-api` classes loading in your own autoloader. ## Using Laravel? @@ -56,38 +46,23 @@ Now we can use autoloader from Composer by: // This file is generated by Composer require_once 'vendor/autoload.php'; -$client = new \Github\Client(); -$repositories = $client->api('user')->repositories('ornicar'); -``` - -From `$client` object, you can access to all GitHub. - -## Cache usage - -```php - '/tmp/github-api-cache')) + new \Github\HttpClient\HttplugClient( + new \Http\Adapter\Guzzle6\Client( // Only available when you require the php-httplug/guzzle6-adapter lib + new \GuzzleHttp\Client() + ), + new \Github\Factory\RequestFactory() + ) ); -// Or select directly which cache you want to use -$client = new \Github\HttpClient\CachedHttpClient(); -$client->setCache( - // Built in one, or any cache implementing this interface: - // Github\HttpClient\Cache\CacheInterface - new \Github\HttpClient\Cache\FilesystemCache('/tmp/github-api-cache') -); - -$client = new \Github\Client($client); +$users = $client->api('user')->all(); ``` -Using cache, the client will get cached responses if resources haven't changed since last time, -**without** reaching the `X-Rate-Limit` [imposed by github](http://developer.github.com/v3/#rate-limiting). +From `$client` object, you can access to all GitHub. + +## Cache usage +TBD ## Documentation diff --git a/composer.json b/composer.json index ca541c41c3b..7d31624c625 100644 --- a/composer.json +++ b/composer.json @@ -17,23 +17,25 @@ } ], "require": { - "php": ">=5.3.2", - "ext-curl": "*", - "guzzle/guzzle": "~3.7" + "php": ">=5.4", + "php-http/httplug": "1.0.0-RC1", + "php-http/promise": "1.0.0-RC1", + "zendframework/zend-diactoros": "^1.3" }, "require-dev": { "phpunit/phpunit": "~4.0", - "sllh/php-cs-fixer-styleci-bridge": "~1.3" + "phpspec/phpspec": "^2.4", + "php-http/guzzle6-adapter": "^0.4.1" }, "suggest": { "knplabs/gaufrette": "Needed for optional Gaufrette cache" }, "autoload": { - "psr-4": { "Github\\": "lib/Github/" } + "psr-4": { "Github\\": "src/Github/" } }, "extra": { "branch-alias": { - "dev-master": "1.5.x-dev" + "dev-master": "2.0.x-dev" } } } diff --git a/spec/Github/HttpClient/HttplugClientSpec.php b/spec/Github/HttpClient/HttplugClientSpec.php new file mode 100644 index 00000000000..8960d970a53 --- /dev/null +++ b/spec/Github/HttpClient/HttplugClientSpec.php @@ -0,0 +1,59 @@ +beConstructedWith($adapter, $factory); + } + + function it_is_a_http_client() + { + $this->shouldBeAnInstanceOf('Github\HttpClient\HttpClientInterface'); + } + + function it_sends_GET_request( + RequestInterface $request, + ResponseInterface $response, + $adapter, + $factory + ) { + $headers = [ + 'Accept' => 'application/vnd.github.v3+json', + 'User-Agent' => 'php-github-api (http://github.com/KnpLabs/php-github-api)', + 'X-Debug-Token' => '13fe23ab', + ]; + + $factory->createRequest('GET', 'https://api.github.com/endpoint?page=1', $headers)->willReturn($request); + $adapter->sendRequest($request)->willReturn($response); + + $this->get('/endpoint', ['page' => 1], ['X-Debug-Token' => '13fe23ab'])->shouldReturn($response); + } + + function it_sends_POST_request( + RequestInterface $request, + ResponseInterface $response, + $adapter, + $factory + ) { + $headers = [ + 'Accept' => 'application/vnd.github.v3+json', + 'User-Agent' => 'php-github-api (http://github.com/KnpLabs/php-github-api)', + 'X-Debug-Token' => '13fe23ab', + ]; + + $factory->createRequest('POST', 'https://api.github.com/endpoint', $headers, 'body')->willReturn($request); + $adapter->sendRequest($request)->willReturn($response); + + $this->post('/endpoint', 'body', ['X-Debug-Token' => '13fe23ab'])->shouldReturn($response); + } +} diff --git a/lib/Github/Api/AbstractApi.php b/src/Github/Api/AbstractApi.php similarity index 100% rename from lib/Github/Api/AbstractApi.php rename to src/Github/Api/AbstractApi.php diff --git a/lib/Github/Api/ApiInterface.php b/src/Github/Api/ApiInterface.php similarity index 100% rename from lib/Github/Api/ApiInterface.php rename to src/Github/Api/ApiInterface.php diff --git a/lib/Github/Api/Authorizations.php b/src/Github/Api/Authorizations.php similarity index 100% rename from lib/Github/Api/Authorizations.php rename to src/Github/Api/Authorizations.php diff --git a/lib/Github/Api/CurrentUser.php b/src/Github/Api/CurrentUser.php similarity index 100% rename from lib/Github/Api/CurrentUser.php rename to src/Github/Api/CurrentUser.php diff --git a/lib/Github/Api/CurrentUser/DeployKeys.php b/src/Github/Api/CurrentUser/DeployKeys.php similarity index 100% rename from lib/Github/Api/CurrentUser/DeployKeys.php rename to src/Github/Api/CurrentUser/DeployKeys.php diff --git a/lib/Github/Api/CurrentUser/Emails.php b/src/Github/Api/CurrentUser/Emails.php similarity index 100% rename from lib/Github/Api/CurrentUser/Emails.php rename to src/Github/Api/CurrentUser/Emails.php diff --git a/lib/Github/Api/CurrentUser/Followers.php b/src/Github/Api/CurrentUser/Followers.php similarity index 100% rename from lib/Github/Api/CurrentUser/Followers.php rename to src/Github/Api/CurrentUser/Followers.php diff --git a/lib/Github/Api/CurrentUser/Memberships.php b/src/Github/Api/CurrentUser/Memberships.php similarity index 100% rename from lib/Github/Api/CurrentUser/Memberships.php rename to src/Github/Api/CurrentUser/Memberships.php diff --git a/lib/Github/Api/CurrentUser/Notifications.php b/src/Github/Api/CurrentUser/Notifications.php similarity index 100% rename from lib/Github/Api/CurrentUser/Notifications.php rename to src/Github/Api/CurrentUser/Notifications.php diff --git a/lib/Github/Api/CurrentUser/Starring.php b/src/Github/Api/CurrentUser/Starring.php similarity index 100% rename from lib/Github/Api/CurrentUser/Starring.php rename to src/Github/Api/CurrentUser/Starring.php diff --git a/lib/Github/Api/CurrentUser/Watchers.php b/src/Github/Api/CurrentUser/Watchers.php similarity index 100% rename from lib/Github/Api/CurrentUser/Watchers.php rename to src/Github/Api/CurrentUser/Watchers.php diff --git a/lib/Github/Api/Deployment.php b/src/Github/Api/Deployment.php similarity index 100% rename from lib/Github/Api/Deployment.php rename to src/Github/Api/Deployment.php diff --git a/lib/Github/Api/Enterprise.php b/src/Github/Api/Enterprise.php similarity index 100% rename from lib/Github/Api/Enterprise.php rename to src/Github/Api/Enterprise.php diff --git a/lib/Github/Api/Enterprise/License.php b/src/Github/Api/Enterprise/License.php similarity index 100% rename from lib/Github/Api/Enterprise/License.php rename to src/Github/Api/Enterprise/License.php diff --git a/lib/Github/Api/Enterprise/ManagementConsole.php b/src/Github/Api/Enterprise/ManagementConsole.php similarity index 100% rename from lib/Github/Api/Enterprise/ManagementConsole.php rename to src/Github/Api/Enterprise/ManagementConsole.php diff --git a/lib/Github/Api/Enterprise/Stats.php b/src/Github/Api/Enterprise/Stats.php similarity index 100% rename from lib/Github/Api/Enterprise/Stats.php rename to src/Github/Api/Enterprise/Stats.php diff --git a/lib/Github/Api/Enterprise/UserAdmin.php b/src/Github/Api/Enterprise/UserAdmin.php similarity index 100% rename from lib/Github/Api/Enterprise/UserAdmin.php rename to src/Github/Api/Enterprise/UserAdmin.php diff --git a/lib/Github/Api/Gist/Comments.php b/src/Github/Api/Gist/Comments.php similarity index 100% rename from lib/Github/Api/Gist/Comments.php rename to src/Github/Api/Gist/Comments.php diff --git a/lib/Github/Api/Gists.php b/src/Github/Api/Gists.php similarity index 100% rename from lib/Github/Api/Gists.php rename to src/Github/Api/Gists.php diff --git a/lib/Github/Api/GitData.php b/src/Github/Api/GitData.php similarity index 100% rename from lib/Github/Api/GitData.php rename to src/Github/Api/GitData.php diff --git a/lib/Github/Api/GitData/Blobs.php b/src/Github/Api/GitData/Blobs.php similarity index 100% rename from lib/Github/Api/GitData/Blobs.php rename to src/Github/Api/GitData/Blobs.php diff --git a/lib/Github/Api/GitData/Commits.php b/src/Github/Api/GitData/Commits.php similarity index 100% rename from lib/Github/Api/GitData/Commits.php rename to src/Github/Api/GitData/Commits.php diff --git a/lib/Github/Api/GitData/References.php b/src/Github/Api/GitData/References.php similarity index 100% rename from lib/Github/Api/GitData/References.php rename to src/Github/Api/GitData/References.php diff --git a/lib/Github/Api/GitData/Tags.php b/src/Github/Api/GitData/Tags.php similarity index 100% rename from lib/Github/Api/GitData/Tags.php rename to src/Github/Api/GitData/Tags.php diff --git a/lib/Github/Api/GitData/Trees.php b/src/Github/Api/GitData/Trees.php similarity index 100% rename from lib/Github/Api/GitData/Trees.php rename to src/Github/Api/GitData/Trees.php diff --git a/lib/Github/Api/Issue.php b/src/Github/Api/Issue.php similarity index 100% rename from lib/Github/Api/Issue.php rename to src/Github/Api/Issue.php diff --git a/lib/Github/Api/Issue/Comments.php b/src/Github/Api/Issue/Comments.php similarity index 100% rename from lib/Github/Api/Issue/Comments.php rename to src/Github/Api/Issue/Comments.php diff --git a/lib/Github/Api/Issue/Events.php b/src/Github/Api/Issue/Events.php similarity index 100% rename from lib/Github/Api/Issue/Events.php rename to src/Github/Api/Issue/Events.php diff --git a/lib/Github/Api/Issue/Labels.php b/src/Github/Api/Issue/Labels.php similarity index 100% rename from lib/Github/Api/Issue/Labels.php rename to src/Github/Api/Issue/Labels.php diff --git a/lib/Github/Api/Issue/Milestones.php b/src/Github/Api/Issue/Milestones.php similarity index 100% rename from lib/Github/Api/Issue/Milestones.php rename to src/Github/Api/Issue/Milestones.php diff --git a/lib/Github/Api/Markdown.php b/src/Github/Api/Markdown.php similarity index 100% rename from lib/Github/Api/Markdown.php rename to src/Github/Api/Markdown.php diff --git a/lib/Github/Api/Meta.php b/src/Github/Api/Meta.php similarity index 100% rename from lib/Github/Api/Meta.php rename to src/Github/Api/Meta.php diff --git a/lib/Github/Api/Notification.php b/src/Github/Api/Notification.php similarity index 100% rename from lib/Github/Api/Notification.php rename to src/Github/Api/Notification.php diff --git a/lib/Github/Api/Organization.php b/src/Github/Api/Organization.php similarity index 100% rename from lib/Github/Api/Organization.php rename to src/Github/Api/Organization.php diff --git a/lib/Github/Api/Organization/Hooks.php b/src/Github/Api/Organization/Hooks.php similarity index 100% rename from lib/Github/Api/Organization/Hooks.php rename to src/Github/Api/Organization/Hooks.php diff --git a/lib/Github/Api/Organization/Members.php b/src/Github/Api/Organization/Members.php similarity index 100% rename from lib/Github/Api/Organization/Members.php rename to src/Github/Api/Organization/Members.php diff --git a/lib/Github/Api/Organization/Teams.php b/src/Github/Api/Organization/Teams.php similarity index 100% rename from lib/Github/Api/Organization/Teams.php rename to src/Github/Api/Organization/Teams.php diff --git a/lib/Github/Api/PullRequest.php b/src/Github/Api/PullRequest.php similarity index 100% rename from lib/Github/Api/PullRequest.php rename to src/Github/Api/PullRequest.php diff --git a/lib/Github/Api/PullRequest/Comments.php b/src/Github/Api/PullRequest/Comments.php similarity index 100% rename from lib/Github/Api/PullRequest/Comments.php rename to src/Github/Api/PullRequest/Comments.php diff --git a/lib/Github/Api/RateLimit.php b/src/Github/Api/RateLimit.php similarity index 100% rename from lib/Github/Api/RateLimit.php rename to src/Github/Api/RateLimit.php diff --git a/lib/Github/Api/Repo.php b/src/Github/Api/Repo.php similarity index 100% rename from lib/Github/Api/Repo.php rename to src/Github/Api/Repo.php diff --git a/lib/Github/Api/Repository/Assets.php b/src/Github/Api/Repository/Assets.php similarity index 100% rename from lib/Github/Api/Repository/Assets.php rename to src/Github/Api/Repository/Assets.php diff --git a/lib/Github/Api/Repository/Collaborators.php b/src/Github/Api/Repository/Collaborators.php similarity index 100% rename from lib/Github/Api/Repository/Collaborators.php rename to src/Github/Api/Repository/Collaborators.php diff --git a/lib/Github/Api/Repository/Comments.php b/src/Github/Api/Repository/Comments.php similarity index 100% rename from lib/Github/Api/Repository/Comments.php rename to src/Github/Api/Repository/Comments.php diff --git a/lib/Github/Api/Repository/Commits.php b/src/Github/Api/Repository/Commits.php similarity index 100% rename from lib/Github/Api/Repository/Commits.php rename to src/Github/Api/Repository/Commits.php diff --git a/lib/Github/Api/Repository/Contents.php b/src/Github/Api/Repository/Contents.php similarity index 100% rename from lib/Github/Api/Repository/Contents.php rename to src/Github/Api/Repository/Contents.php diff --git a/lib/Github/Api/Repository/DeployKeys.php b/src/Github/Api/Repository/DeployKeys.php similarity index 100% rename from lib/Github/Api/Repository/DeployKeys.php rename to src/Github/Api/Repository/DeployKeys.php diff --git a/lib/Github/Api/Repository/Downloads.php b/src/Github/Api/Repository/Downloads.php similarity index 100% rename from lib/Github/Api/Repository/Downloads.php rename to src/Github/Api/Repository/Downloads.php diff --git a/lib/Github/Api/Repository/Forks.php b/src/Github/Api/Repository/Forks.php similarity index 100% rename from lib/Github/Api/Repository/Forks.php rename to src/Github/Api/Repository/Forks.php diff --git a/lib/Github/Api/Repository/Hooks.php b/src/Github/Api/Repository/Hooks.php similarity index 100% rename from lib/Github/Api/Repository/Hooks.php rename to src/Github/Api/Repository/Hooks.php diff --git a/lib/Github/Api/Repository/Labels.php b/src/Github/Api/Repository/Labels.php similarity index 100% rename from lib/Github/Api/Repository/Labels.php rename to src/Github/Api/Repository/Labels.php diff --git a/lib/Github/Api/Repository/Releases.php b/src/Github/Api/Repository/Releases.php similarity index 100% rename from lib/Github/Api/Repository/Releases.php rename to src/Github/Api/Repository/Releases.php diff --git a/lib/Github/Api/Repository/Statuses.php b/src/Github/Api/Repository/Statuses.php similarity index 100% rename from lib/Github/Api/Repository/Statuses.php rename to src/Github/Api/Repository/Statuses.php diff --git a/lib/Github/Api/Search.php b/src/Github/Api/Search.php similarity index 100% rename from lib/Github/Api/Search.php rename to src/Github/Api/Search.php diff --git a/lib/Github/Api/User.php b/src/Github/Api/User.php similarity index 100% rename from lib/Github/Api/User.php rename to src/Github/Api/User.php diff --git a/lib/Github/Client.php b/src/Github/Client.php similarity index 100% rename from lib/Github/Client.php rename to src/Github/Client.php diff --git a/src/Github/Factory/RequestFactory.php b/src/Github/Factory/RequestFactory.php new file mode 100644 index 00000000000..6b09a55a132 --- /dev/null +++ b/src/Github/Factory/RequestFactory.php @@ -0,0 +1,13 @@ + 'https://api.github.com/', + + 'user_agent' => 'php-github-api (http://github.com/KnpLabs/php-github-api)', + + 'api_limit' => 5000, + 'api_version' => 'v3', + + 'cache_dir' => null + ); + + /** + * @param HttpAdapter $adapter + */ + public function __construct( + HttpClient $adapter, + RequestFactory $factory + ) { + $this->adapter = $adapter; + $this->factory = $factory; + } + + /** + * {@inheritdoc} + */ + public function get($path, array $parameters = array(), array $headers = array()) + { + return $this->request( + sprintf( + '%s/%s?%s', + rtrim($this->options['base_url'], '/'), + ltrim($path, '/'), + http_build_query($parameters) + ), + null, + 'GET', + $headers + ); + } + + /** + * {@inheritdoc} + */ + public function post($path, $body = null, array $headers = array()) + { + return $this->request( + sprintf('%s%s', rtrim($this->options['base_url'], '/'), $path), + $body, + 'POST', + $headers + ); + } + + /** + * {@inheritdoc} + */ + public function patch($path, $body = null, array $headers = array()) + { + } + + /** + * {@inheritdoc} + */ + public function put($path, $body, array $headers = array()) + { + } + + /** + * {@inheritdoc} + */ + public function delete($path, $body = null, array $headers = array()) + { + } + + /** + * {@inheritdoc} + */ + public function request($path, $body, $httpMethod = 'GET', array $headers = array()) + { + $headers = array_merge([ + 'Accept' => sprintf('application/vnd.github.%s+json', $this->options['api_version']), + 'User-Agent' => sprintf('%s', $this->options['user_agent']), + ], $headers); + + if (null !== $body) { + $request = $this->factory->createRequest($httpMethod, $path, $headers, $body); + } else { + $request = $this->factory->createRequest($httpMethod, $path, $headers); + } + + // TODO (2016-01-22 14:19 by Gildas): try catch + return $this->adapter->sendRequest($request); + } + + /** + * {@inheritdoc} + */ + public function setOption($name, $value) + { + } + + /** + * {@inheritdoc} + */ + public function setHeaders(array $headers) + { + } + + /** + * {@inheritdoc} + */ + public function authenticate($tokenOrLogin, $password, $authMethod) + { + } +} diff --git a/lib/Github/HttpClient/Message/ResponseMediator.php b/src/Github/HttpClient/Message/ResponseMediator.php similarity index 77% rename from lib/Github/HttpClient/Message/ResponseMediator.php rename to src/Github/HttpClient/Message/ResponseMediator.php index 23982ae3144..96938a2b6a2 100644 --- a/lib/Github/HttpClient/Message/ResponseMediator.php +++ b/src/Github/HttpClient/Message/ResponseMediator.php @@ -2,14 +2,14 @@ namespace Github\HttpClient\Message; -use Guzzle\Http\Message\Response; use Github\Exception\ApiLimitExceedException; +use Psr\Http\Message\ResponseInterface; class ResponseMediator { - public static function getContent(Response $response) + public static function getContent(ResponseInterface $response) { - $body = $response->getBody(true); + $body = $response->getBody(); $content = json_decode($body, true); if (JSON_ERROR_NONE !== json_last_error()) { @@ -19,7 +19,7 @@ public static function getContent(Response $response) return $content; } - public static function getPagination(Response $response) + public static function getPagination(ResponseInterface $response) { $header = (string) $response->getHeader('Link'); @@ -39,14 +39,14 @@ public static function getPagination(Response $response) return $pagination; } - public static function getApiLimit(Response $response) + public static function getApiLimit(ResponseInterface $response) { $remainingCalls = (string) $response->getHeader('X-RateLimit-Remaining'); if (null !== $remainingCalls && 1 > $remainingCalls) { throw new ApiLimitExceedException($remainingCalls); } - + return $remainingCalls; } }