Skip to content

Switch to PSR18 client implementation and bump httplug minimum version to ^2.0 #885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ matrix:
- php: 7.2
name: Backward compatibillity check
env: DEPENDENCIES="roave/backward-compatibility-check" TEST_COMMAND="./.github/bc-test.sh"
- php: 7.1
- php: 7.4
name: phpstan
script:
- vendor/bin/phpstan analyse --no-progress
Expand Down
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,34 @@ Uses [GitHub API v3](http://developer.github.com/v3/) & supports [GitHub API v4]
## Requirements

* PHP >= 7.1
* A [HTTP client](https://packagist.org/providers/php-http/client-implementation)
* A [HTTP client](https://packagist.org/providers/psr/http-client-implementation)
* A [PSR-7 implementation](https://packagist.org/providers/psr/http-message-implementation)
* (optional) PHPUnit to run tests.

## Install

Via Composer:
Via [Composer](https://getcomposer.org).

### PHP 7.2+:

```bash
$ composer require knplabs/github-api php-http/guzzle6-adapter "^1.1"
composer require knplabs/github-api guzzlehttp/guzzle:^7.0.1
```

Why `php-http/guzzle6-adapter`? We are decoupled from any HTTP messaging client with help by [HTTPlug](http://httplug.io/). Read about clients in our [docs](doc/customize.md).
### PHP 7.1+:

```bash
composer require knplabs/github-api php-http/guzzle6-adapter:^2.0.1
```

### Laravel 5.5+:

```bash
composer require graham-campbell/github guzzlehttp/guzzle:^7.0.1
```

## Using Laravel?
We are decoupled from any HTTP messaging client with help by [HTTPlug](http://httplug.io). Read about clients in our [docs](doc/customize.md). [graham-campbell/github](https://github.com/GrahamCampbell/Laravel-GitHub) is by [Graham Campbell](https://github.com/GrahamCampbell).

[Laravel GitHub](https://github.com/GrahamCampbell/Laravel-GitHub) by [Graham Campbell](https://github.com/GrahamCampbell) might interest you.

## Basic usage of `php-github-api` client

Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
],
"require": {
"php": "^7.1",
"psr/http-message": "^1.0",
"php-http/client-common": "^2.1",
"php-http/cache-plugin": "^1.7",
"php-http/discovery": "^1.7",
"php-http/httplug": "^2.1",
"psr/cache": "^1.0",
"php-http/httplug": "^1.1 || ^2.0",
"php-http/discovery": "^1.0",
"php-http/client-implementation": "^1.0",
"php-http/client-common": "^1.6 || ^2.0",
"php-http/cache-plugin": "^1.4"
"psr/http-client-implementation": "^1.0",
"psr/http-message": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0 || ^8.0",
"php-http/guzzle6-adapter": "^1.0 || ^2.0",
"php-http/guzzle6-adapter": "^2.0",
"php-http/mock-client": "^1.2",
"guzzlehttp/psr7": "^1.2",
"cache/array-adapter": "^0.4",
Expand Down
2 changes: 1 addition & 1 deletion lib/Github/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ protected function delete($path, array $parameters = [], array $requestHeaders =
*
* @param array $parameters Request parameters
*
* @return null|string
* @return string|null
*/
protected function createJsonBody(array $parameters)
{
Expand Down
2 changes: 1 addition & 1 deletion lib/Github/Api/Repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function showById($id)
* @param string $description repository description
* @param string $homepage homepage url
* @param bool $public `true` for public, `false` for private
* @param null|string $organization username of organization if applicable
* @param string|null $organization username of organization if applicable
* @param bool $hasIssues `true` to enable issues for this repository, `false` to disable them
* @param bool $hasWiki `true` to enable the wiki for this repository, `false` to disable it
* @param bool $hasDownloads `true` to enable downloads for this repository, `false` to disable them
Expand Down
20 changes: 10 additions & 10 deletions lib/Github/Api/Repository/Contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function configure($bodyType = null)
*
* @param string $username the user who owns the repository
* @param string $repository the name of the repository
* @param null|string $reference reference to a branch or commit
* @param string|null $reference reference to a branch or commit
*
* @return array information for README file
*/
Expand All @@ -63,8 +63,8 @@ public function readme($username, $repository, $reference = null)
*
* @param string $username the user who owns the repository
* @param string $repository the name of the repository
* @param null|string $path path to file or directory
* @param null|string $reference reference to a branch or commit
* @param string|null $path path to file or directory
* @param string|null $reference reference to a branch or commit
*
* @return array|string information for file | information for each item in directory
*/
Expand All @@ -90,7 +90,7 @@ public function show($username, $repository, $path = null, $reference = null)
* @param string $path path to file
* @param string $content contents of the new file
* @param string $message the commit message
* @param null|string $branch name of a branch
* @param string|null $branch name of a branch
* @param null|array $committer information about the committer
*
* @throws MissingArgumentException
Expand Down Expand Up @@ -126,7 +126,7 @@ public function create($username, $repository, $path, $content, $message, $branc
* @param string $username the user who owns the repository
* @param string $repository the name of the repository
* @param string $path path of file to check
* @param null|string $reference reference to a branch or commit
* @param string|null $reference reference to a branch or commit
*
* @return bool
*/
Expand Down Expand Up @@ -166,7 +166,7 @@ public function exists($username, $repository, $path, $reference = null)
* @param string $content contents of the new file
* @param string $message the commit message
* @param string $sha blob SHA of the file being replaced
* @param null|string $branch name of a branch
* @param string|null $branch name of a branch
* @param null|array $committer information about the committer
*
* @throws MissingArgumentException
Expand Down Expand Up @@ -207,7 +207,7 @@ public function update($username, $repository, $path, $content, $message, $sha,
* @param string $path path to file
* @param string $message the commit message
* @param string $sha blob SHA of the file being deleted
* @param null|string $branch name of a branch
* @param string|null $branch name of a branch
* @param null|array $committer information about the committer
*
* @throws MissingArgumentException
Expand Down Expand Up @@ -245,7 +245,7 @@ public function rm($username, $repository, $path, $message, $sha, $branch = null
* @param string $username the user who owns the repository
* @param string $repository the name of the repository
* @param string $format format of archive: tarball or zipball
* @param null|string $reference reference to a branch or commit
* @param string|null $reference reference to a branch or commit
*
* @return string repository archive binary data
*/
Expand All @@ -265,12 +265,12 @@ public function archive($username, $repository, $format, $reference = null)
* @param string $username the user who owns the repository
* @param string $repository the name of the repository
* @param string $path path to file
* @param null|string $reference reference to a branch or commit
* @param string|null $reference reference to a branch or commit
*
* @throws InvalidArgumentException If $path is not a file or if its encoding is different from base64
* @throws ErrorException If $path doesn't include a 'content' index
*
* @return null|string content of file, or null in case of base64_decode failure
* @return string|null content of file, or null in case of base64_decode failure
*/
public function download($username, $repository, $path, $reference = null)
{
Expand Down
12 changes: 6 additions & 6 deletions lib/Github/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
use Github\HttpClient\Plugin\PathPrepend;
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Common\Plugin;
use Http\Client\HttpClient;
use Http\Discovery\UriFactoryDiscovery;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Client\ClientInterface;

/**
* Simple yet very cool PHP GitHub client.
Expand Down Expand Up @@ -161,13 +161,13 @@ public function __construct(Builder $httpClientBuilder = null, $apiVersion = nul
}

/**
* Create a Github\Client using a HttpClient.
* Create a Github\Client using a HTTP client.
*
* @param HttpClient $httpClient
* @param ClientInterface $httpClient
*
* @return Client
*/
public static function createWithHttpClient(HttpClient $httpClient)
public static function createWithHttpClient(ClientInterface $httpClient)
{
$builder = new Builder($httpClient);

Expand Down Expand Up @@ -329,8 +329,8 @@ public function api($name)
* Authenticate a user for all next requests.
*
* @param string $tokenOrLogin GitHub private token/username/client ID
* @param null|string $password GitHub password/secret (optionally can contain $authMethod)
* @param null|string $authMethod One of the AUTH_* class constants
* @param string|null $password GitHub password/secret (optionally can contain $authMethod)
* @param string|null $authMethod One of the AUTH_* class constants
*
* @throws InvalidArgumentException If no authentication method was given
*/
Expand Down
16 changes: 8 additions & 8 deletions lib/Github/HttpClient/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
use Http\Client\Common\Plugin;
use Http\Client\Common\Plugin\Cache\Generator\HeaderCacheKeyGenerator;
use Http\Client\Common\PluginClientFactory;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Http\Discovery\StreamFactoryDiscovery;
use Http\Message\RequestFactory;
use Http\Message\StreamFactory;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Client\ClientInterface;

/**
* A builder that builds the API client.
Expand All @@ -25,7 +25,7 @@ class Builder
/**
* The object that sends HTTP messages.
*
* @var HttpClient
* @var ClientInterface
*/
private $httpClient;

Expand Down Expand Up @@ -73,16 +73,16 @@ class Builder
private $headers = [];

/**
* @param HttpClient $httpClient
* @param RequestFactory $requestFactory
* @param StreamFactory $streamFactory
* @param ClientInterface $httpClient
* @param RequestFactory $requestFactory
* @param StreamFactory $streamFactory
*/
public function __construct(
HttpClient $httpClient = null,
ClientInterface $httpClient = null,
RequestFactory $requestFactory = null,
StreamFactory $streamFactory = null
) {
$this->httpClient = $httpClient ?: HttpClientDiscovery::find();
$this->httpClient = $httpClient ?: Psr18ClientDiscovery::find();
$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
$this->streamFactory = $streamFactory ?: StreamFactoryDiscovery::find();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Github/HttpClient/Message/ResponseMediator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static function getPagination(ResponseInterface $response)
/**
* @param ResponseInterface $response
*
* @return null|string
* @return string|null
*/
public static function getApiLimit(ResponseInterface $response)
{
Expand Down
21 changes: 18 additions & 3 deletions lib/Github/HttpClient/Plugin/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Github\Client;
use Github\Exception\RuntimeException;
use Http\Client\Common\Plugin;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;

/**
Expand All @@ -14,12 +15,26 @@
*/
class Authentication implements Plugin
{
use Plugin\VersionBridgePlugin;

/**
* @var string
*/
private $tokenOrLogin;

/**
* @var string|null
*/
private $password;

/**
* @var string|null
*/
private $method;

/**
* @param string $tokenOrLogin GitHub private token/username/client ID
* @param string|null $password GitHub password/secret (optionally can contain $method)
* @param string|null $method One of the AUTH_* class constants
*/
public function __construct($tokenOrLogin, $password, $method)
{
$this->tokenOrLogin = $tokenOrLogin;
Expand All @@ -30,7 +45,7 @@ public function __construct($tokenOrLogin, $password, $method)
/**
* {@inheritdoc}
*/
public function doHandleRequest(RequestInterface $request, callable $next, callable $first)
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
{
switch ($this->method) {
case Client::AUTH_HTTP_PASSWORD:
Expand Down
5 changes: 2 additions & 3 deletions lib/Github/HttpClient/Plugin/GithubExceptionThrower.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Github\Exception\ValidationFailedException;
use Github\HttpClient\Message\ResponseMediator;
use Http\Client\Common\Plugin;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

Expand All @@ -18,12 +19,10 @@
*/
class GithubExceptionThrower implements Plugin
{
use Plugin\VersionBridgePlugin;

/**
* {@inheritdoc}
*/
public function doHandleRequest(RequestInterface $request, callable $next, callable $first)
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
{
return $next($request)->then(function (ResponseInterface $response) use ($request) {
if ($response->getStatusCode() < 400 || $response->getStatusCode() > 600) {
Expand Down
9 changes: 6 additions & 3 deletions lib/Github/HttpClient/Plugin/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Github\HttpClient\Plugin;

use Http\Client\Common\Plugin\Journal;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

Expand All @@ -13,10 +14,8 @@
*/
class History implements Journal
{
use HistoryTrait;

/**
* @var ResponseInterface
* @var ResponseInterface|null
*/
private $lastResponse;

Expand All @@ -32,4 +31,8 @@ public function addSuccess(RequestInterface $request, ResponseInterface $respons
{
$this->lastResponse = $response;
}

public function addFailure(RequestInterface $request, ClientExceptionInterface $exception)
{
}
}
32 changes: 0 additions & 32 deletions lib/Github/HttpClient/Plugin/HistoryTrait.php

This file was deleted.

Loading