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($url->getUrl())) |
31
|
2 |
|
->withMethod('POST') |
32
|
2 |
|
->withHeader('Authorization', $header->getHeader()) |
33
|
2 |
|
->withBody($body) |
34
|
|
|
; |
35
|
|
|
|
36
|
2 |
|
$options = []; |
37
|
2 |
|
if ($flags & Client::OP_STREAM) { |
38
|
1 |
|
$options[ArtaxClient::OP_TRANSFER_TIMEOUT] = -1; |
39
|
|
|
} |
40
|
|
|
|
41
|
2 |
|
return $this->client->request($request, $options); |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
private function getFormBody(Body $body): FormBody |
45
|
|
|
{ |
46
|
2 |
|
$formBody = new FormBody(); |
47
|
|
|
|
48
|
2 |
|
foreach ($body->getParameters() as $parameter) { |
49
|
2 |
|
if ($parameter instanceof FieldParameter) { |
50
|
2 |
|
$formBody->addField($parameter->getKey(), $parameter->getValue(), $parameter->getType()); |
51
|
|
|
} else if ($parameter instanceof FileParameter) { |
52
|
|
|
$formBody->addFile($parameter->getKey(), $parameter->getPath(), $parameter->getType()); |
53
|
|
|
} else { |
54
|
2 |
|
throw new Exception("Unexpected parameter type: " . get_class($parameter)); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
return $formBody; |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
public function get(Url $url, Header $header, array $parameters, int $flags = 0): Promise |
62
|
|
|
{ |
63
|
2 |
|
$request = (new Request($url->getUrl() . $this->buildQueryString(...$parameters))) |
64
|
2 |
|
->withMethod('GET') |
65
|
2 |
|
->withAllHeaders(['Authorization' => $header->getHeader()]) |
66
|
|
|
; |
67
|
|
|
|
68
|
2 |
|
$options = []; |
69
|
2 |
|
if ($flags & Client::OP_STREAM) { |
70
|
1 |
|
$options[ArtaxClient::OP_TRANSFER_TIMEOUT] = -1; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
return $this->client->request($request, $options); |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
private function buildQueryString(Parameter ...$parameters): string |
77
|
|
|
{ |
78
|
2 |
|
$queryString = []; |
79
|
|
|
|
80
|
2 |
|
foreach ($parameters as $parameter) { |
81
|
2 |
|
if (!$parameter instanceof FieldParameter) { |
82
|
|
|
throw new Exception("Unexpected parameter type: " . get_class($parameter)); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
$queryString[$parameter->getKey()] = $parameter->getValue(); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
return '?' . http_build_query($queryString); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|