Compare commits

...

1 Commits

Author SHA1 Message Date
as2252258 70b4400f95 改名 2021-11-18 10:46:24 +08:00
4 changed files with 128 additions and 94 deletions
+46 -24
View File
@@ -48,11 +48,41 @@ abstract class ClientAbstracts implements IClient
private int $port = 80; private int $port = 80;
/**
* @var ResponseInterface|null
*/
protected ?ResponseInterface $body;
private ?StreamInterface $_data = null; private ?StreamInterface $_data = null;
private int $connect_timeout = 1; private int $connect_timeout = 1;
protected mixed $client;
/**
* @return string|ResponseInterface|null
*/
public function getBody(): string|ResponseInterface|null
{
if ($this->body instanceof ResponseInterface) {
return $this->body->getBody()->getContents();
}
return $this->body;
}
/**
* @param ResponseInterface|null $body
*/
public function setBody(?ResponseInterface $body): void
{
$this->body = $body;
}
/** /**
* @param $host * @param $host
* @param $port * @param $port
@@ -67,22 +97,20 @@ abstract class ClientAbstracts implements IClient
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function post(string $path, array $params = []): ResponseInterface public function post(string $path, array $params = []): void
{ {
return $this->request(self::POST, $path, $params); $this->request(self::POST, $path, $params);
} }
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function put(string $path, array $params = []): ResponseInterface public function put(string $path, array $params = []): void
{ {
return $this->request(self::PUT, $path, $params); $this->request(self::PUT, $path, $params);
} }
@@ -100,63 +128,57 @@ abstract class ClientAbstracts implements IClient
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function head(string $path, array $params = []): ResponseInterface public function head(string $path, array $params = []): void
{ {
return $this->request(self::HEAD, $path, $params); $this->request(self::HEAD, $path, $params);
} }
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function get(string $path, array $params = []): ResponseInterface public function get(string $path, array $params = []): void
{ {
return $this->request(self::GET, $path, $params); $this->request(self::GET, $path, $params);
} }
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function option(string $path, array $params = []): ResponseInterface public function option(string $path, array $params = []): void
{ {
return $this->request(self::OPTIONS, $path, $params); $this->request(self::OPTIONS, $path, $params);
} }
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function delete(string $path, array $params = []): ResponseInterface public function delete(string $path, array $params = []): void
{ {
return $this->request(self::DELETE, $path, $params); $this->request(self::DELETE, $path, $params);
} }
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function options(string $path, array $params = []): ResponseInterface public function options(string $path, array $params = []): void
{ {
return $this->request(self::OPTIONS, $path, $params); $this->request(self::OPTIONS, $path, $params);
} }
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function upload(string $path, array $params = []): ResponseInterface public function upload(string $path, array $params = []): void
{ {
return $this->request(self::UPLOAD, $path, $params); $this->request(self::UPLOAD, $path, $params);
} }
+32 -27
View File
@@ -15,7 +15,6 @@ use Http\Message\Stream;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Logger; use Kiri\Abstracts\Logger;
use Kiri\Kiri; use Kiri\Kiri;
use Psr\Http\Message\ResponseInterface;
use Swoole\Coroutine\Http\Client as SwowClient; use Swoole\Coroutine\Http\Client as SwowClient;
/** /**
@@ -29,12 +28,12 @@ class CoroutineClient extends ClientAbstracts
* @param string $method * @param string $method
* @param $path * @param $path
* @param array $params * @param array $params
* @return ResponseInterface * @return void
* @throws Exception * @throws Exception
*/ */
public function request(string $method, $path, array $params = []): ResponseInterface public function request(string $method, $path, array $params = []): void
{ {
return $this->withMethod($method) $this->withMethod($method)
->coroutine( ->coroutine(
$this->matchHost($path), $this->matchHost($path),
$this->paramEncode($params) $this->paramEncode($params)
@@ -45,23 +44,24 @@ class CoroutineClient extends ClientAbstracts
/** /**
* @param $url * @param $url
* @param array|string $data * @param array|string $data
* @return ResponseInterface
* @throws Exception 使用swoole协程方式请求 * @throws Exception 使用swoole协程方式请求
*/ */
private function coroutine($url, array|string $data = []): ResponseInterface private function coroutine($url, array|string $data = []): void
{ {
try { try {
$client = $this->generate_client($data, ...$url); $this->generate_client($data, ...$url);
if ($client->statusCode < 0) { if ($this->client->statusCode < 0) {
throw new Exception($client->errMsg); throw new Exception($this->client->errMsg);
} }
return (new Response())->withStatus($client->getStatusCode()) $body = (new Response())->withStatus($this->client->getStatusCode())
->withHeaders($client->getHeaders()) ->withHeaders($this->client->getHeaders())
->withBody(new Stream($client->getBody())); ->withBody(new Stream($this->client->getBody()));
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]); Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]);
return (new Response())->withStatus(-1)->withHeaders([]) $body = (new Response())->withStatus(-1)->withHeaders([])
->withBody(new Stream(jTraceEx($exception))); ->withBody(new Stream(jTraceEx($exception)));
} finally {
$this->setBody($body);
} }
} }
@@ -71,50 +71,55 @@ class CoroutineClient extends ClientAbstracts
* @param $host * @param $host
* @param $isHttps * @param $isHttps
* @param $path * @param $path
* @return SwowClient
*/ */
private function generate_client($data, $host, $isHttps, $path): SwowClient private function generate_client($data, $host, $isHttps, $path): void
{ {
if ($isHttps || $this->isSSL()) { if ($isHttps || $this->isSSL()) {
$client = new SwowClient($host, 443, true); $this->client = new SwowClient($host, 443, true);
} else { } else {
$client = new SwowClient($host, $this->getPort(), false); $this->client = new SwowClient($host, $this->getPort(), false);
} }
$client->set($this->settings()); $this->client->set($this->settings());
if (!empty($this->getAgent())) { if (!empty($this->getAgent())) {
$this->withAddedHeader('User-Agent', $this->getAgent()); $this->withAddedHeader('User-Agent', $this->getAgent());
} }
$client->setHeaders($this->getHeader()); $this->client->setHeaders($this->getHeader());
$client->setMethod(strtoupper($this->getMethod())); $this->client->setMethod(strtoupper($this->getMethod()));
$client->execute($this->setParams($client, $path, $data)); $this->client->execute($this->setParams($path, $data));
$client->close();
return $client;
} }
/** /**
* @param SwowClient $client
* @param $path * @param $path
* @param $data * @param $data
* @return string * @return string
*/ */
private function setParams(SwowClient $client, $path, $data): string private function setParams($path, $data): string
{ {
$content = $this->getData()->getContents(); $content = $this->getData()->getContents();
if (!empty($content)) { if (!empty($content)) {
$client->setData($content); $this->client->setData($content);
} }
if ($this->isGet()) { if ($this->isGet()) {
if (!empty($data)) $path .= '?' . $data; if (!empty($data)) $path .= '?' . $data;
} else { } else {
$data = $this->mergeParams($data); $data = $this->mergeParams($data);
if (!empty($data)) { if (!empty($data)) {
$client->setData($data); $this->client->setData($data);
} }
} }
return $path; return $path;
} }
/**
*
*/
public function close(): void
{
$this->client->close();
}
/** /**
* @return array * @return array
*/ */
+30 -27
View File
@@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Http\Client; namespace Http\Client;
use CurlHandle;
use Exception; use Exception;
use Http\Message\Response; use Http\Message\Response;
use Http\Message\Stream; use Http\Message\Stream;
@@ -23,15 +22,17 @@ class Curl extends ClientAbstracts
* @param $method * @param $method
* @param $path * @param $path
* @param array $params * @param array $params
* @return ResponseInterface
* @throws Exception * @throws Exception
*/ */
public function request($method, $path, array $params = []): ResponseInterface public function request($method, $path, array $params = []): void
{ {
if ($method == self::GET) { if ($method == self::GET) {
$path = $this->joinGetParams($path, $params); $path = $this->joinGetParams($path, $params);
} }
return $this->execute($this->getCurlHandler($path, $method, $params));
$this->getCurlHandler($path, $method, $params);
$this->execute();
} }
@@ -39,10 +40,9 @@ class Curl extends ClientAbstracts
* @param $path * @param $path
* @param $method * @param $method
* @param $params * @param $params
* @return CurlHandle
* @throws Exception * @throws Exception
*/ */
private function getCurlHandler($path, $method, $params): CurlHandle private function getCurlHandler($path, $method, $params): void
{ {
[$host, $isHttps, $path] = $this->matchHost($path); [$host, $isHttps, $path] = $this->matchHost($path);
@@ -51,45 +51,43 @@ class Curl extends ClientAbstracts
$host .= ':' . $this->getPort(); $host .= ':' . $this->getPort();
} }
$resource = $this->do(curl_init($host . $path), $host . $path, $method); $this->do(curl_init($host . $path), $host . $path, $method);
if ($isHttps !== false) { if ($isHttps !== false) {
$this->curlHandlerSslSet($resource); $this->curlHandlerSslSet();
} }
$contents = $this->getData()->getContents(); $contents = $this->getData()->getContents();
if (empty($params) && empty($contents)) { if (empty($params) && empty($contents)) {
return $resource; return;
} }
if (!empty($contents)) { if (!empty($contents)) {
curl_setopt($resource, CURLOPT_POSTFIELDS, $contents); curl_setopt($this->client, CURLOPT_POSTFIELDS, $contents);
} else if ($method === self::POST) { } else if ($method === self::POST) {
curl_setopt($resource, CURLOPT_POSTFIELDS, $this->mergeParams($params)); curl_setopt($this->client, CURLOPT_POSTFIELDS, $this->mergeParams($params));
} else if ($method === self::UPLOAD) { } else if ($method === self::UPLOAD) {
curl_setopt($resource, CURLOPT_POSTFIELDS, $params); curl_setopt($this->client, CURLOPT_POSTFIELDS, $params);
} }
return $resource;
} }
/** /**
* @param $resource
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
private function curlHandlerSslSet($resource): void private function curlHandlerSslSet(): void
{ {
if (!empty($this->ssl_key)) { if (!empty($this->ssl_key)) {
if (!file_exists($this->ssl_key)) { if (!file_exists($this->ssl_key)) {
throw new Exception('SSL protocol certificate not found.'); throw new Exception('SSL protocol certificate not found.');
} }
curl_setopt($resource, CURLOPT_SSLKEY, $this->getSslKeyFile()); curl_setopt($this->client, CURLOPT_SSLKEY, $this->getSslKeyFile());
} }
if (!empty($this->ssl_cert)) { if (!empty($this->ssl_cert)) {
if (!!file_exists($this->ssl_cert)) { if (!!file_exists($this->ssl_cert)) {
throw new Exception('SSL protocol certificate not found.'); throw new Exception('SSL protocol certificate not found.');
} }
curl_setopt($resource, CURLOPT_SSLCERT, $this->getSslCertFile()); curl_setopt($this->client, CURLOPT_SSLCERT, $this->getSslCertFile());
} }
} }
@@ -98,10 +96,9 @@ class Curl extends ClientAbstracts
* @param $resource * @param $resource
* @param $path * @param $path
* @param $method * @param $method
* @return CurlHandle
* @throws Exception * @throws Exception
*/ */
private function do($resource, $path, $method): CurlHandle private function do($resource, $path, $method): void
{ {
curl_setopt($resource, CURLOPT_URL, $path); curl_setopt($resource, CURLOPT_URL, $path);
curl_setopt($resource, CURLOPT_TIMEOUT, $this->getTimeout()); // 超时设置 curl_setopt($resource, CURLOPT_TIMEOUT, $this->getTimeout()); // 超时设置
@@ -130,25 +127,31 @@ class Curl extends ClientAbstracts
} }
curl_setopt($resource, CURLOPT_CUSTOMREQUEST, strtoupper($method)); curl_setopt($resource, CURLOPT_CUSTOMREQUEST, strtoupper($method));
return $resource; $this->client = $resource;
} }
/** /**
* @param $curl
* @return ResponseInterface
* @throws Exception * @throws Exception
*/ */
private function execute($curl): ResponseInterface private function execute(): void
{ {
$output = curl_exec($curl); $output = curl_exec($this->client);
curl_close($curl);
if ($output === false) { if ($output === false) {
$response = (new Response())->withStatus(400)->withBody(new Stream(curl_error($curl))); $response = (new Response())->withStatus(400)->withBody(new Stream(curl_error($this->client)));
} else { } else {
$response = $this->explode($output); $response = $this->explode($output);
} }
return $response; $this->setBody($response);
}
/**
*
*/
public function close(): void
{
curl_close($this->client);
} }
+20 -16
View File
@@ -15,66 +15,64 @@ interface IClient
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function get(string $path, array $params = []): ResponseInterface; public function get(string $path, array $params = []): void;
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function post(string $path, array $params = []): ResponseInterface; public function post(string $path, array $params = []): void;
/**
*
*/
public function close(): void;
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function delete(string $path, array $params = []): ResponseInterface; public function delete(string $path, array $params = []): void;
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function options(string $path, array $params = []): ResponseInterface; public function options(string $path, array $params = []): void;
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function upload(string $path, array $params = []): ResponseInterface; public function upload(string $path, array $params = []): void;
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function put(string $path, array $params = []): ResponseInterface; public function put(string $path, array $params = []): void;
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function head(string $path, array $params = []): ResponseInterface; public function head(string $path, array $params = []): void;
/** /**
* @param string $method * @param string $method
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function request(string $method, string $path, array $params = []): ResponseInterface; public function request(string $method, string $path, array $params = []): void;
/** /**
@@ -174,4 +172,10 @@ interface IClient
* @return static * @return static
*/ */
public function withContentType(string $contentType): static; public function withContentType(string $contentType): static;
/**
* @return mixed
*/
public function getBody(): mixed;
} }