Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Guzzle client #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
"Essence\\Http\\": "src"
}
},
"require-dev": {
"guzzlehttp/guzzle": "~6.2"
},
"suggest": {
"ext-curl": "*"
"ext-curl": "*",
"guzzlehttp/guzzle": "~6.2"
}
}
44 changes: 44 additions & 0 deletions src/Client/Guzzle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Essence\Http\Client;

use Essence\Http\Client;
use Essence\Http\Exception;



/**
* Handles HTTP related operations through Guzzle.
*/
class Guzzle implements Client {

private $options;

private $client;

public function __construct($base_uri='http://example.com', $timeout=2.0) {
$this->client = new \GuzzleHttp\Client([
'base_uri' => $base_uri,
'timeout' => $timeout,
]);

$this->options = [];
}

public function setUserAgent($agent) {
$this->options = [
'User-Agent' => $agent
];
}

public function get($url) {
try {
$response = $this->client->request('GET', $url, $this->options);

} catch (\GuzzleHttp\Exception\RequestException $e) {
throw new Exception($url, $e->getResponse()->getStatusCode());
}

return (string)$response->getBody();
}
}
34 changes: 34 additions & 0 deletions tests/Client/GuzzleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* @author Félix Girault <[email protected]>
* @license MIT
*/

namespace Essence\Http\Client;

use PHPUnit_Framework_TestCase as TestCase;



/**
* Test case for Guzzle.
*/
class GuzzleTest extends TestCase {

public $Guzzle = null;

public function setUp() {
$this->Guzzle = new Guzzle();
}

public function testGet() {
$content = $this->Guzzle->get('http://example.com/');
$this->assertRegExp('/This domain is established to be used/', $content);
}

public function testGetUnreachable() {
$this->setExpectedException('\\Essence\\Http\\Exception');
$this->Guzzle->get('http://example.com/dfzgdz/czcdcd');
}
}