Skip to content

Commit

Permalink
Switch API endpoint to /v3/shorten (#3)
Browse files Browse the repository at this point in the history
Switch API endpoint to /v3/shorten
  • Loading branch information
trevorgehman authored and Shivella committed Mar 27, 2017
1 parent 3a128ac commit 0b790e5
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Tests/Shivella/Bitly/Client/response.json
@@ -1 +1 @@
{"status_code": 200, "data": {"link_lookup": [{"url": "http://www.nu.nl/", "aggregate_link": "http://bit.ly/1nRtGA"}]}, "status_txt": "OK"}
{"status_code":200,"status_txt":"OK","data":{"url":"http://bit.ly/1nRtGA","hash":"2nk8zqP","global_hash":"SmaYx","long_url":"http://www.laravel.com/","new_hash":1}}
2 changes: 1 addition & 1 deletion Tests/Shivella/Bitly/Client/response_statuscode.json
@@ -1 +1 @@
{"status_code": 503, "data": {"link_lookup": [{"url": "http://www.nu.nl/", "aggregate_link": "http://bit.ly/1nRtGA"}]}, "status_txt": "OK"}
{"status_code":503,"status_txt":"OK","data":{"url":"http://bit.ly/2nk8zqP","hash":"2nk8zqP","global_hash":"SmaYx","long_url":"http://www.laravel.com/","new_hash":1}}
8 changes: 4 additions & 4 deletions src/Shivella/Bitly/Client/BitlyClient.php
Expand Up @@ -52,7 +52,7 @@ public function getUrl($url)
}

try {
$requestUrl = sprintf('https://api-ssl.bitly.com/v3/link/lookup?url=%s&access_token=%s', $url, $this->token);
$requestUrl = sprintf('https://api-ssl.bitly.com/v3/shorten?longUrl=%s&access_token=%s', $url, $this->token);
$response = $this->client->send(new Request('GET', $requestUrl));

if ($response->getStatusCode() === Response::HTTP_FORBIDDEN) {
Expand All @@ -65,15 +65,15 @@ public function getUrl($url)

$data = json_decode($response->getBody()->getContents(), true);

if (false === isset($data['data']['link_lookup'][0]['aggregate_link'])) {
throw new InvalidResponseException('The response does not contain a aggregate link');
if (false === isset($data['data']['url'])) {
throw new InvalidResponseException('The response does not contain a shortened link');
}

if ($data['status_code'] !== Response::HTTP_OK) {
throw new InvalidResponseException('The API does not return a 200 status code');
}

return $data['data']['link_lookup'][0]['aggregate_link'];
return $data['data']['url'];

} catch (\Exception $exception) {
throw new InvalidResponseException($exception->getMessage());
Expand Down

0 comments on commit 0b790e5

Please sign in to comment.