1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mpyw\Cowitter\Traits; |
4
|
|
|
|
5
|
|
|
use mpyw\Co\CoInterface; |
6
|
|
|
use mpyw\Co\CURLException; |
7
|
|
|
use mpyw\Cowitter\Components\StreamHandler; |
8
|
|
|
use mpyw\Cowitter\Helpers\CurlExecutor; |
9
|
|
|
|
10
|
|
|
trait RequestorTrait |
11
|
|
|
{ |
12
|
|
|
abstract protected function getInternalCurl(); |
13
|
|
|
|
14
|
6 |
|
public function getAsync($endpoint, array $params = [], $return_response_object = false) |
15
|
6 |
|
{ |
16
|
6 |
|
yield CoInterface::RETURN_WITH => (yield CurlExecutor::execDecodedAsync($this->getInternalCurl()->get($endpoint, $params), $return_response_object)); |
17
|
|
|
// @codeCoverageIgnoreStart |
18
|
|
|
} |
19
|
|
|
// @codeCoverageIgnoreEnd |
20
|
|
|
|
21
|
11 |
|
public function postAsync($endpoint, array $params = [], $return_response_object = false) |
22
|
11 |
|
{ |
23
|
11 |
|
yield CoInterface::RETURN_WITH => (yield CurlExecutor::execDecodedAsync($this->getInternalCurl()->post($endpoint, $params), $return_response_object)); |
24
|
|
|
// @codeCoverageIgnoreStart |
25
|
|
|
} |
26
|
|
|
// @codeCoverageIgnoreEnd |
27
|
|
|
|
28
|
|
|
public function deleteAsync($endpoint, array $params = [], $return_response_object = false) |
29
|
|
|
{ |
30
|
|
|
yield CoInterface::RETURN_WITH => (yield CurlExecutor::execDecodedAsync($this->getInternalCurl()->delete($endpoint, $params), $return_response_object)); |
31
|
|
|
// @codeCoverageIgnoreStart |
32
|
|
|
} |
33
|
|
|
// @codeCoverageIgnoreEnd |
34
|
|
|
|
35
|
|
|
public function putAsync($endpoint, array $params = [], $return_response_object = false) |
36
|
|
|
{ |
37
|
|
|
yield CoInterface::RETURN_WITH => (yield CurlExecutor::execDecodedAsync($this->getInternalCurl()->put($endpoint, $params), $return_response_object)); |
38
|
|
|
// @codeCoverageIgnoreStart |
39
|
|
|
} |
40
|
|
|
// @codeCoverageIgnoreEnd |
41
|
|
|
|
42
|
11 |
|
public function postMultipartAsync($endpoint, array $params = [], $return_response_object = false) |
43
|
11 |
|
{ |
44
|
11 |
|
yield CoInterface::RETURN_WITH => (yield CurlExecutor::execDecodedAsync($this->getInternalCurl()->postMultipart($endpoint, $params), $return_response_object)); |
45
|
|
|
// @codeCoverageIgnoreStart |
46
|
|
|
} |
47
|
|
|
// @codeCoverageIgnoreEnd |
48
|
|
|
|
49
|
6 |
|
public function get($endpoint, array $params = [], $return_response_object = false) |
50
|
6 |
|
{ |
51
|
6 |
|
return CurlExecutor::execDecoded($this->getInternalCurl()->get($endpoint, $params), $return_response_object); |
52
|
|
|
} |
53
|
|
|
|
54
|
5 |
|
public function post($endpoint, array $params = [], $return_response_object = false) |
55
|
5 |
|
{ |
56
|
5 |
|
return CurlExecutor::execDecoded($this->getInternalCurl()->post($endpoint, $params), $return_response_object); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function postJson($endpoint, array $params = [], $return_response_object = false) |
60
|
|
|
{ |
61
|
|
|
return CurlExecutor::execDecoded($this->getInternalCurl()->postJson($endpoint, $params), $return_response_object); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function delete($endpoint, array $params = [], $return_response_object = false) |
65
|
|
|
{ |
66
|
|
|
return CurlExecutor::execDecoded($this->getInternalCurl()->delete($endpoint, $params), $return_response_object); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function put($endpoint, array $params = [], $return_response_object = false) |
70
|
|
|
{ |
71
|
|
|
return CurlExecutor::execDecoded($this->getInternalCurl()->put($endpoint, $params), $return_response_object); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
public function postMultipart($endpoint, array $params = [], $return_response_object = false) |
75
|
1 |
|
{ |
76
|
1 |
|
return CurlExecutor::execDecoded($this->getInternalCurl()->postMultipart($endpoint, $params), $return_response_object); |
77
|
|
|
} |
78
|
|
|
|
79
|
6 |
|
public function streamingAsync($endpoint, callable $event_handler, array $params = [], callable $header_response_handler = null) |
80
|
6 |
|
{ |
81
|
6 |
|
$handler = new StreamHandler($header_response_handler, $event_handler); |
82
|
6 |
|
$ch = $this->getInternalCurl()->streaming($endpoint, $params, $handler); |
83
|
|
|
try { |
84
|
6 |
|
yield $ch; |
85
|
5 |
|
} catch (CURLException $e) { |
86
|
5 |
|
if (!$handler->isHaltedByUser()) { |
87
|
1 |
|
throw $e; |
88
|
|
|
} |
89
|
|
|
} |
90
|
5 |
|
if (!$handler->isHaltedByUser()) { |
91
|
1 |
|
throw new \UnexpectedValueException('Streaming stopped unexpectedly.'); |
92
|
|
|
} |
93
|
4 |
|
} |
94
|
|
|
|
95
|
9 |
|
public function streaming($endpoint, callable $event_handler, array $params = [], callable $header_response_handler = null) |
96
|
9 |
|
{ |
97
|
9 |
|
$handler = new StreamHandler($header_response_handler, $event_handler); |
98
|
9 |
|
$ch = $this->getInternalCurl()->streaming($endpoint, $params, $handler); |
99
|
9 |
|
$result = curl_exec($ch); |
100
|
7 |
|
if (!$handler->isHaltedByUser() && $result === false) { |
101
|
1 |
|
throw new CURLException(curl_error($ch), curl_errno($ch), $ch); |
102
|
|
|
} |
103
|
6 |
|
if (!$handler->isHaltedByUser()) { |
104
|
1 |
|
throw new \UnexpectedValueException('Streaming stopped unexpectedly.'); |
105
|
|
|
} |
106
|
5 |
|
} |
107
|
|
|
|
108
|
1 |
|
public function getOutAsync($endpoint, array $params = [], $return_response_object = false) |
109
|
1 |
|
{ |
110
|
1 |
|
yield CoInterface::RETURN_WITH => (yield CurlExecutor::execDecodedAsync($this->getInternalCurl()->getOut($endpoint, $params), $return_response_object)); |
111
|
|
|
// @codeCoverageIgnoreStart |
112
|
|
|
} |
113
|
|
|
// @codeCoverageIgnoreEnd |
114
|
|
|
|
115
|
1 |
|
public function postOutAsync($endpoint, array $params = [], $return_response_object = false) |
116
|
1 |
|
{ |
117
|
1 |
|
yield CoInterface::RETURN_WITH => (yield CurlExecutor::execDecodedAsync($this->getInternalCurl()->postOut($endpoint, $params), $return_response_object)); |
118
|
|
|
// @codeCoverageIgnoreStart |
119
|
|
|
} |
120
|
|
|
// @codeCoverageIgnoreEnd |
121
|
|
|
|
122
|
1 |
|
public function postMultipartOutAsync($endpoint, array $params = [], $return_response_object = false) |
123
|
1 |
|
{ |
124
|
1 |
|
yield CoInterface::RETURN_WITH => (yield CurlExecutor::execDecodedAsync($this->getInternalCurl()->postMultipartOut($endpoint, $params), $return_response_object)); |
125
|
|
|
// @codeCoverageIgnoreStart |
126
|
|
|
} |
127
|
|
|
// @codeCoverageIgnoreEnd |
128
|
|
|
|
129
|
1 |
|
public function getOut($endpoint, array $params = [], $return_response_object = false) |
130
|
1 |
|
{ |
131
|
1 |
|
return CurlExecutor::execDecoded($this->getInternalCurl()->getOut($endpoint, $params), $return_response_object); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
public function postOut($endpoint, array $params = [], $return_response_object = false) |
135
|
1 |
|
{ |
136
|
1 |
|
return CurlExecutor::execDecoded($this->getInternalCurl()->postOut($endpoint, $params), $return_response_object); |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
public function postMultipartOut($endpoint, array $params = [], $return_response_object = false) |
140
|
1 |
|
{ |
141
|
1 |
|
return CurlExecutor::execDecoded($this->getInternalCurl()->postMultipartOut($endpoint, $params), $return_response_object); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|