1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace PeeHaa\AsyncTwitter\Http; |
4
|
|
|
|
5
|
|
|
use Amp\Artax\Client as ArtaxClient; |
6
|
|
|
use Amp\Artax\FormBody; |
7
|
|
|
use Amp\Promise; |
8
|
|
|
use PeeHaa\AsyncTwitter\Exception; |
9
|
|
|
use PeeHaa\AsyncTwitter\Request\Body; |
10
|
|
|
use PeeHaa\AsyncTwitter\Request\FieldParameter; |
11
|
|
|
use PeeHaa\AsyncTwitter\Request\FileParameter; |
12
|
|
|
use PeeHaa\AsyncTwitter\Request\Parameter; |
13
|
|
|
use PeeHaa\AsyncTwitter\Request\Url; |
14
|
|
|
use PeeHaa\AsyncTwitter\Oauth\Header; |
15
|
|
|
use Amp\Artax\Request; |
16
|
|
|
|
17
|
|
|
class Artax implements Client |
18
|
|
|
{ |
19
|
|
|
private $client; |
20
|
|
|
|
21
|
4 |
|
public function __construct(ArtaxClient $client) |
22
|
|
|
{ |
23
|
4 |
|
$this->client = $client; |
24
|
|
|
} |
25
|
|
|
|
26
|
2 |
|
public function post(Url $url, Header $header, Body $body, int $flags = 0): Promise |
27
|
|
|
{ |
28
|
2 |
|
$body = $this->getFormBody($body); |
|
|
|
|
29
|
|
|
|
30
|
2 |
|
$request = (new Request) |
31
|
2 |
|
->setMethod('POST') |
32
|
2 |
|
->setUri($url->getUrl()) |
33
|
2 |
|
->setHeader('Authorization', $header->getHeader()) |
34
|
2 |
|
->setBody($body) |
35
|
|
|
; |
36
|
|
|
|
37
|
2 |
|
$options = []; |
38
|
2 |
|
if ($flags & Client::OP_STREAM) { |
39
|
1 |
|
$options[ArtaxClient::OP_MS_TRANSFER_TIMEOUT] = -1; |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
return $this->client->request($request, $options); |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
private function getFormBody(Body $body): FormBody |
46
|
|
|
{ |
47
|
2 |
|
$formBody = new FormBody(); |
48
|
|
|
|
49
|
2 |
|
foreach ($body->getParameters() as $parameter) { |
50
|
2 |
|
if ($parameter instanceof FieldParameter) { |
51
|
2 |
|
$formBody->addField($parameter->getKey(), $parameter->getValue(), $parameter->getType()); |
52
|
|
|
} else if ($parameter instanceof FileParameter) { |
53
|
|
|
$formBody->addFile($parameter->getKey(), $parameter->getPath(), $parameter->getType()); |
54
|
|
|
} else { |
55
|
2 |
|
throw new Exception("Unexpected parameter type: " . get_class($parameter)); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
return $formBody; |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
public function get(Url $url, Header $header, array $parameters, int $flags = 0): Promise |
63
|
|
|
{ |
64
|
2 |
|
$request = (new Request) |
65
|
2 |
|
->setMethod('GET') |
66
|
2 |
|
->setUri($url->getUrl() . $this->buildQueryString(...$parameters)) |
67
|
2 |
|
->setAllHeaders(['Authorization' => $header->getHeader()]) |
68
|
|
|
; |
69
|
|
|
|
70
|
2 |
|
$options = []; |
71
|
2 |
|
if ($flags & Client::OP_STREAM) { |
72
|
1 |
|
$options[ArtaxClient::OP_MS_TRANSFER_TIMEOUT] = -1; |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
return $this->client->request($request, $options); |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
private function buildQueryString(Parameter ...$parameters): string |
79
|
|
|
{ |
80
|
2 |
|
$queryString = []; |
81
|
|
|
|
82
|
2 |
|
foreach ($parameters as $parameter) { |
83
|
2 |
|
if (!$parameter instanceof FieldParameter) { |
84
|
|
|
throw new Exception("Unexpected parameter type: " . get_class($parameter)); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
$queryString[$parameter->getKey()] = $parameter->getValue(); |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
return '?' . http_build_query($queryString); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|