Skip to content

Commit

Permalink
Fix bitly client
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivella committed Aug 13, 2019
1 parent 40bdfa1 commit 1a30486
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/Shivella/Bitly/Client/BitlyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Shivella\Bitly\Exceptions\InvalidResponseException;
use Symfony\Component\HttpFoundation\Response;

use function json_decode;
use function json_encode;

/**
* Class BitlyClient
*/
Expand All @@ -26,21 +29,14 @@ class BitlyClient
/** @var string $token */
private $token;

/** @var array $header */
private $header;

/**
* @param ClientInterface $client
* @param string $token
*/
public function __construct(ClientInterface $client, string $token)
public function __construct(ClientInterface $client, $token)
{
$this->client = $client;
$this->token = $token;
$this->header = [
'Authorization' => 'Bearer '.$token,
'Content-Type' => 'application/json',
];
$this->token = $token;
}

/**
Expand All @@ -51,16 +47,19 @@ public function __construct(ClientInterface $client, string $token)
*
* @return string
*/
public function getUrl(string $url) : string
public function getUrl(string $url): string
{
try {
$requestUrl = 'https://api-ssl.bitly.com/v4/shorten';
$response = $this->client->send(
new Request('POST', $requestUrl, [
'json' => ['long_url' => $url]
]),
['headers' => $this->header]
);

$header = [
'Authorization' => 'Bearer ' . $this->token,
'Content-Type' => 'application/json',
];

$trequest = new Request('POST', $requestUrl, $header, json_encode(['long_url' => $url]));

$response = $this->client->send($trequest);

if ($response->getStatusCode() === Response::HTTP_FORBIDDEN) {
throw new AccessDeniedException('Invalid access token');
Expand Down

0 comments on commit 1a30486

Please sign in to comment.