Skip to content

Commit 74a029f

Browse files
committed
Add dedicated ResponseParser
1 parent daef3e5 commit 74a029f

3 files changed

Lines changed: 90 additions & 37 deletions

File tree

src/Client.php

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Clue\React\Buzz\Browser;
66
use Clue\React\Buzz\Message\Response;
7+
use Clue\React\Docker\Io\ResponseParser;
78

89
/**
910
*
@@ -12,45 +13,51 @@
1213
class Client
1314
{
1415
private $browser;
16+
private $parser;
1517

16-
public function __construct(Browser $browser)
18+
public function __construct(Browser $browser, ResponseParser $parser = null)
1719
{
20+
if ($parser === null) {
21+
$parser = new ResponseParser();
22+
}
23+
1824
$this->browser = $browser;
25+
$this->parser = $parser;
1926
}
2027

2128
public function ping()
2229
{
23-
return $this->browser->get('/_ping')->then(array($this, 'expectPlain'));
30+
return $this->browser->get('/_ping')->then(array($this->parser, 'expectPlain'));
2431
}
2532

2633
public function info()
2734
{
28-
return $this->browser->get('/info')->then(array($this, 'expectJson'));
35+
return $this->browser->get('/info')->then(array($this->parser, 'expectJson'));
2936
}
3037

3138
public function version()
3239
{
33-
return $this->browser->get('/version')->then(array($this, 'expectJson'));
40+
return $this->browser->get('/version')->then(array($this->parser, 'expectJson'));
3441
}
3542

3643
public function containerList($all = false, $size = false)
3744
{
38-
return $this->browser->get('/containers/json?all=' . $all . '&size=' . $size)->then(array($this, 'expectJson'));
45+
return $this->browser->get('/containers/json?all=' . $all . '&size=' . $size)->then(array($this->parser, 'expectJson'));
3946
}
4047

4148
public function containerInspect($container)
4249
{
43-
return $this->browser->get('/containers/' . $container . '/json')->then(array($this, 'expectJson'));
50+
return $this->browser->get('/containers/' . $container . '/json')->then(array($this->parser, 'expectJson'));
4451
}
4552

4653
public function containerTop($container)
4754
{
48-
return $this->browser->get('/containers/' . $container . '/top')->then(array($this, 'expectJson'));
55+
return $this->browser->get('/containers/' . $container . '/top')->then(array($this->parser, 'expectJson'));
4956
}
5057

5158
public function containerWait($container)
5259
{
53-
return $this->browser->post('/containers/' . $container . '/wait')->then(array($this, 'expectJson'));
60+
return $this->browser->post('/containers/' . $container . '/wait')->then(array($this->parser, 'expectJson'));
5461
}
5562

5663
/**
@@ -61,7 +68,7 @@ public function containerWait($container)
6168
*/
6269
public function containerStop($container, $t)
6370
{
64-
return $this->browser->post('/containers/' . $container . '/stop?t=' . $t)->then(array($this, 'expectEmpty'));
71+
return $this->browser->post('/containers/' . $container . '/stop?t=' . $t)->then(array($this->parser, 'expectEmpty'));
6572
}
6673

6774
/**
@@ -72,22 +79,22 @@ public function containerStop($container, $t)
7279
*/
7380
public function containerRestart($container, $t)
7481
{
75-
return $this->browser->post('/containers/' . $container . '/restart?t=' . $t)->then(array($this, 'expectEmpty'));
82+
return $this->browser->post('/containers/' . $container . '/restart?t=' . $t)->then(array($this->parser, 'expectEmpty'));
7683
}
7784

7885
public function containerKill($container, $signal = null)
7986
{
80-
return $this->browser->post('/containers/' . $container . '/kill?signal=' . $signal)->then(array($this, 'expectEmpty'));
87+
return $this->browser->post('/containers/' . $container . '/kill?signal=' . $signal)->then(array($this->parser, 'expectEmpty'));
8188
}
8289

8390
public function containerPause($container)
8491
{
85-
return $this->browser->post('/containers/' . $container . '/pause')->then(array($this, 'expectEmpty'));
92+
return $this->browser->post('/containers/' . $container . '/pause')->then(array($this->parser, 'expectEmpty'));
8693
}
8794

8895
public function containerUnpause($container)
8996
{
90-
return $this->browser->post('/containers/' . $container . '/unpause')->then(array($this, 'expectEmpty'));
97+
return $this->browser->post('/containers/' . $container . '/unpause')->then(array($this->parser, 'expectEmpty'));
9198
}
9299

93100
/**
@@ -99,33 +106,11 @@ public function containerUnpause($container)
99106
*/
100107
public function containerDelete($container, $v = false, $force = false)
101108
{
102-
return $this->browser->delete('/containers/' . $container . '?v=' . (int)$v . '&force=' . (int)$force)->then(array($this, 'expectEmpty'));
109+
return $this->browser->delete('/containers/' . $container . '?v=' . (int)$v . '&force=' . (int)$force)->then(array($this->parser, 'expectEmpty'));
103110
}
104111

105112
public function containerResize($container, $w, $h)
106113
{
107-
return $this->browser->get('/containers/' . $container . '/resize?w=' . $w . '&h=' . $h)->then(array($this, 'expectEmpty'));
108-
}
109-
110-
public function expectPlain(Response $response)
111-
{
112-
// text/plain
113-
114-
return (string)$response->getBody();
115-
}
116-
117-
public function expectJson(Response $response)
118-
{
119-
// application/json
120-
121-
return json_decode((string)$response->getBody(), true);
122-
}
123-
124-
public function expectEmpty(Response $response)
125-
{
126-
// 204 No Content
127-
// no content-type
128-
129-
return $this->expectPlain($response);
114+
return $this->browser->get('/containers/' . $container . '/resize?w=' . $w . '&h=' . $h)->then(array($this->parser, 'expectEmpty'));
130115
}
131116
}

src/Io/ResponseParser.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Clue\React\Docker\Io;
4+
5+
use Clue\React\Buzz\Message\Response;
6+
7+
class ResponseParser
8+
{
9+
public function expectPlain(Response $response)
10+
{
11+
// text/plain
12+
13+
return (string)$response->getBody();
14+
}
15+
16+
public function expectJson(Response $response)
17+
{
18+
// application/json
19+
20+
return json_decode((string)$response->getBody(), true);
21+
}
22+
23+
public function expectEmpty(Response $response)
24+
{
25+
// 204 No Content
26+
// no content-type
27+
28+
return $this->expectPlain($response);
29+
}
30+
}

tests/Io/ResponseParserTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Clue\React\Docker\Io\ResponseParser;
4+
use Clue\React\Buzz\Message\Response;
5+
use Clue\React\Buzz\Message\Body;
6+
7+
class ResponseParserTest extends TestCase
8+
{
9+
private $parser;
10+
11+
public function setUp()
12+
{
13+
$this->parser = new ResponseParser();
14+
}
15+
16+
public function testPlain()
17+
{
18+
$body = 'OK';
19+
$this->assertEquals($body, $this->parser->expectPlain($this->createResponse($body)));
20+
}
21+
22+
public function testJson()
23+
{
24+
$json = array('test' => 'value');
25+
$this->assertEquals($json, $this->parser->expectJson($this->createResponse(json_encode($json))));
26+
}
27+
28+
public function testEmpty()
29+
{
30+
$this->assertEquals('', $this->parser->expectEmpty($this->createResponse()));
31+
}
32+
33+
private function createResponse($body = '')
34+
{
35+
return new Response('HTTP/1.0', 200, 'OK', null, new Body($body));
36+
}
37+
38+
}

0 commit comments

Comments
 (0)