Skip to content

Commit

Permalink
Merge pull request #2 from Weebly/support-oauth
Browse files Browse the repository at this point in the history
Add public getRequestHeaders method. Refactor usage.
  • Loading branch information
wmichelin authored Feb 28, 2018
2 parents 90565e7 + 15b7a72 commit 42efaa6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
44 changes: 28 additions & 16 deletions lib/Shippo/ApiRequestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,31 +106,30 @@ public function handleApiError($rbody, $rcode, $resp)
throw new Shippo_ApiError($msg, $rcode, $rbody, $resp);
}
}
private function _requestRaw($method, $url, $params)

public function getRequestHeaders()
{
$myApiKey = $this->_apiKey;
if (!$myApiKey)
$myApiKey = Shippo::$apiKey;

if (!$myApiKey) {
$msg = 'No credentials provided.';
throw new Shippo_AuthenticationError($msg);
}

$absUrl = $this->apiUrl($url);
$params = self::_encodeObjects($params);
$langVersion = phpversion();
$uname = php_uname();
$apiKey = $this->_getApiKey();

$headers = array(
'Content-Type: application/json',
'Authorization: ' . $this->_getAuthorizationType($myApiKey) . ' ' . $myApiKey,
'Authorization: ' . $this->_getAuthorizationType($apiKey) . ' ' . $apiKey,
'Accept: application/json',
'User-Agent: Shippo/v1 PHPBindings/' . Shippo::VERSION
);
if (Shippo::getApiVersion()){
$headers[] = 'Shippo-API-Version: ' . Shippo::getApiVersion();
}

return $headers;
}

private function _requestRaw($method, $url, $params)
{
$absUrl = $this->apiUrl($url);
$params = self::_encodeObjects($params);
$myApiKey = $this->_getApiKey();
$headers = $this->getRequestHeaders();

list($rbody, $rcode) = $this->httpClient()->request($method, $absUrl, $headers, $params);
return array(
Expand All @@ -156,6 +155,19 @@ private function _interpretResponse($rbody, $rcode)
return $resp;
}

private function _getApiKey()
{
$apiKey = $this->_apiKey;
if (!$apiKey)
$apiKey = Shippo::$apiKey;

if (!$apiKey) {
throw new Shippo_AuthenticationError('No credentials provided.');
}

return $apiKey;
}

private function _getAuthorizationType($apiKey = '')
{
return strpos($apiKey, 'oauth.') === 0 ? 'Bearer' : 'ShippoToken';
Expand Down
17 changes: 6 additions & 11 deletions test/ApiRequestorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,16 @@ public function testEncodeObjects()
*
* @param $expectedAuthorizationType
* @param $apiToken
* @throws ReflectionException
*/
public function testGetAuthorizationType($expectedAuthorizationType, $apiToken)
{
// We have to do some work here because this is normally
// private. This is just for testing! Also it only works on PHP >=
// 5.3
if (version_compare(PHP_VERSION, '5.3.2', '>=')) {
$reflector = new ReflectionClass('Shippo_APIRequestor');
$method = $reflector->getMethod('_getAuthorizationType');
$method->setAccessible(true);
$apiRequestor = new Shippo_ApiRequestor($apiToken);
$headers = $apiRequestor->getRequestHeaders();
$authorizationHeader = current(array_filter($headers, function ($header) {
return strpos($header, 'Authorization:') === 0;
}));

$apiRequestor = new Shippo_ApiRequestor($apiToken);
$this->assertEquals($expectedAuthorizationType, $method->invoke($apiRequestor, $apiToken));
}
$this->assertEquals(strpos($authorizationHeader, 'Authorization: ' . $expectedAuthorizationType), 0);
}

public function provideValidAPITokens()
Expand Down

0 comments on commit 42efaa6

Please sign in to comment.