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
|
11 |
|
public function deleteAsync($endpoint, array $params = [], $return_response_object = false) |
29
|
11 |
|
{ |
30
|
11 |
|
yield CoInterface::RETURN_WITH => (yield CurlExecutor::execDecodedAsync($this->getInternalCurl()->delete($endpoint, $params), $return_response_object)); |
31
|
|
|
// @codeCoverageIgnoreStart |
32
|
|
|
} |
33
|
|
|
// @codeCoverageIgnoreEnd |
34
|
|
|
|
35
|
6 |
|
public function putAsync($endpoint, array $params = [], $return_response_object = false) |
36
|
6 |
|
{ |
37
|
6 |
|
yield CoInterface::RETURN_WITH => (yield CurlExecutor::execDecodedAsync($this->getInternalCurl()->put($endpoint, $params), $return_response_object)); |
38
|
|
|
// @codeCoverageIgnoreStart |
39
|
|
|
} |
40
|
5 |
|
// @codeCoverageIgnoreEnd |
41
|
5 |
|
|
42
|
5 |
|
public function postMultipartAsync($endpoint, array $params = [], $return_response_object = false) |
43
|
|
|
{ |
44
|
|
|
yield CoInterface::RETURN_WITH => (yield CurlExecutor::execDecodedAsync($this->getInternalCurl()->postMultipart($endpoint, $params), $return_response_object)); |
45
|
1 |
|
// @codeCoverageIgnoreStart |
46
|
1 |
|
} |
47
|
1 |
|
// @codeCoverageIgnoreEnd |
48
|
|
|
|
49
|
|
|
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
|
6 |
|
} |
53
|
6 |
|
|
54
|
|
|
public function post($endpoint, array $params = [], $return_response_object = false) |
55
|
6 |
|
{ |
56
|
5 |
|
return CurlExecutor::execDecoded($this->getInternalCurl()->post($endpoint, $params), $return_response_object); |
57
|
5 |
|
} |
58
|
1 |
|
|
59
|
|
|
public function delete($endpoint, array $params = [], $return_response_object = false) |
60
|
|
|
{ |
61
|
5 |
|
return CurlExecutor::execDecoded($this->getInternalCurl()->delete($endpoint, $params), $return_response_object); |
62
|
1 |
|
} |
63
|
|
|
|
64
|
4 |
|
public function put($endpoint, array $params = [], $return_response_object = false) |
65
|
|
|
{ |
66
|
9 |
|
return CurlExecutor::execDecoded($this->getInternalCurl()->put($endpoint, $params), $return_response_object); |
67
|
9 |
|
} |
68
|
9 |
|
|
69
|
9 |
|
public function postMultipart($endpoint, array $params = [], $return_response_object = false) |
70
|
9 |
|
{ |
71
|
7 |
|
return CurlExecutor::execDecoded($this->getInternalCurl()->postMultipart($endpoint, $params), $return_response_object); |
72
|
1 |
|
} |
73
|
|
|
|
74
|
6 |
|
public function streamingAsync($endpoint, callable $event_handler, array $params = [], callable $header_response_handler = null) |
75
|
1 |
|
{ |
76
|
|
|
$handler = new StreamHandler($header_response_handler, $event_handler); |
77
|
5 |
|
$ch = $this->getInternalCurl()->streaming($endpoint, $params, $handler); |
78
|
|
|
try { |
79
|
1 |
|
yield $ch; |
80
|
1 |
|
} catch (CURLException $e) { |
81
|
1 |
|
if (!$handler->isHaltedByUser()) { |
82
|
|
|
throw $e; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
if (!$handler->isHaltedByUser()) { |
86
|
1 |
|
throw new \UnexpectedValueException('Streaming stopped unexpectedly.'); |
87
|
1 |
|
} |
88
|
1 |
|
} |
89
|
|
|
|
90
|
|
|
public function streaming($endpoint, callable $event_handler, array $params = [], callable $header_response_handler = null) |
91
|
|
|
{ |
92
|
|
|
$handler = new StreamHandler($header_response_handler, $event_handler); |
93
|
1 |
|
$ch = $this->getInternalCurl()->streaming($endpoint, $params, $handler); |
94
|
1 |
|
$result = curl_exec($ch); |
95
|
1 |
|
if (!$handler->isHaltedByUser() && $result === false) { |
96
|
|
|
throw new CURLException(curl_error($ch), curl_errno($ch), $ch); |
97
|
|
|
} |
98
|
|
|
if (!$handler->isHaltedByUser()) { |
99
|
|
|
throw new \UnexpectedValueException('Streaming stopped unexpectedly.'); |
100
|
1 |
|
} |
101
|
1 |
|
} |
102
|
1 |
|
|
103
|
|
|
public function getOutAsync($endpoint, array $params = [], $return_response_object = false) |
104
|
|
|
{ |
105
|
1 |
|
yield CoInterface::RETURN_WITH => (yield CurlExecutor::execDecodedAsync($this->getInternalCurl()->getOut($endpoint, $params), $return_response_object)); |
106
|
1 |
|
// @codeCoverageIgnoreStart |
107
|
1 |
|
} |
108
|
|
|
// @codeCoverageIgnoreEnd |
109
|
|
|
|
110
|
1 |
|
public function postOutAsync($endpoint, array $params = [], $return_response_object = false) |
111
|
1 |
|
{ |
112
|
1 |
|
yield CoInterface::RETURN_WITH => (yield CurlExecutor::execDecodedAsync($this->getInternalCurl()->postOut($endpoint, $params), $return_response_object)); |
113
|
|
|
// @codeCoverageIgnoreStart |
114
|
|
|
} |
115
|
|
|
// @codeCoverageIgnoreEnd |
116
|
|
|
|
117
|
|
|
public function postMultipartOutAsync($endpoint, array $params = [], $return_response_object = false) |
118
|
|
|
{ |
119
|
|
|
yield CoInterface::RETURN_WITH => (yield CurlExecutor::execDecodedAsync($this->getInternalCurl()->postMultipartOut($endpoint, $params), $return_response_object)); |
120
|
|
|
// @codeCoverageIgnoreStart |
121
|
|
|
} |
122
|
|
|
// @codeCoverageIgnoreEnd |
123
|
|
|
|
124
|
|
|
public function getOut($endpoint, array $params = [], $return_response_object = false) |
125
|
|
|
{ |
126
|
|
|
return CurlExecutor::execDecoded($this->getInternalCurl()->getOut($endpoint, $params), $return_response_object); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function postOut($endpoint, array $params = [], $return_response_object = false) |
130
|
|
|
{ |
131
|
|
|
return CurlExecutor::execDecoded($this->getInternalCurl()->postOut($endpoint, $params), $return_response_object); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function postMultipartOut($endpoint, array $params = [], $return_response_object = false) |
135
|
|
|
{ |
136
|
|
|
return CurlExecutor::execDecoded($this->getInternalCurl()->postMultipartOut($endpoint, $params), $return_response_object); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|