Skip to content

Commit

Permalink
Merge pull request #25 from stevenmaguire/sm-add-api-key-support
Browse files Browse the repository at this point in the history
Add preferred support for api keys
  • Loading branch information
stevenmaguire authored Feb 10, 2018
2 parents 5d8d531 + 910e2c8 commit 9f2587e
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 13 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ matrix:
- php: 5.6
- php: 7.0
- php: 7.1
- php: 7.2
- php: nightly
- php: hhvm-3.6
sudo: required
Expand Down
5 changes: 5 additions & 0 deletions API-GUIDE-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ $client = new \Stevenmaguire\Yelp\v3\Client(array(
));
```

> Prior to December 7, 2017 `accessToken` was required to authenticate requests. Since then, `apiKey` is the preferred authentication method. This library supports both `accessToken` and `apiKey`, prioritizing `apiKey` over `accessToken` if both are provided.
>
> https://www.yelp.com/developers/documentation/v3/authentication#where-is-my-client-secret-going

## Search for businesses

See also [https://www.yelp.com/developers/documentation/v3/business_search](https://www.yelp.com/developers/documentation/v3/business_search)
Expand Down
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
# Changelog
All Notable changes to `yelp-php` will be documented in this file

## 2.1.0 - 2018-02-10

### Added
- Support for API Keys alongside Access Tokens; preference given to API Keys (https://www.yelp.com/developers/documentation/v3/authentication#where-is-my-client-secret-going)

### Deprecated
- Nothing

### Fixed
- Nothing

### Removed
- Nothing

### Security
- Nothing

## 2.0.0 - 2017-06-26

### Added
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ $client = \Stevenmaguire\Yelp\ClientFactory::makeWith(

```php
$options = array(
'accessToken' => 'YOUR ACCESS TOKEN',
'apiHost' => 'api.yelp.com' // Optional, default 'api.yelp.com'
'accessToken' => 'YOUR ACCESS TOKEN', // Required, unless apiKey is provided
'apiHost' => 'api.yelp.com' // Optional, default 'api.yelp.com',
'apiKey' => 'YOUR ACCESS TOKEN', // Required, unless accessToken is provided
);

