Skip to content

Commit

Permalink
Prefer guzzle over file get contents (#3)
Browse files Browse the repository at this point in the history
* Updated dependencies

* updated services

* Using guzzle to make get calls
  • Loading branch information
gpenverne authored May 16, 2017
1 parent f7ffa3f commit dcbf24e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"symfony/class-loader": "*",
"symfony/dependency-injection": "*",
"nicoswd/putio": "*",
"gpenverne/psr-cloud-files": "dev-master"
"gpenverne/psr-cloud-files": "dev-master",
"guzzlehttp/guzzle": "~6.0"
},
"require-dev": {
"phpspec/phpspec": "^3.2",
Expand Down
26 changes: 26 additions & 0 deletions spec/Gpenverne/PutioDriveBundle/Service/HttpClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,38 @@
namespace spec\Gpenverne\PutioDriveBundle\Service;

use Gpenverne\PutioDriveBundle\Service\HttpClient;
use GuzzleHttp\Client;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;

class HttpClientSpec extends ObjectBehavior
{
public function let(Client $client)
{
$this->beConstructedWith($client);
}

public function it_is_initializable()
{
$this->shouldHaveType(HttpClient::class);
}

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();
$request->getBody()->willReturn($stream);
$stream->getContents()->willReturn('Some content');

$this->get('some-url')->shouldReturn('Some content');
}

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();
$request->getBody()->willReturn($stream);
$stream->getContents()->willReturn('"some json content"');

$this->getJson('some-url')->shouldReturn('some json content');
}
}
4 changes: 4 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ services:
# -- utils
putio.drive.http_client:
class: Gpenverne\PutioDriveBundle\Service\HttpClient
arguments:
- "@put.guzzle.client"
putio.drive.url_generator:
class: Gpenverne\PutioDriveBundle\Service\UrlGenerator
arguments:
Expand Down Expand Up @@ -49,6 +51,8 @@ services:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 0 }

# -- external
put.guzzle.client:
class: GuzzleHttp\Client
psr_cloud_files.factory.folder:
class: Gpenverne\PsrCloudFiles\Factories\FolderFactory
psr_cloud_files.factory.file:
Expand Down
44 changes: 42 additions & 2 deletions src/Service/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@

namespace Gpenverne\PutioDriveBundle\Service;

use GuzzleHttp\Client;
use Psr\Http\Message\StreamInterface;

class HttpClient
{
/**
* @param Client $client
*/
public function __construct(Client $client)
{
$this->client = $client;
}

/**
* @param string $url
*
* @return string
*/
public function get($url)
{
return file_get_contents($url);
$res = $this->getResponse($url);

return $res->getBody()->getContents();
}

/**
Expand All @@ -21,6 +34,33 @@ public function get($url)
*/
public function getJson($url)
{
return json_decode($this->get($url));
$res = $this->getResponse($url);

return json_decode($res->getBody()->getContents());
}

/**
* @param string $url
* @param string $method
* @param array $parameters
* @param array $headers
*
* @return StreamInterface
*/
private function getResponse($url, $method = 'GET')
{
return $this->client->request($method, $url, [
'headers' => $this->getDefaultHeaders(),
]);
}

/**
* @return array
*/
private function getDefaultHeaders()
{
return [
'Accept' => 'application/json',
];
}
}

0 comments on commit dcbf24e

Please sign in to comment.