Skip to content

Commit ee93c73

Browse files
committed
Pass apikey in header instead of querystring
1 parent 410243f commit ee93c73

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

spec/ClientSpec.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function it_sends_an_API_key_with_each_request()
6868
$this->getJSON(str_random());
6969

7070
// Query string should include apikey
71-
expect($http->getRequests()[0])->getUri()->getQuery()->toContain('apikey=DummyApiKey');
71+
expect($http->getRequests()[0])->getHeaderLine('authorization')->toBe('apikey DummyApiKey');
7272
}
7373

7474
public function it_can_request_and_parse_JSON()

src/Client.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,6 @@ public function buildUrl($url, $query = [])
275275
parse_str($url[1], $query0);
276276
$query = array_merge($query0, $query);
277277
}
278-
$query['apikey'] = $this->key;
279278

280279
$url = $url[0];
281280

@@ -300,8 +299,8 @@ public function buildUrl($url, $query = [])
300299
*/
301300
public function request(RequestInterface $request, $attempt = 1)
302301
{
303-
if (!$this->key) {
304-
throw new AlmaClientException('No API key defined for ' . $this->zone);
302+
if (isset($this->key)) {
303+
$request = $request->withHeader('Authorization', 'apikey ' . $this->key);
305304
}
306305
foreach ($this->extraHeaders as $key => $val) {
307306
$request = $request->withHeader($key, $val);
@@ -593,6 +592,10 @@ protected function parseClientError(HttpException $exception)
593592
// so we generalize it as a string.
594593
$code = empty($code) ? null : (string) $code;
595594

595+
if ($code == 'UNAUTHORIZED') {
596+
return new InvalidApiKey($message, null, $exception);
597+
}
598+
596599
if (strtolower($message) == 'invalid api key') {
597600
return new InvalidApiKey($message, null, $exception);
598601
}

0 commit comments

Comments
 (0)