$client = \Stevenmaguire\Yelp\ClientFactory::makeWith(
Expand All @@ -56,6 +57,10 @@ $client = \Stevenmaguire\Yelp\ClientFactory::makeWith(
);
```

> Prior to December 7, 2017 `accessToken` was required to authenticate requests. Since then, `apiKey` is the preferred authentication method. This library supports both `accessToken` and `apiKey`, prioritizing `apiKey` over `accessToken` if both are provided.
>
> https://www.yelp.com/developers/documentation/v3/authentication#where-is-my-client-secret-going
Version | Constant | Documentation
--------|----------|--------------
v2 | `Stevenmaguire\Yelp\Version::TWO` | [API-GUIDE-v2.md](API-GUIDE-v2.md)
Expand Down
12 changes: 6 additions & 6 deletions src/Contract/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ interface Http
/**
* Creates default http client with appropriate authorization configuration.
*
* @return GuzzleHttp\Client
* @return \GuzzleHttp\Client
*/
public function createDefaultHttpClient();

/**
* Returns the yelp client's http client to the given http client. Client.
*
* @return GuzzleHttp\Client|null
* @return \GuzzleHttp\Client|null
*/
public function getHttpClient();

Expand All @@ -30,7 +30,7 @@ public function getHttpClient();
* @param string|resource|StreamInterface $body Message body.
* @param string $version HTTP protocol version.
*
* @return GuzzleHttp\Psr7\Request
* @return \GuzzleHttp\Psr7\Request
*/
public function getRequest(
$method,
Expand All @@ -46,15 +46,15 @@ public function getRequest(
* WARNING: This method does not attempt to catch exceptions caused by HTTP
* errors! It is recommended to wrap this method in a try/catch block.
*
* @param Psr\Http\Message\RequestInterface $request
* @return Psr\Http\Message\ResponseInterface
* @param \Psr\Http\Message\RequestInterface $request
* @return \Psr\Http\Message\ResponseInterface
*/
public function getResponse(RequestInterface $request);

/**
* Updates the yelp client's http client to the given http client. Client.
*
* @param GuzzleHttp\Client $client
* @param \GuzzleHttp\Client $client
*
* @return mixed
*/
Expand Down
29 changes: 26 additions & 3 deletions src/v3/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class Client implements HttpContract
*/
protected $accessToken;

/**
* Api key
*
* @var string
*/
protected $apiKey;

/**
* Creates new client
*
Expand All @@ -28,7 +35,8 @@ public function __construct(array $options = array())
{
$defaults = [
'accessToken' => null,
'apiHost' => 'api.yelp.com'
'apiHost' => 'api.yelp.com',
'apiKey' => null,
];

$this->parseConfiguration($options, $defaults);
Expand All @@ -41,13 +49,13 @@ public function __construct(array $options = array())
/**
* Creates default http client with appropriate authorization configuration.
*
* @return HttpClient
* @return \GuzzleHttp\Client
*/
public function createDefaultHttpClient()
{
return new HttpClient([
'headers' => [
'Authorization' => 'Bearer ' . $this->accessToken,
'Authorization' => 'Bearer ' . $this->getBearerToken(),
]
]);
}
Expand All @@ -69,6 +77,21 @@ public function getAutocompleteResults($parameters = [])
return $this->processRequest($request);
}

/**
* Returns the api key, if available, otherwise returns access token.
*
* @return string|null
* @link https://www.yelp.com/developers/documentation/v3/authentication#where-is-my-client-secret-going
*/
private function getBearerToken()
{
if ($this->apiKey) {
return $this->apiKey;
}

return $this->accessToken;
}

/**
* Fetches a specific business by id.
*
Expand Down
35 changes: 33 additions & 2 deletions tests/v3/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public function setUp()
{
$this->client = new Yelp([
'accessToken' => 'mock_access_token',
'apiHost' => 'api.yelp.com'
'apiHost' => 'api.yelp.com',
'apiKey' => 'mock_api_key',
]);
}

Expand All @@ -32,12 +33,14 @@ public function testConfigurationMapper()
{
$config = [
'accessToken' => uniqid(),
'apiHost' => uniqid()
'apiHost' => uniqid(),
'apiKey' => uniqid()
];

$client = new Yelp($config);
$this->assertEquals($config['accessToken'], $client->accessToken);
$this->assertEquals($config['apiHost'], $client->apiHost);
$this->assertEquals($config['apiKey'], $client->apiKey);
$this->assertNull($client->{uniqid()});
}

Expand All @@ -48,6 +51,7 @@ public function testClientCanBeConfiguredWithHttpClient()
$client = new Yelp([
'accessToken' => 'mock_access_token',
'apiHost' => 'api.yelp.com',
'apiKey' => 'mock_api_key',
'httpClient' => $httpClient
]);

Expand All @@ -67,6 +71,33 @@ public function testDefaultClientIncludesAccessToken()
);
}

public function testDefaultClientIncludesApiKey()
{
$client = new Yelp([
'apiHost' => 'api.yelp.com',
'apiKey' => 'mock_api_key',
]);

$this->assertContains(
'mock_api_key',
$client->getHttpClient()->getConfig()['headers']['Authorization']
);
}

public function testApiKeyIsPreferredOverAccessToken()
{
$client = new Yelp([
'accessToken' => 'mock_access_token',
'apiHost' => 'api.yelp.com',
'apiKey' => 'mock_api_key',
]);

$this->assertContains(
'mock_api_key',
$client->getHttpClient()->getConfig()['headers']['Authorization']
);
}

public function testGetAutocompleteResults()
{
$path = '/v3/autocomplete';
Expand Down

0 comments on commit 9f2587e

Please sign in to comment.