Skip to content

Commit 9f2587e

Browse files
Merge pull request #25 from stevenmaguire/sm-add-api-key-support
Add preferred support for api keys
2 parents 5d8d531 + 910e2c8 commit 9f2587e

File tree

7 files changed

+95
-13
lines changed

7 files changed

+95
-13
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ matrix:
55
- php: 5.6
66
- php: 7.0
77
- php: 7.1
8+
- php: 7.2
89
- php: nightly
910
- php: hhvm-3.6
1011
sudo: required

API-GUIDE-v3.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ $client = new \Stevenmaguire\Yelp\v3\Client(array(
1919
));
2020
```
2121

22+
> 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.
23+
>
24+
> https://www.yelp.com/developers/documentation/v3/authentication#where-is-my-client-secret-going
25+
26+
2227
## Search for businesses
2328

2429
See also [https://www.yelp.com/developers/documentation/v3/business_search](https://www.yelp.com/developers/documentation/v3/business_search)

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
# Changelog
22
All Notable changes to `yelp-php` will be documented in this file
33

4+
## 2.1.0 - 2018-02-10
5+
6+
### Added
7+
- 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)
8+
9+
### Deprecated
10+
- Nothing
11+
12+
### Fixed
13+
- Nothing
14+
15+
### Removed
16+
- Nothing
17+
18+
### Security
19+
- Nothing
20+
421
## 2.0.0 - 2017-06-26
522

623
### Added

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ $client = \Stevenmaguire\Yelp\ClientFactory::makeWith(
4646

4747
```php
4848
$options = array(
49-
'accessToken' => 'YOUR ACCESS TOKEN',
50-
'apiHost' => 'api.yelp.com' // Optional, default 'api.yelp.com'
49+
'accessToken' => 'YOUR ACCESS TOKEN', // Required, unless apiKey is provided
50+
'apiHost' => 'api.yelp.com' // Optional, default 'api.yelp.com',
51+
'apiKey' => 'YOUR ACCESS TOKEN', // Required, unless accessToken is provided
5152
);
5253

