Skip to content

Commit 42efaa6

Browse files
authored
Merge pull request #2 from Weebly/support-oauth
Add public getRequestHeaders method. Refactor usage.
2 parents 90565e7 + 15b7a72 commit 42efaa6

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

lib/Shippo/ApiRequestor.php

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,31 +106,30 @@ public function handleApiError($rbody, $rcode, $resp)
106106
throw new Shippo_ApiError($msg, $rcode, $rbody, $resp);
107107
}
108108
}
109-
110-
private function _requestRaw($method, $url, $params)
109+
110+
public function getRequestHeaders()
111111
{
112-
$myApiKey = $this->_apiKey;
113-
if (!$myApiKey)
114-
$myApiKey = Shippo::$apiKey;
115-
116-
if (!$myApiKey) {
117-
$msg = 'No credentials provided.';
118-
throw new Shippo_AuthenticationError($msg);
119-
}
120-
121-
$absUrl = $this->apiUrl($url);
122-
$params = self::_encodeObjects($params);
123-
$langVersion = phpversion();
124-
$uname = php_uname();
112+
$apiKey = $this->_getApiKey();
113+
125114
$headers = array(
126115
'Content-Type: application/json',
127-
'Authorization: ' . $this->_getAuthorizationType($myApiKey) . ' ' . $myApiKey,
116+
'Authorization: ' . $this->_getAuthorizationType($apiKey) . ' ' . $apiKey,
128117
'Accept: application/json',
129118
'User-Agent: Shippo/v1 PHPBindings/' . Shippo::VERSION
130119
);
131120
if (Shippo::getApiVersion()){
132121
$headers[] = 'Shippo-API-Version: ' . Shippo::getApiVersion();
133122
}
123+
124+
return $headers;
125+
}
126+
127+
private function _requestRaw($method, $url, $params)
128+
{
129+
$absUrl = $this->apiUrl($url);
130+
$params = self::_encodeObjects($params);
131+
$myApiKey = $this->_getApiKey();
132+
$headers = $this->getRequestHeaders();
134133

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

158+
private function _getApiKey()
159+
{
160+
$apiKey = $this->_apiKey;
161+
if (!$apiKey)
162+
$apiKey = Shippo::$apiKey;
163+
164+
if (!$apiKey) {
165+
throw new Shippo_AuthenticationError('No credentials provided.');
166+
}
167+
168+
return $apiKey;
169+
}
170+
159171
private function _getAuthorizationType($apiKey = '')
160172
{
161173
return strpos($apiKey, 'oauth.') === 0 ? 'Bearer' : 'ShippoToken';

test/ApiRequestorTest.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,16 @@ public function testEncodeObjects()
4343
*
4444
* @param $expectedAuthorizationType
4545
* @param $apiToken
46-
* @throws ReflectionException
4746
*/
4847
public function testGetAuthorizationType($expectedAuthorizationType, $apiToken)
4948
{
50-
// We have to do some work here because this is normally
51-
// private. This is just for testing! Also it only works on PHP >=
52-
// 5.3
53-
if (version_compare(PHP_VERSION, '5.3.2', '>=')) {
54-
$reflector = new ReflectionClass('Shippo_APIRequestor');
55-
$method = $reflector->getMethod('_getAuthorizationType');
56-
$method->setAccessible(true);
49+
$apiRequestor = new Shippo_ApiRequestor($apiToken);
50+
$headers = $apiRequestor->getRequestHeaders();
51+
$authorizationHeader = current(array_filter($headers, function ($header) {
52+
return strpos($header, 'Authorization:') === 0;
53+
}));
5754

58-
$apiRequestor = new Shippo_ApiRequestor($apiToken);
59-
$this->assertEquals($expectedAuthorizationType, $method->invoke($apiRequestor, $apiToken));
60-
}
55+
$this->assertEquals(strpos($authorizationHeader, 'Authorization: ' . $expectedAuthorizationType), 0);
6156
}
6257

6358
public function provideValidAPITokens()

0 commit comments

Comments
 (0)