mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore.git
synced 2026-01-15 07:15:06 +08:00
208 lines
5.4 KiB
PHP
208 lines
5.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Singularity\HDK\Core\Http;
|
|
|
|
use Ergebnis\Http\Method\Rfc\Rfc5789;
|
|
use Ergebnis\Http\Method\Rfc\Rfc7231;
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
use GuzzleHttp\Psr7\Request;
|
|
use GuzzleHttp\Psr7\Utils;
|
|
use Hyperf\Codec\Json;
|
|
use Hyperf\Guzzle\ClientFactory;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Singularity\HDK\Core\Service\AbstractSingleton;
|
|
|
|
use function Hyperf\Support\make;
|
|
|
|
/**
|
|
* Singularity\HDK\Core\Service\Http\RequestService@Core
|
|
*
|
|
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
|
* Powered by PhpStorm
|
|
* Created on 2023/8/30
|
|
*
|
|
* @template Options of array<literal-string, mixed>
|
|
*/
|
|
final class RequestService extends AbstractSingleton
|
|
{
|
|
// public const TIMEOUT = 20;
|
|
|
|
private ClientFactory $client;
|
|
|
|
/**
|
|
* @var Options
|
|
*/
|
|
private array $options = [];
|
|
|
|
/**
|
|
* @param Options $options
|
|
*/
|
|
public function __construct(array $options = [])
|
|
{
|
|
$this->setOptions($options);
|
|
/** @var ClientFactory $client */
|
|
$client = make(ClientFactory::class);
|
|
$this->client = $client;
|
|
}
|
|
|
|
/**
|
|
* 定制 options
|
|
*
|
|
* @param Options $options
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function setOptions(array $options): self
|
|
{
|
|
$this->options = array_replace_recursive($this->options, $options);
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @param string $url
|
|
* @param array<string, string|int> $params
|
|
* @param array<string, mixed> $data
|
|
* @param Options $options
|
|
*
|
|
* @return ResponseInterface
|
|
* @throws GuzzleException
|
|
*/
|
|
public function requestGet(
|
|
string $url,
|
|
array $params = [],
|
|
array $data = [],
|
|
array $options = []
|
|
): ResponseInterface {
|
|
$request = new Request(
|
|
method: Rfc7231::GET,
|
|
uri: $url
|
|
);
|
|
return $this->getClient($options)->send($request, ['query' => $params, 'json' => $data,]);
|
|
}
|
|
|
|
/**
|
|
* 工厂模式初始化请求
|
|
*
|
|
* @param Options $options
|
|
*
|
|
* @return Client
|
|
*/
|
|
private function getClient(array $options): Client
|
|
{
|
|
$options = array_replace_recursive($this->options, $options);
|
|
return $this->client->create($options);
|
|
}
|
|
|
|
/**
|
|
* @param string $url
|
|
* @param array<string, string|int> $params
|
|
* @param array<string, mixed> $data
|
|
* @param Options $options
|
|
*
|
|
* @return ResponseInterface
|
|
* @throws GuzzleException
|
|
*/
|
|
public function requestPost(
|
|
string $url,
|
|
array $params = [],
|
|
array $data = [],
|
|
array $options = []
|
|
): ResponseInterface {
|
|
$data = Json::encode($data);
|
|
$request = new Request(
|
|
method: Rfc7231::POST,
|
|
uri: $url,
|
|
headers: ['Content-Type' => 'application/json'],
|
|
body: Utils::streamFor($data),
|
|
);
|
|
return $this->getClient($options)->send($request, ['query' => $params,]);
|
|
}
|
|
|
|
/**
|
|
* @param string $url
|
|
* @param array<string, mixed> $data
|
|
* @param array<string, string|int> $params
|
|
* @param Options $options
|
|
*
|
|
* @return ResponseInterface
|
|
* @throws GuzzleException
|
|
*/
|
|
public function requestPut(
|
|
string $url,
|
|
array $params = [],
|
|
array $data = [],
|
|
array $options = []
|
|
): ResponseInterface {
|
|
$data = Json::encode($data);
|
|
$request = new Request(
|
|
method: Rfc7231::PUT,
|
|
uri: $url,
|
|
headers: ['Content-Type' => 'application/json'],
|
|
body: Utils::streamFor($data)
|
|
);
|
|
return $this->getClient($options)->send(
|
|
$request,
|
|
['query' => $params,]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param string $url
|
|
* @param array<string, mixed> $data
|
|
* @param array<string, string|int> $params
|
|
* @param Options $options
|
|
*
|
|
* @return ResponseInterface
|
|
* @throws GuzzleException
|
|
*/
|
|
public function requestPatch(
|
|
string $url,
|
|
array $params = [],
|
|
array $data = [],
|
|
array $options = []
|
|
): ResponseInterface {
|
|
$data = Json::encode($data);
|
|
$request = new Request(
|
|
method: Rfc5789::PATCH,
|
|
uri: $url,
|
|
headers: ['Content-Type' => 'application/json'],
|
|
body: Utils::streamFor($data)
|
|
);
|
|
return $this->getClient($options)->send(
|
|
$request,
|
|
['query' => $params,]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param string $url
|
|
* @param array<string, mixed> $data
|
|
* @param array<string, string|int> $params
|
|
* @param Options $options
|
|
*
|
|
* @return ResponseInterface
|
|
* @throws GuzzleException
|
|
*/
|
|
public function requestDelete(
|
|
string $url,
|
|
array $params = [],
|
|
array $data = [],
|
|
array $options = []
|
|
): ResponseInterface {
|
|
$data = Json::encode($data);
|
|
$request = new Request(
|
|
method: Rfc7231::DELETE,
|
|
uri: $url,
|
|
headers: ['Content-Type' => 'application/json'],
|
|
body: Utils::streamFor($data)
|
|
);
|
|
return $this->getClient($options)->send(
|
|
$request,
|
|
['query' => $params,]
|
|
);
|
|
}
|
|
}
|