5354
$client = \Stevenmaguire\Yelp\ClientFactory::makeWith(
@@ -56,6 +57,10 @@ $client = \Stevenmaguire\Yelp\ClientFactory::makeWith(
5657
);
5758
```
5859

60+
> 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.
61+
>
62+
> https://www.yelp.com/developers/documentation/v3/authentication#where-is-my-client-secret-going
63+
5964
Version | Constant | Documentation
6065
--------|----------|--------------
6166
v2 | `Stevenmaguire\Yelp\Version::TWO` | [API-GUIDE-v2.md](API-GUIDE-v2.md)

src/Contract/Http.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ interface Http
1010
/**
1111
* Creates default http client with appropriate authorization configuration.
1212
*
13-
* @return GuzzleHttp\Client
13+
* @return \GuzzleHttp\Client
1414
*/
1515
public function createDefaultHttpClient();
1616

1717
/**
1818
* Returns the yelp client's http client to the given http client. Client.
1919
*
20-
* @return GuzzleHttp\Client|null
20+
* @return \GuzzleHttp\Client|null
2121
*/
2222
public function getHttpClient();
2323

@@ -30,7 +30,7 @@ public function getHttpClient();
3030
* @param string|resource|StreamInterface $body Message body.
3131
* @param string $version HTTP protocol version.
3232
*
33-
* @return GuzzleHttp\Psr7\Request
33+
* @return \GuzzleHttp\Psr7\Request
3434
*/
3535
public function getRequest(
3636
$method,
@@ -46,15 +46,15 @@ public function getRequest(
4646
* WARNING: This method does not attempt to catch exceptions caused by HTTP
4747
* errors! It is recommended to wrap this method in a try/catch block.
4848
*
49-
* @param Psr\Http\Message\RequestInterface $request
50-
* @return Psr\Http\Message\ResponseInterface
49+
* @param \Psr\Http\Message\RequestInterface $request
50+
* @return \Psr\Http\Message\ResponseInterface
5151
*/
5252
public function getResponse(RequestInterface $request);
5353

5454
/**
5555
* Updates the yelp client's http client to the given http client. Client.
5656
*
57-
* @param GuzzleHttp\Client $client
57+
* @param \GuzzleHttp\Client $client
5858
*
5959
* @return mixed
6060
*/

src/v3/Client.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ class Client implements HttpContract
1919
*/
2020
protected $accessToken;
2121

22+
/**
23+
* Api key
24+
*
25+
* @var string
26+
*/
27+
protected $apiKey;
28+
2229
/**
2330
* Creates new client
2431
*
@@ -28,7 +35,8 @@ public function __construct(array $options = array())
2835
{
2936
$defaults = [
3037
'accessToken' => null,
31-
'apiHost' => 'api.yelp.com'
38+
'apiHost' => 'api.yelp.com',
39+
'apiKey' => null,
3240
];
3341

3442
$this->parseConfiguration($options, $defaults);
@@ -41,13 +49,13 @@ public function __construct(array $options = array())
4149
/**
4250
* Creates default http client with appropriate authorization configuration.
4351
*
44-
* @return HttpClient
52+
* @return \GuzzleHttp\Client
4553
*/
4654
public function createDefaultHttpClient()
4755
{
4856
return new HttpClient([
4957
'headers' => [
50-
'Authorization' => 'Bearer ' . $this->accessToken,
58+
'Authorization' => 'Bearer ' . $this->getBearerToken(),
5159
]
5260
]);
5361
}
@@ -69,6 +77,21 @@ public function getAutocompleteResults($parameters = [])
6977
return $this->processRequest($request);
7078
}
7179

80+
/**
81+
* Returns the api key, if available, otherwise returns access token.
82+
*
83+
* @return string|null
84+
* @link https://www.yelp.com/developers/documentation/v3/authentication#where-is-my-client-secret-going
85+
*/
86+
private function getBearerToken()
87+
{
88+
if ($this->apiKey) {
89+
return $this->apiKey;
90+
}
91+
92+
return $this->accessToken;
93+
}
94+
7295
/**
7396
* Fetches a specific business by id.
7497
*

tests/v3/ClientTest.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public function setUp()
1919
{
2020
$this->client = new Yelp([
2121
'accessToken' => 'mock_access_token',
22-
'apiHost' => 'api.yelp.com'
22+
'apiHost' => 'api.yelp.com',
23+
'apiKey' => 'mock_api_key',
2324
]);
2425
}
2526

@@ -32,12 +33,14 @@ public function testConfigurationMapper()
3233
{
3334
$config = [
3435
'accessToken' => uniqid(),
35-
'apiHost' => uniqid()
36+
'apiHost' => uniqid(),
37+
'apiKey' => uniqid()
3638
];
3739

3840
$client = new Yelp($config);
3941
$this->assertEquals($config['accessToken'], $client->accessToken);
4042
$this->assertEquals($config['apiHost'], $client->apiHost);
43+
$this->assertEquals($config['apiKey'], $client->apiKey);
4144
$this->assertNull($client->{uniqid()});
4245
}
4346

@@ -48,6 +51,7 @@ public function testClientCanBeConfiguredWithHttpClient()
4851
$client = new Yelp([
4952
'accessToken' => 'mock_access_token',
5053
'apiHost' => 'api.yelp.com',
54+
'apiKey' => 'mock_api_key',
5155
'httpClient' => $httpClient
5256
]);
5357

@@ -67,6 +71,33 @@ public function testDefaultClientIncludesAccessToken()
6771
);
6872
}
6973

74+
public function testDefaultClientIncludesApiKey()
75+
{
76+
$client = new Yelp([
77+
'apiHost' => 'api.yelp.com',
78+
'apiKey' => 'mock_api_key',
79+
]);
80+
81+
$this->assertContains(
82+
'mock_api_key',
83+
$client->getHttpClient()->getConfig()['headers']['Authorization']
84+
);
85+
}
86+
87+
public function testApiKeyIsPreferredOverAccessToken()
88+
{
89+
$client = new Yelp([
90+
'accessToken' => 'mock_access_token',
91+
'apiHost' => 'api.yelp.com',
92+
'apiKey' => 'mock_api_key',
93+
]);
94+
95+
$this->assertContains(
96+
'mock_api_key',
97+
$client->getHttpClient()->getConfig()['headers']['Authorization']
98+
);
99+
}
100+
70101
public function testGetAutocompleteResults()
71102
{
72103
$path = '/v3/autocomplete';

0 commit comments

Comments
 (0)