mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore.git
synced 2026-01-15 03:45:06 +08:00
refactor(http): 整理了 Http 请求服务
Signed-off-by: 李东云 <dongyu.li@luxcreo.ai>
This commit is contained in:
2
.idea/HDK-Core.iml
generated
2
.idea/HDK-Core.iml
generated
@@ -2,8 +2,8 @@
|
|||||||
<module type="WEB_MODULE" version="4">
|
<module type="WEB_MODULE" version="4">
|
||||||
<component name="NewModuleRootManager">
|
<component name="NewModuleRootManager">
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
<sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
|
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" packagePrefix="Singularity\HDK\Core\" />
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" packagePrefix="Singularity\HDK\Core\" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" packagePrefix="Singularity\HDK\Test\Core\" />
|
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" packagePrefix="Singularity\HDK\Test\Core\" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/recursion-context" />
|
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/recursion-context" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/lines-of-code" />
|
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/lines-of-code" />
|
||||||
|
|||||||
12
.idea/codeception.xml
generated
Normal file
12
.idea/codeception.xml
generated
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Codeception">
|
||||||
|
<option name="configurations">
|
||||||
|
<list>
|
||||||
|
<Configuration>
|
||||||
|
<option name="path" value="$PROJECT_DIR$/tests" />
|
||||||
|
</Configuration>
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
28
.idea/phpspec.xml
generated
Normal file
28
.idea/phpspec.xml
generated
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="PHPSpec">
|
||||||
|
<suites>
|
||||||
|
<PhpSpecSuiteConfiguration>
|
||||||
|
<option name="myPath" value="$PROJECT_DIR$" />
|
||||||
|
</PhpSpecSuiteConfiguration>
|
||||||
|
<PhpSpecSuiteConfiguration>
|
||||||
|
<option name="myPath" value="$PROJECT_DIR$" />
|
||||||
|
</PhpSpecSuiteConfiguration>
|
||||||
|
<PhpSpecSuiteConfiguration>
|
||||||
|
<option name="myPath" value="$PROJECT_DIR$" />
|
||||||
|
</PhpSpecSuiteConfiguration>
|
||||||
|
<PhpSpecSuiteConfiguration>
|
||||||
|
<option name="myPath" value="$PROJECT_DIR$" />
|
||||||
|
</PhpSpecSuiteConfiguration>
|
||||||
|
<PhpSpecSuiteConfiguration>
|
||||||
|
<option name="myPath" value="$PROJECT_DIR$" />
|
||||||
|
</PhpSpecSuiteConfiguration>
|
||||||
|
<PhpSpecSuiteConfiguration>
|
||||||
|
<option name="myPath" value="$PROJECT_DIR$" />
|
||||||
|
</PhpSpecSuiteConfiguration>
|
||||||
|
<PhpSpecSuiteConfiguration>
|
||||||
|
<option name="myPath" value="$PROJECT_DIR$" />
|
||||||
|
</PhpSpecSuiteConfiguration>
|
||||||
|
</suites>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
26
docs/Http 请求.md
Normal file
26
docs/Http 请求.md
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
# Http 请求
|
||||||
|
|
||||||
|
## 1 组成
|
||||||
|
|
||||||
|
* 单例类
|
||||||
|
* \Singularity\HDK\Core\Http\RequestService
|
||||||
|
* 工厂类
|
||||||
|
* \Singularity\HDK\Core\Http\RequestServiceFactory
|
||||||
|
|
||||||
|
## 2 初始化服务
|
||||||
|
|
||||||
|
创建一个请求服务的方法有两种,效果是一致的:
|
||||||
|
|
||||||
|
* 工厂模式
|
||||||
|
* `\Singularity\HDK\Core\Http\RequestServiceFactory::make($options)`
|
||||||
|
* `\Singularity\HDK\Core\Http\RequestServiceFactory::make()->setOptions($options)`
|
||||||
|
* 单例模式
|
||||||
|
* `new \Singularity\HDK\Core\Http\RequestService($options)`
|
||||||
|
* `(new \Singularity\HDK\Core\Http\RequestService())->setOptions($options)`
|
||||||
|
|
||||||
|
这两个方法都回返回一个 `\Singularity\HDK\Core\Http\RequestService` 实例
|
||||||
|
|
||||||
|
## 3 规范
|
||||||
|
|
||||||
|
每个通过本包提供的 Http 请求服务创建的服务,都应该在构建方法中实例化一个 RequestService 服务,
|
||||||
|
之后该服务中使用的所有请求都基于该服务,这样可以避免共用单例模式导致的 options 污染
|
||||||
190
src/Http/RequestService.php
Normal file
190
src/Http/RequestService.php
Normal file
@@ -0,0 +1,190 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Singularity\HDK\Core\Http;
|
||||||
|
|
||||||
|
use Ergebnis\Http\Method;
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
|
use GuzzleHttp\Psr7\Request;
|
||||||
|
use GuzzleHttp\Psr7\Utils;
|
||||||
|
use Hyperf\Codec\Json;
|
||||||
|
use Hyperf\Di\Annotation\Inject;
|
||||||
|
use Hyperf\Guzzle\ClientFactory;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Singularity\HDK\Core\Service\AbstractSingleton;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
#[Inject]
|
||||||
|
private ClientFactory $client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Options
|
||||||
|
*/
|
||||||
|
private array $options = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Options $options
|
||||||
|
*/
|
||||||
|
public function __construct(array $options = [])
|
||||||
|
{
|
||||||
|
$this->setOptions($options);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 定制 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 $data
|
||||||
|
* @param Options $options
|
||||||
|
* @return ResponseInterface
|
||||||
|
* @throws GuzzleException
|
||||||
|
*/
|
||||||
|
public function requestGet(
|
||||||
|
string $url,
|
||||||
|
array $params = [],
|
||||||
|
array $data = [],
|
||||||
|
array $options = []
|
||||||
|
): ResponseInterface {
|
||||||
|
$request = new Request(
|
||||||
|
method: Method\Rfc\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: Method\Rfc\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: Method\Rfc\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: Method\Rfc\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: Method\Rfc\Rfc7231::DELETE,
|
||||||
|
uri: $url,
|
||||||
|
headers: ['Content-Type' => 'application/json'],
|
||||||
|
body: Utils::streamFor($data)
|
||||||
|
);
|
||||||
|
return $this->getClient($options)->send(
|
||||||
|
$request, ['query' => $params,]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
22
src/Http/RequestServiceFactory.php
Normal file
22
src/Http/RequestServiceFactory.php
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
/**
|
||||||
|
* RequestFactory.php@Core
|
||||||
|
*
|
||||||
|
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||||
|
* Powered by PhpStorm
|
||||||
|
* Created on 2023/8/30
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Singularity\HDK\Core\Http;
|
||||||
|
|
||||||
|
use Singularity\HDK\Core\Service\AbstractFactory;
|
||||||
|
|
||||||
|
final class RequestServiceFactory extends AbstractFactory
|
||||||
|
{
|
||||||
|
public static function make(array $options = []): RequestService
|
||||||
|
{
|
||||||
|
return new RequestService($options);
|
||||||
|
}
|
||||||
|
}
|
||||||
30
src/Service/AbstractFactory.php
Normal file
30
src/Service/AbstractFactory.php
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
/**
|
||||||
|
* AbstractFactory.php@Core
|
||||||
|
*
|
||||||
|
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||||
|
* Powered by PhpStorm
|
||||||
|
* Created on 2023/8/30
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Singularity\HDK\Core\Service;
|
||||||
|
|
||||||
|
use Dont\DontCall;
|
||||||
|
use Dont\DontInstantiate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singularity\HDK\Core\Service\AbstractFactory@Core
|
||||||
|
*
|
||||||
|
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||||
|
* Powered by PhpStorm
|
||||||
|
* Created on 2023/8/30
|
||||||
|
*/
|
||||||
|
abstract class AbstractFactory
|
||||||
|
{
|
||||||
|
use DontInstantiate;
|
||||||
|
use DontCall;
|
||||||
|
|
||||||
|
abstract static function make();
|
||||||
|
}
|
||||||
26
src/Service/AbstractSingleton.php
Normal file
26
src/Service/AbstractSingleton.php
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* SingletonInterface.php@Core
|
||||||
|
*
|
||||||
|
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||||
|
* Powered by PhpStorm
|
||||||
|
* Created on 2023/8/30
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Singularity\HDK\Core\Service;
|
||||||
|
|
||||||
|
use Dont\DontCallStatic;
|
||||||
|
use Dont\DontClone;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singularity\HDK\Core\Service\SingletonInterface@Core
|
||||||
|
*
|
||||||
|
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||||
|
* Powered by PhpStorm
|
||||||
|
* Created on 2023/8/30
|
||||||
|
*/
|
||||||
|
abstract class AbstractSingleton
|
||||||
|
{
|
||||||
|
use DontCallStatic;
|
||||||
|
use DontClone;
|
||||||
|
}
|
||||||
@@ -16,6 +16,7 @@ use Psr\Http\Message\ResponseInterface;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 发起 Http 请求的类
|
* 发起 Http 请求的类
|
||||||
|
* @deprecated since version 1.0-alpha.3, to be removed in 1.0
|
||||||
*/
|
*/
|
||||||
class HttpRequestService
|
class HttpRequestService
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user