Skip to content

Commit

Permalink
Use array parameters for GET url (#4)
Browse files Browse the repository at this point in the history
* Pass query parameters as array

* fix specs
  • Loading branch information
gpenverne authored May 18, 2017
1 parent dcbf24e commit 2c3523b
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public function let(
$tokenStd->access_token = 'a-token';

$event->getCode()->willReturn('a-code');
$urlGenerator->getTokenUrl('a-code')->willReturn('an-url');
$httpClient->getJson('an-url')->willReturn($tokenStd);
$urlGenerator->getTokenUrl()->willReturn('an-url');
$urlGenerator->getTokenUrlParameters('a-code')->willReturn(['some-parameters']);
$httpClient->getJson('an-url', ['some-parameters'])->willReturn($tokenStd);

$this->beConstructedWith($dispatcher, $urlGenerator, $httpClient);
}
Expand All @@ -44,7 +45,7 @@ public function it_retrieves_a_token($event)

public function it_throws_a_no_token_exception_if_no_token_found($tokenStd, $event, $httpClient)
{
$httpClient->getJson('an-url')->willReturn('something wrong');
$httpClient->getJson('an-url', ['some-parameters'])->willReturn('something wrong');
$this->shouldThrow(NoTokenFoundException::class)->duringGetToken('a-code');
}
}
4 changes: 2 additions & 2 deletions spec/Gpenverne/PutioDriveBundle/Service/HttpClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function it_is_initializable()

public function it_makes_get_request_using_guzzle_client(StreamInterface $stream, RequestInterface $request, $client)
{
$client->request('GET', 'some-url', ['headers' => ['Accept' => 'application/json']])->willReturn($request)->shouldBeCalled();
$client->request('GET', 'some-url', ['headers' => ['Accept' => 'application/json'], 'query' => []])->willReturn($request)->shouldBeCalled();
$request->getBody()->willReturn($stream);
$stream->getContents()->willReturn('Some content');

Expand All @@ -31,7 +31,7 @@ public function it_makes_get_request_using_guzzle_client(StreamInterface $stream

public function it_makes_get_request_and_return_json(StreamInterface $stream, RequestInterface $request, $client)
{
$client->request('GET', 'some-url', ['headers' => ['Accept' => 'application/json']])->willReturn($request)->shouldBeCalled();
$client->request('GET', 'some-url', ['headers' => ['Accept' => 'application/json'], 'query' => []])->willReturn($request)->shouldBeCalled();
$request->getBody()->willReturn($stream);
$stream->getContents()->willReturn('"some json content"');

Expand Down
15 changes: 13 additions & 2 deletions spec/Gpenverne/PutioDriveBundle/Service/UrlGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function let(Router $router, RequestStack $requestStack, Request $request

public function it_returns_a_redirect_url()
{
$url = urlencode('http://a-full-url');
$url = 'http://a-full-url';

$this->getRedirectUrl()->shouldReturn(
sprintf('https://api.put.io/v2/oauth2/authenticate?client_id=a-client-id&response_type=code&redirect_uri=%s', $url)
Expand All @@ -32,6 +32,17 @@ public function it_returns_a_redirect_url()

public function it_returns_a_token_url()
{
$this->getTokenUrl('a-code')->shouldReturn('https://api.put.io/v2/oauth2/access_token?client_id=a-client-id&client_secret=a-client-secret&grant_type=authorization_code&code=a-code&redirect_uri=http%3A%2F%2Fa-full-url');
$this->getTokenUrl()->shouldReturn('https://api.put.io/v2/oauth2/access_token');
}

public function it_returns_token_url_parameters()
{
$this->getTokenUrlParameters('some-code')->shouldReturn([
'client_id' => 'a-client-id',
'client_secret' => 'a-client-secret',
'grant_type' => 'authorization_code',
'code' => 'some-code',
'redirect_uri' => 'http://a-full-url',
]);
}
}
6 changes: 4 additions & 2 deletions src/EventListener/PutioCodeEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ public function onCodeObtained(PutioCodeEvent $event)
*/
public function getToken($code)
{
$url = $this->urlGenerator->getTokenUrl($code);
$data = $this->httpClient->getJson($url);
$url = $this->urlGenerator->getTokenUrl();
$parameters = $this->urlGenerator->getTokenUrlParameters($code);

$data = $this->httpClient->getJson($url, $parameters);

if (null === $data || !is_object($data) || !isset($data->access_token)) {
$result = $data;
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:
putio.drive.http_client:
class: Gpenverne\PutioDriveBundle\Service\HttpClient
arguments:
- "@put.guzzle.client"
- "@putio.guzzle.client"
putio.drive.url_generator:
class: Gpenverne\PutioDriveBundle\Service\UrlGenerator
arguments:
Expand Down Expand Up @@ -51,7 +51,7 @@ services:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 0 }

# -- external
put.guzzle.client:
putio.guzzle.client:
class: GuzzleHttp\Client
psr_cloud_files.factory.folder:
class: Gpenverne\PsrCloudFiles\Factories\FolderFactory
Expand Down
7 changes: 4 additions & 3 deletions src/Service/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public function get($url)
*
* @return mixed
*/
public function getJson($url)
public function getJson($url, $parameters = [])
{
$res = $this->getResponse($url);
$res = $this->getResponse($url, 'GET', $parameters);

return json_decode($res->getBody()->getContents());
}
Expand All @@ -47,10 +47,11 @@ public function getJson($url)
*
* @return StreamInterface
*/
private function getResponse($url, $method = 'GET')
private function getResponse($url, $method = 'GET', $parameters = [])
{
return $this->client->request($method, $url, [
'headers' => $this->getDefaultHeaders(),
'query' => $parameters,
]);
}

Expand Down
25 changes: 21 additions & 4 deletions src/Service/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Router;

/**
* @see https://put.io/v2/docs/gettingstarted.html#obtain-an-access-token-oauth-token
*/
class UrlGenerator
{
const CODE_PARAMETER = 'code';
Expand Down Expand Up @@ -40,16 +43,30 @@ public function getRedirectUrl()
{
$url = 'https://api.put.io/v2/oauth2/authenticate?client_id=%s&response_type=code&redirect_uri=%s';

return sprintf($url, $this->params['client_id'], urlencode($this->params['redirect_uri']));
return sprintf($url, $this->params['client_id'], $this->params['redirect_uri']);
}

/**
* @return string
*/
public function getTokenUrl($code)
public function getTokenUrl()
{
$url = 'https://api.put.io/v2/oauth2/access_token?client_id=%s&client_secret=%s&grant_type=authorization_code&code=%s&redirect_uri=%s';
return 'https://api.put.io/v2/oauth2/access_token';
}

return sprintf($url, $this->params['client_id'], $this->params['client_secret'], $code, urlencode($this->params['redirect_uri']));
/**
* @param string $code
*
* @return array
*/
public function getTokenUrlParameters($code)
{
return [
'client_id' => $this->params['client_id'],
'client_secret' => $this->params['client_secret'],
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $this->params['redirect_uri'],
];
}
}

0 comments on commit 2c3523b

Please sign in to comment.