1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mpyw\Cowitter\Traits; |
4
|
|
|
|
5
|
|
|
use mpyw\Co\CURLException; |
6
|
|
|
use mpyw\Cowitter\Components\StreamHandler; |
7
|
|
|
use mpyw\Cowitter\Helpers\ResponseYielder; |
8
|
|
|
|
9
|
|
|
trait RequestorTrait |
10
|
|
|
{ |
11
|
|
|
abstract protected function getInternalCurl(); |
12
|
|
|
|
13
|
5 |
|
public function getAsync($endpoint, array $params = [], $return_response_object = false) |
14
|
5 |
|
{ |
15
|
5 |
|
return ResponseYielder::asyncExecDecoded($this->getInternalCurl()->get($endpoint, $params), $return_response_object); |
16
|
|
|
} |
17
|
|
|
|
18
|
8 |
|
public function postAsync($endpoint, array $params = [], $return_response_object = false) |
19
|
8 |
|
{ |
20
|
8 |
|
return ResponseYielder::asyncExecDecoded($this->getInternalCurl()->post($endpoint, $params), $return_response_object); |
21
|
|
|
} |
22
|
|
|
|
23
|
8 |
|
public function postMultipartAsync($endpoint, array $params = [], $return_response_object = false) |
24
|
8 |
|
{ |
25
|
8 |
|
return ResponseYielder::asyncExecDecoded($this->getInternalCurl()->postMultipart($endpoint, $params), $return_response_object); |
26
|
|
|
} |
27
|
|
|
|
28
|
6 |
|
public function get($endpoint, array $params = [], $return_response_object = false) |
29
|
6 |
|
{ |
30
|
6 |
|
return ResponseYielder::syncExecDecoded($this->getInternalCurl()->get($endpoint, $params), $return_response_object); |
31
|
|
|
} |
32
|
|
|
|
33
|
5 |
|
public function post($endpoint, array $params = [], $return_response_object = false) |
34
|
5 |
|
{ |
35
|
5 |
|
return ResponseYielder::syncExecDecoded($this->getInternalCurl()->post($endpoint, $params), $return_response_object); |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
public function postMultipart($endpoint, array $params = [], $return_response_object = false) |
39
|
1 |
|
{ |
40
|
1 |
|
return ResponseYielder::syncExecDecoded($this->getInternalCurl()->postMultipart($endpoint, $params), $return_response_object); |
41
|
|
|
} |
42
|
|
|
|
43
|
6 |
|
public function streamingAsync($endpoint, callable $event_handler, array $params = [], callable $header_response_handler = null) |
44
|
6 |
|
{ |
45
|
6 |
|
$handler = new StreamHandler($header_response_handler, $event_handler); |
46
|
6 |
|
$ch = $this->getInternalCurl()->streaming($endpoint, $params, $handler); |
47
|
|
|
try { |
48
|
6 |
|
yield $ch; |
49
|
5 |
|
} catch (CURLException $e) { |
50
|
5 |
|
if (!$handler->isHaltedByUser()) { |
51
|
1 |
|
throw $e; |
52
|
|
|
} |
53
|
|
|
} |
54
|
5 |
|
if (!$handler->isHaltedByUser()) { |
55
|
1 |
|
throw new \UnexpectedValueException('Streaming stopped unexpectedly.'); |
56
|
|
|
} |
57
|
4 |
|
} |
58
|
|
|
|
59
|
6 |
|
public function streaming($endpoint, callable $event_handler, array $params = [], callable $header_response_handler = null) |
60
|
6 |
|
{ |
61
|
6 |
|
$handler = new StreamHandler($header_response_handler, $event_handler); |
62
|
6 |
|
$ch = $this->getInternalCurl()->streaming($endpoint, $params, $handler); |
63
|
6 |
|
$result = curl_exec($ch); |
64
|
4 |
|
if (!$handler->isHaltedByUser() && $result === false) { |
65
|
1 |
|
throw new CURLException(curl_error($ch), curl_errno($ch), $ch); |
66
|
|
|
} |
67
|
3 |
|
if (!$handler->isHaltedByUser()) { |
68
|
1 |
|
throw new \UnexpectedValueException('Streaming stopped unexpectedly.'); |
69
|
|
|
} |
70
|
2 |
|
} |
71
|
|
|
|
72
|
1 |
|
public function getOutAsync($endpoint, array $params = [], $return_response_object = false) |
73
|
1 |
|
{ |
74
|
1 |
|
return ResponseYielder::asyncExecDecoded($this->getInternalCurl()->getOut($endpoint, $params), $return_response_object); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function postOutAsync($endpoint, array $params = [], $return_response_object = false) |
78
|
1 |
|
{ |
79
|
1 |
|
return ResponseYielder::asyncExecDecoded($this->getInternalCurl()->postOut($endpoint, $params), $return_response_object); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public function postMultipartOutAsync($endpoint, array $params = [], $return_response_object = false) |
83
|
1 |
|
{ |
84
|
1 |
|
return ResponseYielder::asyncExecDecoded($this->getInternalCurl()->postMultipartOut($endpoint, $params), $return_response_object); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
public function getOut($endpoint, array $params = [], $return_response_object = false) |
88
|
1 |
|
{ |
89
|
1 |
|
return ResponseYielder::syncExecDecoded($this->getInternalCurl()->getOut($endpoint, $params), $return_response_object); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
public function postOut($endpoint, array $params = [], $return_response_object = false) |
93
|
1 |
|
{ |
94
|
1 |
|
return ResponseYielder::syncExecDecoded($this->getInternalCurl()->postOut($endpoint, $params), $return_response_object); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
public function postMultipartOut($endpoint, array $params = [], $return_response_object = false) |
98
|
1 |
|
{ |
99
|
1 |
|
return ResponseYielder::syncExecDecoded($this->getInternalCurl()->postMultipartOut($endpoint, $params), $return_response_object); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|