本文整理汇总了PHP中GuzzleHttp\Middleware::mapRequest方法的典型用法代码示例。如果您正苦于以下问题:PHP Middleware::mapRequest方法的具体用法?PHP Middleware::mapRequest怎么用?PHP Middleware::mapRequest使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\Middleware
的用法示例。
在下文中一共展示了Middleware::mapRequest方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getStackFunction
public function getStackFunction()
{
return Middleware::mapRequest(function (RequestInterface $request) {
// Notice that we have to return a request object
return $request->withHeader('X-Vault-Token', $this->_token);
});
}
示例2: sayHelloBeforeTheRequest
public function sayHelloBeforeTheRequest()
{
parent::$stack->push(Middleware::mapRequest(function (RequestInterface $request) {
print 'Hello' . PHP_EOL;
return $request;
}));
}
示例3: testSSO
public function testSSO()
{
$pid = Helper::getSomeProject();
$user = Helper::getSomeUser();
Helper::getClient()->getProjects()->addUser($pid, $user['uid']);
$targetUrl = "/#s=/gdc/projects/{$pid}|projectDashboardPage";
$sso = new SSO(null, null, KBGDC_API_URL);
$ssoLink = $sso->getUrl(KBGDC_USERNAME, KBGDC_SSO_KEY, KBGDC_SSO_PROVIDER, $targetUrl, $user['email'], 3600, KBGDC_SSO_KEY_PASS);
$stack = \GuzzleHttp\HandlerStack::create();
$lastRequest = null;
$stack->push(\GuzzleHttp\Middleware::mapRequest(function (Request $request) use(&$lastRequest) {
$lastRequest = $request;
return $request;
}));
$client = new Client(['handler' => $stack, \GuzzleHttp\RequestOptions::ALLOW_REDIRECTS => true, 'verify' => false]);
try {
$client->request('GET', $ssoLink, ['headers' => ['Accept' => 'application/json']]);
} catch (RequestException $e) {
if ($e->hasResponse()) {
$response = $e->getResponse()->getBody();
$this->fail("{$response} on link: {$ssoLink}");
} else {
$this->fail($e->getMessage() . " on link: {$ssoLink}");
}
}
/** @var Request $lastRequest */
$result = $lastRequest->getUri()->__toString();
$this->assertStringEndsWith($targetUrl, urldecode($result));
}
示例4: __construct
/**
* @param string $id
* @param string $secret
* @param string $url
*/
public function __construct($id, $secret, $url)
{
$this->logger = new NullLogger();
if (!strlen($id) || !strlen($secret)) {
throw new InvalidArgumentException('api_id and api_secret must both be provided');
}
$validatedUrl = filter_var($url, FILTER_VALIDATE_URL);
if (!$validatedUrl) {
throw new InvalidArgumentException($url . ' is not a valid URL');
}
$this->id = $id;
$this->secret = $secret;
$this->baseUrl = $validatedUrl . '/page/api/';
$handlerStack = HandlerStack::create(GuzzleHttp\choose_handler());
$handlerStack->push(Middleware::mapRequest(function (RequestInterface $request) {
$uri = $request->getUri();
$query = new Query($uri->getQuery());
/*
* Add id and version to the query
*/
$query = $query->merge(Query::createFromArray(['api_id' => $this->id, 'api_ver' => '2']));
/*
* Add timestamp to the query
*/
if (!$query->hasKey('api_ts')) {
$query = $query->merge(Query::createFromArray(['api_ts', time()]));
}
$query = $query->merge(Query::createFromArray(['api_mac' => $this->generateMac($uri->getPath(), $query)]));
return $request->withUri($uri->withQuery((string) $query));
}));
$this->guzzleClient = new GuzzleClient(['handler' => $handlerStack]);
}
示例5: createAuthStack
private function createAuthStack()
{
$stack = HandlerStack::create();
$stack->push(Middleware::mapRequest(function (RequestInterface $request) {
return $request->withHeader('Authorization', "Bearer " . $this->accessToken);
}));
return $stack;
}
示例6: getCallable
/**
* @return callable
*/
public function getCallable()
{
return Middleware::mapRequest(function (RequestInterface $request) {
if ($request->hasHeader(AbstractHttpClient::HEADER_HOST_YVES)) {
$this->getLogger()->info(sprintf('Transfer request [%s] %s', $request->getMethod(), $request->getRequestTarget()), ['guzzle-body' => $request->getBody()->getContents()]);
}
return $request;
});
}
示例7: getSigningMiddleware
private function getSigningMiddleware()
{
$region = $this->getConnection()->hasParam('aws_region') ? $this->getConnection()->getParam('aws_region') : getenv('AWS_REGION');
$signer = new SignatureV4('es', $region);
$credProvider = $this->getCredentialProvider();
return Middleware::mapRequest(function (RequestInterface $req) use($signer, $credProvider) {
return $signer->signRequest($req, $credProvider()->wait());
});
}
示例8: get
public static function get(array $options, $key)
{
$stack = HandlerStack::create();
$stack->unshift(Middleware::mapRequest(function (RequestInterface $request) use($key) {
return $request->withUri(Uri::withQueryValue($request->getUri(), 'key', $key));
}));
$options['handler'] = $stack;
return new Client($options);
}
示例9: create
public static function create()
{
return Middleware::mapRequest(function (RequestInterface $request) {
if ($request->getUri()->getHost() !== 'api.github.com') {
return $request;
}
$authorization = 'token ' . getenv('GITHUB_TOKEN');
return $request->withHeader('Authorization', $authorization);
});
}
示例10: getCallable
/**
* @return callable
*/
public function getCallable()
{
return Middleware::mapRequest(function (RequestInterface $request) {
if ($request->hasHeader('X-Yves-Host')) {
$requestId = new RequestId();
$request = $request->withAddedHeader('X-Request-ID', $requestId->getRequestId());
}
return $request;
});
}
示例11: setupBeforeClass
public static function setupBeforeClass()
{
$baseUri = getenv('TEST_BASE_URL');
$handler = HandlerStack::create();
$handler->push(Middleware::mapRequest(function (RequestInterface $request) {
$baseUri = $request->getUri();
$baseUri = $baseUri->withPath('/app_test.php' . $baseUri->getPath());
return $request->withUri($baseUri);
}));
self::$staticClient = new Client(['base_uri' => $baseUri, 'defaults' => ['exceptions' => false], 'handler' => $handler]);
self::bootKernel();
}
示例12: getClient
/**
* @return \GuzzleHttp\Client
*/
public function getClient()
{
if ($this->_client === null) {
$this->_client = new Client($this->clientOptions);
$handler = $this->_client->getConfig('handler');
$handler->push(Middleware::mapRequest(function ($request) {
$this->_restObject = new RestObject($request);
return $request;
}));
}
return $this->_client;
}
示例13: __construct
/**
*
* @param \Kazoo\SDK $sdk
*/
public function __construct(SDK $sdk)
{
$this->setSDK($sdk);
$sdk = $this->getSDK();
$options = $sdk->getOptions();
$handler = HandlerStack::create();
$handler->push(Middleware::mapRequest(function (Request $request) {
$sdk = $this->getSDK();
$token = $sdk->getAuthToken()->getToken();
return $request->withHeader('X-Auth-Token', $token);
}));
$handler->push(Middleware::mapResponse(function (GuzzleResponse $guzzleResponse) {
$response = new Response($guzzleResponse);
$code = $response->getStatusCode();
switch ($code) {
case 400:
throw new Validation($response);
case 401:
// invalid creds
throw new Unauthenticated($response);
case 402:
// not enough credit
throw new Billing($response);
case 403:
// forbidden
throw new Unauthorized($response);
case 404:
// not found
throw new NotFound($response);
case 405:
// invalid method
throw new InvalidMethod($response);
case 409:
// conflicting documents
throw new Conflict($response);
case 429:
// too many requests
throw new RateLimit($response);
default:
if ($code >= 400 && $code < 500) {
throw new ApiException($response);
} else {
if ($code > 500) {
throw new HttpException($response);
}
}
}
return $guzzleResponse;
}));
$options['handler'] = $handler;
$this->setClient(new GuzzleClient($options));
}
示例14: mapRequest
public function mapRequest()
{
return Middleware::mapRequest(function (RequestInterface $request) {
if ($this->getConfig('auth') == 'bulk') {
$token = $this->getAccessToken();
if ($token !== null) {
$request = $request->withHeader('X-SFDC-Session', $token->getToken());
return $request;
}
}
return $request;
});
}
示例15: getGuzzleHandler
/**
* @return HandlerStack
*/
protected function getGuzzleHandler()
{
$handler = HandlerStack::create();
$handler->push(Middleware::mapResponse(function (ResponseInterface $response) {
$this->lastResponse = $response;
return $response;
}));
$handler->push(Middleware::mapRequest(function (RequestInterface $request) {
$this->lastRequest = $request;
return $request;
}));
return $handler;
}