{guzzle-services} 如何将中间件与 GuzzleClient 客户端一起使用,而不是直接与原始 GuzzleHttp\Client 一起使用?
{guzzle-services} How to use middlewares with GuzzleClient client AS OPPOSED TO directly with raw GuzzleHttp\Client?
我的中间件需要:
- 向来自
GuzzleHttp\Command\Guzzle\GuzzleClient
的 REST API 客户端发出的请求添加一个额外的查询参数
我无法在通过客户端调用 API 时直接执行此操作,因为 GuzzleClient
使用 API 规范并且它仅传递 "legal" 查询参数。因此,我必须安装一个中间件来拦截 HTTP 请求 API 客户端准备它们。
我目前所在的曲目:
$apiClient->getHandlerStack()-push($myMiddleware)
问题:
我想不出assemble $myMiddleware
必须具备的功能性俄罗斯娃娃的正确方法。这是一个疯狂 gazilliardth-order 函数场景,完全正确 编写函数的方式似乎不同 从直接使用 GuzzleHttp\Client
时广泛记录的做事方式。无论我尝试什么,我最终都会将错误的东西传递到俄罗斯套娃的某个层,导致参数类型错误,或者我最终从某个层返回错误的东西,导致 Guzzle 代码中出现类型错误。
我做了一个慎重的决定,放弃尝试理解。请给我一个样板解决方案 for GuzzleHttp\Command\Guzzle\GuzzleClient
,而不是 GuzzleHttp\Client
.
GuzzleHttp\Command\Guzzle\GuzzleClient
中用来处理中间件的HandlerStack
既可以transform/validate序列化前的命令,也可以处理返回后的结果。如果你想在命令变成请求之后修改命令,但在实际发送之前,那么你可以使用与不使用 GuzzleClient
相同的中间件方法 - 创建并附加中间件到作为第一个参数传递给 GuzzleClient
.
的 GuzzleHttp\Client
实例
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\Guzzle\Description;
class MyCustomMiddleware
{
public function __invoke(callable $handler) {
return function (RequestInterface $request, array $options) use ($handler) {
// ... do something with request
return $handler($request, $options);
}
}
}
$handlerStack = HandlerStack::create();
$handlerStack->push(new MyCustomMiddleware);
$config['handler'] = $handlerStack;
$apiClient = new GuzzleClient(new Client($config), new Description(...));
GuzzleClient
的样板解决方案与 GuzzleHttp\Client
的样板解决方案相同,因为无论是否使用 Guzzle 服务,您的请求修改中间件都需要继续 GuzzleHttp\Client
。
您也可以使用
$handler->push(Middleware::mapRequest(function(){...});
各种操纵请求。我不是 100% 确定这就是您要找的东西。但我假设您可以将额外的参数添加到那里的 Request 中。
private function createAuthStack()
{
$stack = HandlerStack::create();
$stack->push(Middleware::mapRequest(function (RequestInterface $request) {
return $request->withHeader('Authorization', "Bearer " . $this->accessToken);
}));
return $stack;
}
我的中间件需要:
- 向来自
GuzzleHttp\Command\Guzzle\GuzzleClient
的 REST API 客户端发出的请求添加一个额外的查询参数
我无法在通过客户端调用 API 时直接执行此操作,因为 GuzzleClient
使用 API 规范并且它仅传递 "legal" 查询参数。因此,我必须安装一个中间件来拦截 HTTP 请求 API 客户端准备它们。
我目前所在的曲目:
$apiClient->getHandlerStack()-push($myMiddleware)
问题:
我想不出assemble $myMiddleware
必须具备的功能性俄罗斯娃娃的正确方法。这是一个疯狂 gazilliardth-order 函数场景,完全正确 编写函数的方式似乎不同 从直接使用 GuzzleHttp\Client
时广泛记录的做事方式。无论我尝试什么,我最终都会将错误的东西传递到俄罗斯套娃的某个层,导致参数类型错误,或者我最终从某个层返回错误的东西,导致 Guzzle 代码中出现类型错误。
我做了一个慎重的决定,放弃尝试理解。请给我一个样板解决方案 for GuzzleHttp\Command\Guzzle\GuzzleClient
,而不是 GuzzleHttp\Client
.
GuzzleHttp\Command\Guzzle\GuzzleClient
中用来处理中间件的HandlerStack
既可以transform/validate序列化前的命令,也可以处理返回后的结果。如果你想在命令变成请求之后修改命令,但在实际发送之前,那么你可以使用与不使用 GuzzleClient
相同的中间件方法 - 创建并附加中间件到作为第一个参数传递给 GuzzleClient
.
GuzzleHttp\Client
实例
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\Guzzle\Description;
class MyCustomMiddleware
{
public function __invoke(callable $handler) {
return function (RequestInterface $request, array $options) use ($handler) {
// ... do something with request
return $handler($request, $options);
}
}
}
$handlerStack = HandlerStack::create();
$handlerStack->push(new MyCustomMiddleware);
$config['handler'] = $handlerStack;
$apiClient = new GuzzleClient(new Client($config), new Description(...));
GuzzleClient
的样板解决方案与 GuzzleHttp\Client
的样板解决方案相同,因为无论是否使用 Guzzle 服务,您的请求修改中间件都需要继续 GuzzleHttp\Client
。
您也可以使用
$handler->push(Middleware::mapRequest(function(){...});
各种操纵请求。我不是 100% 确定这就是您要找的东西。但我假设您可以将额外的参数添加到那里的 Request 中。
private function createAuthStack()
{
$stack = HandlerStack::create();
$stack->push(Middleware::mapRequest(function (RequestInterface $request) {
return $request->withHeader('Authorization', "Bearer " . $this->accessToken);
}));
return $stack;
}