Skip to content

Commit ade7ab1

Browse files
committed
Simplify constructing URIs
1 parent 7c94374 commit ade7ab1

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
},
1616
"require": {
1717
"php": ">=5.3",
18-
"clue/buzz-react": "0.1.*"
18+
"clue/buzz-react": "~0.4.1"
1919
}
2020
}

src/Client.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@
77

88
class Client
99
{
10-
private $url;
10+
private $browser;
1111
private $key;
1212
private $hash;
13-
private $browser;
1413

15-
public function __construct($url, $key, $hash, Browser $browser)
14+
public function __construct(Browser $browser, $key, $hash)
1615
{
17-
$this->url = $url;
16+
$this->browser = $browser;
1817
$this->key = $key;
1918
$this->hash = $hash;
20-
$this->browser = $browser;
2119
}
2220

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

8987
private function request($action, array $args = array())
9088
{
91-
$url = $this->url . '?' . http_build_query(array(
92-
'key' => $this->key,
93-
'hash' => $this->hash,
94-
'action' => $action
95-
) + $args);
96-
97-
return $this->browser->get($url)->then(
89+
return $this->browser->get(
90+
$this->browser->resolve(
91+
'{?key,hash,action,args*}',
92+
array(
93+
'action' => $action,
94+
'args' => $args,
95+
'key' => $this->key,
96+
'hash' => $this->hash
97+
)
98+
)
99+
)->then(
98100
function (Response $response) {
99101
preg_match_all('/<(.*?)>([^<]+)<\/\\1>/i', (string)$response->getBody(), $match);
100102
$result = array();

src/Factory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function createClient($address)
4141
}
4242
}
4343

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

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

70-
return new Client($str, $parts['pass'], $parts['user'], $this->browser);
70+
return new Client($this->browser->withBase($str), $parts['pass'], $parts['user']);
7171
}
7272
}

tests/ClientTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ class ClientTest extends TestCase
1313

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

18-
$this->client = new Client('url', 'mykey', 'myhash', $this->browser);
19+
$this->client = new Client($this->browser, 'mykey', 'myhash');
1920
}
2021

2122
public function testReboot()
@@ -109,7 +110,7 @@ public function testHttpError()
109110

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

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

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

0 commit comments

Comments
 (0)