2
2
3
3
namespace ZhenMu \Support \Traits ;
4
4
5
+
6
+ use GuzzleHttp \Client ;
7
+ use GuzzleHttp \Promise \Utils ;
8
+ use GuzzleHttp \Psr7 \Response ;
9
+ use GuzzleHttp \Promise \Promise ;
10
+ use Psr \Http \Message \ResponseInterface ;
11
+ use Illuminate \Pagination \LengthAwarePaginator ;
12
+
5
13
trait Clientable
6
14
{
7
15
use Arrayable;
8
16
9
- /** @var \GuzzleHttp\Psr7\ Response */
17
+ /** @var Response */
10
18
protected $ response ;
11
19
12
20
protected array $ result = [];
13
21
14
- public static function make (): static |\ GuzzleHttp \ Promise \ Utils |\ GuzzleHttp \ Client
22
+ public static function make (): static |Utils |Client
15
23
{
16
24
return new static ();
17
25
}
@@ -32,10 +40,10 @@ public function getOptions()
32
40
33
41
public function getHttpClient ()
34
42
{
35
- return new \ GuzzleHttp \ Client ($ this ->getOptions ());
43
+ return new Client ($ this ->getOptions ());
36
44
}
37
45
38
- abstract public function handleEmptyResponse (?string $ content = null , ?\ Psr \ Http \ Message \ ResponseInterface $ response = null );
46
+ abstract public function handleEmptyResponse (?string $ content = null , ?ResponseInterface $ response = null );
39
47
40
48
abstract public function isErrorResponse (array $ data ): bool ;
41
49
@@ -74,7 +82,7 @@ public function paginate()
74
82
return null ;
75
83
}
76
84
77
- $ paginate = new \ Illuminate \ Pagination \ LengthAwarePaginator (
85
+ $ paginate = new LengthAwarePaginator (
78
86
items: $ this ->getDataList (),
79
87
total: $ this ->getTotal (),
80
88
perPage: $ this ->getPageSize (),
@@ -91,8 +99,8 @@ public function paginate()
91
99
public function __call ($ method , $ args )
92
100
{
93
101
// 异步请求处理
94
- if (method_exists (\ GuzzleHttp \ Promise \ Utils::class, $ method )) {
95
- $ results = call_user_func_array ([\ GuzzleHttp \ Promise \ Utils::class, $ method ], $ args );
102
+ if (method_exists (Utils::class, $ method )) {
103
+ $ results = call_user_func_array ([Utils::class, $ method ], $ args );
96
104
97
105
if (!is_array ($ results )) {
98
106
return $ results ;
@@ -114,14 +122,14 @@ public function __call($method, $args)
114
122
}
115
123
116
124
// 响应结果处理
117
- if ($ this ->response instanceof \ GuzzleHttp \ Psr7 \ Response) {
125
+ if ($ this ->response instanceof Response) {
118
126
$ this ->result = $ this ->castResponse ($ this ->response );
119
127
120
128
$ this ->setAttributes ($ this ->result );
121
129
}
122
130
123
131
// 将 promise 请求直接返回
124
- if ($ this ->response instanceof \ GuzzleHttp \ Promise \ Promise) {
132
+ if ($ this ->response instanceof Promise) {
125
133
return $ this ->response ;
126
134
}
127
135
0 commit comments