22
33namespace ZhenMu \Support \Traits ;
44
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+
513trait Clientable
614{
715 use Arrayable;
816
9- /** @var \GuzzleHttp\Psr7\ Response */
17+ /** @var Response */
1018 protected $ response ;
1119
1220 protected array $ result = [];
1321
14- public static function make (): static |\ GuzzleHttp \ Promise \ Utils |\ GuzzleHttp \ Client
22+ public static function make (): static |Utils |Client
1523 {
1624 return new static ();
1725 }
@@ -32,10 +40,10 @@ public function getOptions()
3240
3341 public function getHttpClient ()
3442 {
35- return new \ GuzzleHttp \ Client ($ this ->getOptions ());
43+ return new Client ($ this ->getOptions ());
3644 }
3745
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 );
3947
4048 abstract public function isErrorResponse (array $ data ): bool ;
4149
@@ -74,7 +82,7 @@ public function paginate()
7482 return null ;
7583 }
7684
77- $ paginate = new \ Illuminate \ Pagination \ LengthAwarePaginator (
85+ $ paginate = new LengthAwarePaginator (
7886 items: $ this ->getDataList (),
7987 total: $ this ->getTotal (),
8088 perPage: $ this ->getPageSize (),
@@ -91,8 +99,8 @@ public function paginate()
9199 public function __call ($ method , $ args )
92100 {
93101 // 异步请求处理
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 );
96104
97105 if (!is_array ($ results )) {
98106 return $ results ;
@@ -114,14 +122,14 @@ public function __call($method, $args)
114122 }
115123
116124 // 响应结果处理
117- if ($ this ->response instanceof \ GuzzleHttp \ Psr7 \ Response) {
125+ if ($ this ->response instanceof Response) {
118126 $ this ->result = $ this ->castResponse ($ this ->response );
119127
120128 $ this ->setAttributes ($ this ->result );
121129 }
122130
123131 // 将 promise 请求直接返回
124- if ($ this ->response instanceof \ GuzzleHttp \ Promise \ Promise) {
132+ if ($ this ->response instanceof Promise) {
125133 return $ this ->response ;
126134 }
127135
0 commit comments