Skip to content
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

Removing dependency on Guzzlehttp #81

Closed
wants to merge 14 commits into from
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 0.5.0 (31/12/2015)

### Changes

* The dependency to Guzzle is removed. We are now using PHP-HTTP to make us independent from any transport library.
* We are using PSR7 requests and responses
* Introduced HttpFactory to create clients and responses

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

* `OAuth2::setAuthorizationUri`, `OAuth2::setRedirectUri` and `OAuth2::SetTokenCredentialUri` do not support an array as first parameter anymore.

## 0.4.0 (23/04/2015)

### Changes
Expand Down
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ As long as you update the environment variable below to point to *your* JSON
credentials file, the following code should output a list of your Drive files.

```php
use GuzzleHttp\Client;
use Google\Http\HttpFactory;
use Google\Auth\ApplicationDefaultCredentials;

// specify the path to your application credentials
Expand All @@ -93,21 +93,20 @@ putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json');
// define the scopes for your API call
$scopes = ['https://www.googleapis.com/auth/drive.readonly'];

// create the HTTP client
$client = new Client([
'base_url' => 'https://www.googleapis.com',
'defaults' => ['auth' => 'google_auth'] // authorize all requests
]);

// attach this library's auth listener
$fetcher = ApplicationDefaultCredentials::getFetcher($scopes);
$client->getEmitter()->attach($fetcher);

// make the request
$response = $client->get('drive/v2/files');
// create the HTTP client with the auth listener
$client = HttpFactory::getClient($fetcher);

// create a PSR7 request
$request = HttpFactory::getRequest('GET', 'https://www.googleapis.com/drive/v2/files');

// send the request and get a PSR7 response back
$response = $client->sendRequest($request);

// show the result!
print_r($response->json());
print_r(json_decode($resp->getBody(), true));
```

## What about auth in google-apis-php-client?
Expand Down
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
"homepage": "http://github.com/google/google-auth-library-php",
"license": "Apache-2.0",
"require": {
"php": ">=5.4",
"firebase/php-jwt": "~2.0|~3.0",
"guzzlehttp/guzzle": "5.2.*",
"php": ">=5.4"
"php-http/client-implementation": "@dev",
"php-http/discovery": "dev-master",
"php-http/plugins":"dev-master",
"php-http/utils":"dev-master"
},
"require-dev": {
"phpunit/phpunit": "3.7.*",
"php-http/guzzle6-adapter": "dev-master",
"php-http/client-tools": "dev-master",
"php-http/httplug": "dev-master",
"phplint/phplint": "0.0.1"
},
"autoload": {
Expand Down
8 changes: 3 additions & 5 deletions src/AppIdentityCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@

namespace Google\Auth;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Client;

/**
* The AppIdentityService class is automatically defined on App Engine,
* so including this dependency is not necessary, and will result in a
* PHP fatal error in the App Engine environment.
*/
use google\appengine\api\app_identity\AppIdentityService;
use Http\Client\HttpClient;

/**
* AppIdentityCredentials supports authorization on Google App Engine.
Expand Down Expand Up @@ -80,7 +78,7 @@ public static function onAppEngine()
* As the AppIdentityService uses protobufs to fetch the access token,
* the GuzzleHttp\ClientInterface instance passed in will not be used.
*
* @param $client GuzzleHttp\ClientInterface optional client.
* @param $client HttpClient optional client.
* @return array the auth metadata:
* array(2) {
* ["access_token"]=>
Expand All @@ -89,7 +87,7 @@ public static function onAppEngine()
* string(10) "1444339905"
* }
*/
public function fetchAuthToken(ClientInterface $unusedClient = null)
public function fetchAuthToken(HttpClient $httpClient = null)
{
if (!self::onAppEngine()) {
return array();
Expand Down
16 changes: 8 additions & 8 deletions src/ApplicationDefaultCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

namespace Google\Auth;

use GuzzleHttp\Stream\Stream;
use GuzzleHttp\ClientInterface;
use Google\Auth\Http\Plugin\AuthTokenFetcher;
use Http\Client\HttpClient;

/**
* ApplicationDefaultCredentials obtains the default credentials for
Expand Down Expand Up @@ -60,15 +60,15 @@ class ApplicationDefaultCredentials
*
* @param string|array scope the scope of the access request, expressed
* either as an Array or as a space-delimited String.
* @param $client GuzzleHttp\ClientInterface optional client.
* @param $client HttpClient optional client.
* @param cacheConfig configuration for the cache when it's present
* @param object $cache an implementation of CacheInterface
*
* @throws DomainException if no implementation can be obtained.
* @throws \DomainException if no implementation can be obtained.
*/
public static function getFetcher(
$scope = null,
ClientInterface $client = null,
HttpClient $client = null,
array $cacheConfig = null,
CacheInterface $cache = null)
{
Expand All @@ -86,10 +86,10 @@ public static function getFetcher(
* @param string|array scope the scope of the access request, expressed
* either as an Array or as a space-delimited String.
*
* @param $client GuzzleHttp\ClientInterface optional client.
* @throws DomainException if no implementation can be obtained.
* @param $client HttpClient optional client.
* @throws \DomainException if no implementation can be obtained.
*/
public static function getCredentials($scope = null, $client = null)
public static function getCredentials($scope = null, HttpClient $client = null)
{
$creds = CredentialsLoader::fromEnv($scope);
if (!is_null($creds)) {
Expand Down
162 changes: 0 additions & 162 deletions src/AuthTokenFetcher.php

This file was deleted.

20 changes: 9 additions & 11 deletions src/CredentialsLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@

namespace Google\Auth;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Stream\Stream;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use Google\Auth\Http\HttpFactory;
use Http\Client\HttpClient;

/**
* CredentialsLoader contains the behaviour used to locate and find default
Expand Down Expand Up @@ -75,7 +72,7 @@ public static function fromEnv($scope = null)
$cause = "file " . $path . " does not exist";
throw new \DomainException(self::unableToReadEnv($cause));
}
$keyStream = Stream::factory(file_get_contents($path));
$keyStream = HttpFactory::getStream(file_get_contents($path));
return static::makeCredentials($scope, $keyStream);
}

Expand Down Expand Up @@ -105,7 +102,7 @@ public static function fromWellKnownFile($scope = null)
if (!file_exists($path)) {
return null;
}
$keyStream = Stream::factory(file_get_contents($path));
$keyStream = HttpFactory::getStream(file_get_contents($path));
return static::makeCredentials($scope, $keyStream);
}

Expand Down Expand Up @@ -157,10 +154,11 @@ public function getUpdateMetadataFunc()
*
* @return array updated metadata hashmap
*/
public function updateMetadata($metadata,
$authUri = null,
ClientInterface $client = null)
{
public function updateMetadata(
$metadata,
$authUri = null,
HttpClient $client = null
) {
$result = $this->fetchAuthToken($client);
if (!isset($result['access_token'])) {
return $metadata;
Expand Down
6 changes: 3 additions & 3 deletions src/FetchAuthTokenInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Google\Auth;

use GuzzleHttp\ClientInterface;
use Http\Client\HttpClient;

/**
* An interface implemented by objects that can fetch auth tokens.
Expand All @@ -28,10 +28,10 @@ interface FetchAuthTokenInterface
/**
* Fetchs the auth tokens based on the current state.
*
* @param $client GuzzleHttp\ClientInterface the optional client.
* @param HttpClient $client the optional client.
* @return array a hash of auth tokens
*/
public function fetchAuthToken(ClientInterface $client = null);
public function fetchAuthToken(HttpClient $client = null);


/**
Expand Down
Loading