Skip to content

Simplify constructing URIs #1

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

Merged
merged 1 commit into from
Sep 5, 2015
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
},
"require": {
"php": ">=5.3",
"clue/buzz-react": "0.1.*"
"clue/buzz-react": "~0.4.1"
}
}
26 changes: 14 additions & 12 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@

class Client
{
private $url;
private $browser;
private $key;
private $hash;
private $browser;

public function __construct($url, $key, $hash, Browser $browser)
public function __construct(Browser $browser, $key, $hash)
{
$this->url = $url;
$this->browser = $browser;
$this->key = $key;
$this->hash = $hash;
$this->browser = $browser;
}

public function reboot()
Expand Down Expand Up @@ -88,13 +86,17 @@ public function info($ipaddr = true, $hdd = true, $mem = true, $bw = true)

private function request($action, array $args = array())
{
$url = $this->url . '?' . http_build_query(array(
'key' => $this->key,
'hash' => $this->hash,
'action' => $action
) + $args);

return $this->browser->get($url)->then(
return $this->browser->get(
$this->browser->resolve(
'{?key,hash,action,args*}',
array(
'action' => $action,
'args' => $args,
'key' => $this->key,
'hash' => $this->hash
)
)
)->then(
function (Response $response) {
preg_match_all('/<(.*?)>([^<]+)<\/\\1>/i', (string)$response->getBody(), $match);
$result = array();
Expand Down
4 changes: 2 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function createClient($address)
}
}

if (!isset($parts['user'], $parts['user'])) {
if (!isset($parts['user'], $parts['pass'])) {
throw new \InvalidArgumentException('Given API URL must include user (API hash) and pass (API key)');
}

Expand All @@ -67,6 +67,6 @@ public function createClient($address)
}
$str .= $parts['path'];

return new Client($str, $parts['pass'], $parts['user'], $this->browser);
return new Client($this->browser->withBase($str), $parts['pass'], $parts['user']);
}
}
9 changes: 5 additions & 4 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ class ClientTest extends TestCase

public function setUp()
{
$this->browser = $this->getMockBuilder('Clue\React\Buzz\Browser')->disableOriginalConstructor()->getMock();
$this->browser = $this->getMockBuilder('Clue\React\Buzz\Browser')->setConstructorArgs(array($this->getMock('React\EventLoop\LoopInterface')))->setMethods(array('get'))->getMock();
$this->browser = $this->browser->withBase('http://a/path');

$this->client = new Client('url', 'mykey', 'myhash', $this->browser);
$this->client = new Client($this->browser, 'mykey', 'myhash');
}

public function testReboot()
Expand Down Expand Up @@ -109,7 +110,7 @@ public function testHttpError()

$this->browser->expects($this->once())
->method('get')
->with($this->equalTo('url?key=mykey&hash=myhash&action=status'), array())
->with($this->equalTo('http://a/path?key=mykey&hash=myhash&action=status'), array())
->will($this->returnValue($d->promise()));

$this->expectPromiseReject($this->client->status());
Expand All @@ -122,7 +123,7 @@ private function setupBrowser($expectedUrl, $fakeResponseBody)

$this->browser->expects($this->once())
->method('get')
->with($this->equalTo('url?key=mykey&hash=myhash&' . $expectedUrl), array())
->with($this->equalTo('http://a/path?key=mykey&hash=myhash&' . $expectedUrl), array())
->will($this->returnValue($d->promise()));
}

Expand Down