/** * @param RequestInterface $request * @param CacheStrategyInterface $cacheStorage * @param CacheEntry $cacheEntry * * @return bool if added */ protected function addReValidationRequest(RequestInterface $request, CacheStrategyInterface &$cacheStorage, CacheEntry $cacheEntry) { // Add the promise for revalidate if ($this->client !== null) { /** @var RequestInterface $request */ $request = $request->withHeader(self::HEADER_RE_VALIDATION, '1'); $this->waitingRevalidate[] = $this->client->sendAsync($request)->then(function (ResponseInterface $response) use($request, &$cacheStorage, $cacheEntry) { $update = false; if ($response->getStatusCode() == 304) { // Not modified => cache entry is re-validate /** @var ResponseInterface $response */ $response = $response->withStatus($cacheEntry->getResponse()->getStatusCode()); $response = $response->withBody($cacheEntry->getResponse()->getBody()); // Merge headers of the "304 Not Modified" and the cache entry foreach ($cacheEntry->getResponse()->getHeaders() as $headerName => $headerValue) { if (!$response->hasHeader($headerName)) { $response = $response->withHeader($headerName, $headerValue); } } $update = true; } self::addToCache($cacheStorage, $request, $response, $update); }); return true; } return false; }
/** * @param mixed $data */ public function notify($data) { $client = new Client(); $request = new Request('POST', $this->webhookUrl, ['Content-type' => 'application/json'], json_encode($data)); $promise = $client->sendAsync($request, ['timeout' => 10]); $promise->then(function (ResponseInterface $res) use($data) { $this->logger->info(sprintf('Request to SLACK webhook OK [%s] with data: %s', $res->getStatusCode(), json_encode($data))); }, function (RequestException $e) { $this->logger->error(sprintf('Request to SLACK webhook FAILED with message: %s', $e->getMessage())); }); $promise->wait(false); }
public function __construct() { // call parent constructor parent::__construct(); // create the asyncronous curl handler $this->handler = new CurlMultiHandler(); $httpClient = new Client(['handler' => HandlerStack::create($this->handler)]); // have the request wrapper call guzzle asyncronously $this->setRequestWrapper(new RequestWrapper(['scopes' => PubSubClient::FULL_CONTROL_SCOPE, 'httpHandler' => function ($request, $options = []) use($httpClient) { return $httpClient->sendAsync($request, $options); }, 'authHttpHandler' => HttpHandlerFactory::build()])); }
/** * @param RequestInterface $request * @param CacheStrategyInterface $cacheStorage * @param CacheEntry $cacheEntry * * @return bool if added */ protected function addReValidationRequest(RequestInterface $request, CacheStrategyInterface &$cacheStorage, CacheEntry $cacheEntry) { // Add the promise for revalidate if ($this->client !== null) { /** @var RequestInterface $request */ $request = $request->withHeader(self::HEADER_RE_VALIDATION, '1'); $this->waitingRevalidate[] = $this->client->sendAsync($request)->then(function (ResponseInterface $response) use($request, &$cacheStorage, $cacheEntry) { if ($response->getStatusCode() == 304) { // Not modified => cache entry is re-validate /** @var ResponseInterface $response */ $response = $response->withStatus($cacheEntry->getResponse()->getStatusCode()); $response = $response->withBody($cacheEntry->getResponse()->getBody()); } $cacheStorage->cache($request, $response); }); return true; } return false; }
/** * @param RequestInterface $request * @param CacheStorageInterface $cacheStorage * @param CacheEntry $cacheEntry * @return bool if added */ protected static function addReValidationRequest(RequestInterface $request, CacheStorageInterface &$cacheStorage, CacheEntry $cacheEntry) { // Add the promise for revalidate if (static::$client !== null) { /** @var RequestInterface $request */ $request = $request->withHeader("X-ReValidation", "1"); static::$waitingRevalidate[] = static::$client->sendAsync($request)->then(function (ResponseInterface $response) use($request, &$cacheStorage, $cacheEntry) { if ($response->getStatusCode() == 304) { // Not modified => cache entry is re-validate /** @var ResponseInterface $response */ $response = $response->withStatus($cacheEntry->getResponse()->getStatusCode()); $response = $response->withBody($cacheEntry->getResponse()->getBody()); } $cacheStorage->cache($request, $response); }); return true; } return false; }
public function testCanRetryExceptions() { $calls = []; $decider = function ($retries, $request, $response, $error) use(&$calls) { $calls[] = func_get_args(); return $error instanceof \Exception; }; $m = Middleware::retry($decider); $h = new MockHandler([new \Exception(), new Response(201)]); $c = new Client(['handler' => $m($h)]); $p = $c->sendAsync(new Request('GET', 'http://test.com'), []); $this->assertEquals(201, $p->wait()->getStatusCode()); $this->assertCount(2, $calls); $this->assertEquals(0, $calls[0][0]); $this->assertNull($calls[0][2]); $this->assertInstanceOf('Exception', $calls[0][3]); $this->assertEquals(1, $calls[1][0]); $this->assertInstanceOf(Response::class, $calls[1][2]); $this->assertNull($calls[1][3]); }
/** * Send our request to Guzzle and process the result * * @param Request $ourRequest * @param Client $client * @return \GuzzleHttp\Promise\PromiseInterface */ protected function sendRequest(Request $ourRequest, Client $client) { $ourRequest->processed = false; $ourRequest->processing = true; $request = new GuzzleRequest($ourRequest->options->get(CURLOPT_POSTFIELDS, false) ? 'POST' : 'GET', $ourRequest->url); $promise = $client->sendAsync($request, ['curl' => $ourRequest->options->get()])->then(function ($response) use($client, $ourRequest) { $body = (string) $response->getBody(); $ourRequest->response = $body; $ourRequest->error = false; if ($ourRequest->remote_encoding) { $ourRequest->response = mb_convert_encoding($ourRequest, 'UTF-8', $ourRequest->remote_encoding); } $this->handleProcessedRequest($client, $ourRequest); }, function ($error) use($client, $ourRequest) { $ourRequest->error = $error; $this->handleProcessedRequest($client, $ourRequest); }); return $promise; }
/** * @param Request $command * @param array $operation * * @return bool */ public function executeAsync(Request $command, $operation) { $client = new Client(); $client->sendAsync($command)->then(function ($response) { })->wait(); return true; }
/** * @inheritDoc */ public function sendAsync(RequestInterface $request, array $options = []) { return $this->client->sendAsync($request, $options); }
public function toApi(Request $request) { $client = new \GuzzleHttp\Client(); $input = $request::all(); $command = 'request'; $api_key = 'MjuhMtfAAfvJqzbnWFLA'; // $api_key = 'mysendykey'; $api_username = '******'; // $api_username = '******'; $from_name = 'Chris Munialo'; $from_lat = $input['lat']; $from_long = $input['lng']; $from_description = ''; $to_name = 'Thika Road Mall'; $to_lat = $input['lat1']; $to_long = $input['lng1']; $to_description = ''; $recepient_name = 'John Doe'; $recepient_phone = '0710000000'; $recepient_email = '*****@*****.**'; $pick_up_date = '2016-04-20 12:12:12'; $status = false; $pay_method = 0; $amount = 10; $return = true; $note = 'Sample note'; $note_status = true; $request_type = 'delivery'; $info = ['command' => $command, 'data' => ['api_key' => $api_key, 'api_username' => $api_username, 'from' => ['from_name' => $from_name, 'from_lat' => $from_lat, 'from_long' => $from_long, 'from_description' => $from_description], 'to' => ['to_name' => $to_name, 'to_lat' => $to_lat, 'to_long' => $to_long, 'to_description' => $to_description], 'recepient' => ['recepient_name' => $recepient_name, 'recepient_phone' => $recepient_phone, 'recepient_email' => $recepient_email], 'delivery_details' => ['pick_up_date' => $pick_up_date, 'collect_payment' => ['status' => $status, 'pay_method' => $pay_method, 'amount' => $amount], 'return' => $return, 'note' => $note, 'note_status' => $note_status, 'request_type' => $request_type]]]; $clientHandler = $client->getConfig('handler'); // Create a middleware that echoes parts of the request. $tapMiddleware = Middleware::tap(function ($request) { $request->getHeader('Content-Type'); // application/json echo $request->getBody(); // {"foo":"bar"} }); $endpoint = 'https://developer.sendyit.com/v1/api#request'; // $info = json_encode($info); $client = new \GuzzleHttp\Client(); $res = $client->request('POST', $endpoint, ['json' => $info, 'handler' => $tapMiddleware($clientHandler)]); $res->getStatusCode(); // "200" $res->getHeader('content-type'); // 'application/json; charset=utf8' $res->getBody(); // {"type":"User"... // Send an asynchronous request. $request = new \GuzzleHttp\Psr7\Request('GET', $endpoint); $promise = $client->sendAsync($request)->then(function ($response) { echo 'I completed! ' . $response->getBody(); }); $promise->wait(); return view('orders.new', compact($res)); }
/** * @inheritDoc */ public function exec(RequestInterface $request) { return $this->client->sendAsync($request); }
/** * @param Client $client * @param Request $request * @return PromiseInterface */ protected function send(Client $client, Request $request) { $promise = $client->sendAsync($request)->then([$this, 'handleSuccess'], [$this, 'handleError']); return $promise; }
/** * @param RpcRequest $rpcRequest * @param string $resultType * * @return RpcResponse|null */ protected function sendRequest(RpcRequest $rpcRequest, $resultType = null) { try { $request = $this->convertRequest($rpcRequest); if (!$this->synchronous) { $this->client->sendAsync($request); return; } $response = $this->client->send($request); } catch (ServerException $ex) { $response = $ex->getResponse(); } catch (ClientException $ex) { $response = $ex->getResponse(); } $rpcResponse = $this->convertResponse($response, $resultType); return $rpcResponse; }