/** * {@inheritdoc} */ public function fetch(FeedInterface $feed) { $request = $this->httpClient->createRequest('GET', $feed->getUrl()); $feed->source_string = FALSE; // Generate conditional GET headers. if ($feed->getEtag()) { $request->addHeader('If-None-Match', $feed->getEtag()); } if ($feed->getLastModified()) { $request->addHeader('If-Modified-Since', gmdate(DateTimePlus::RFC7231, $feed->getLastModified())); } try { $response = $this->httpClient->send($request); // In case of a 304 Not Modified, there is no new content, so return // FALSE. if ($response->getStatusCode() == 304) { return FALSE; } $feed->source_string = $response->getBody(TRUE); $feed->setEtag($response->getHeader('ETag')); $feed->setLastModified(strtotime($response->getHeader('Last-Modified'))); $feed->http_headers = $response->getHeaders(); // Update the feed URL in case of a 301 redirect. if ($response->getEffectiveUrl() != $feed->getUrl()) { $feed->setUrl($response->getEffectiveUrl()); } return TRUE; } catch (RequestException $e) { $this->logger->warning('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage())); drupal_set_message(t('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage())), 'warning'); return FALSE; } }
/** * @param \DonePM\ConsoleClient\Http\Commands\Command $command * * @return mixed|\Psr\Http\Message\ResponseInterface */ public function send(Command $command) { if ($command instanceof NeedsToken) { $command->token($this->token); } return $this->client->send($command->request()); }
public function testOnlyResponse() { $request = $this->guzzleHttpClient->createRequest('GET', 'http://petstore.swagger.io/v2/pet/findByStatus'); $request->addHeader('Accept', 'application/json'); $response = $this->guzzleHttpClient->send($request); $this->assertResponseMatch($response, self::$schemaManager, '/v2/pet/findByStatus', 'get'); }
/** * @param string $url * @param mixed[]|null $body * @return \SlevomatZboziApi\Response\ZboziApiResponse */ public function sendPostRequest($url, array $body = null) { TypeValidator::checkString($url); $options = ['allow_redirects' => false, 'verify' => true, 'decode_content' => true, 'expect' => false, 'timeout' => $this->timeoutInSeconds]; $request = $this->client->createRequest('POST', $url, $options); $request->setHeaders([static::HEADER_PARTNER_TOKEN => $this->partnerToken, static::HEADER_API_SECRET => $this->apiSecret]); if ($body !== null) { $request->setBody(\GuzzleHttp\Stream\Stream::factory(json_encode($body))); } try { try { $response = $this->client->send($request); $this->log($request, $response); return $this->getZboziApiResponse($response); } catch (\GuzzleHttp\Exception\RequestException $e) { $response = $e->getResponse(); $this->log($request, $response); if ($response !== null) { return $this->getZboziApiResponse($response); } throw new \SlevomatZboziApi\Request\ConnectionErrorException('Connection to Slevomat API failed.', $e->getCode(), $e); } } catch (\GuzzleHttp\Exception\ParseException $e) { $this->log($request, isset($response) ? $response : null, true); throw new \SlevomatZboziApi\Response\ResponseErrorException('Slevomat API invalid response: invalid JSON data.', $e->getCode(), $e); } }
protected function handleRequest(Request $request) { if (!isset($this->config['batch'])) { return $this->client->send($request); } return $request; }
/** * Send asynchronous guzzle request * * @param RequestInterface $psrRequest * @param \Tebru\Retrofit\Http\Callback $callback * @return null */ public function sendAsync(RequestInterface $psrRequest, Callback $callback) { $request = $this->createRequest($psrRequest, true); /** @var FutureInterface $response */ $response = $this->client->send($request); $response->then(function (ResponseInterface $response) { return new Psr7Response($response->getStatusCode(), $response->getHeaders(), $response->getBody(), $response->getProtocolVersion(), $response->getReasonPhrase()); }, function (Exception $exception) use($callback, $psrRequest) { $request = $psrRequest; $response = null; if ($exception instanceof \GuzzleHttp\Exception\RequestException) { $request = $exception->getRequest(); $response = $exception->getResponse(); } $requestException = new RequestException($exception->getMessage(), $exception->getCode(), $exception->getPrevious(), $request, $response); if (null !== $this->eventDispatcher) { $this->eventDispatcher->dispatch(ApiExceptionEvent::NAME, new ApiExceptionEvent($requestException, $request)); } $callback->onFailure($requestException); })->then(function (Psr7Response $response) use($callback, $psrRequest) { if (null !== $this->eventDispatcher) { $this->eventDispatcher->dispatch(AfterSendEvent::NAME, new AfterSendEvent($psrRequest, $response)); } $callback->onResponse($response); }); $this->responses[] = $response; }
/** * @param TransportRequestInterface $request The configured request to send. * * @throws \Elastification\Client\Exception\ClientException * @return \Elastification\Client\Transport\TransportResponseInterface * @author Mario Mueller */ public function send(TransportRequestInterface $request) { try { return new GuzzleTransportResponse($this->guzzleClient->send($request->getWrappedRequest())); } catch (\Exception $exception) { throw new TransportLayerException($exception->getMessage(), $exception->getCode(), $exception); } }
function it_should_post_an_url(ClientInterface $handler, RequestInterface $request, ResponseInterface $response) { $options = ['body' => 'foo']; $handler->createRequest('POST', 'foo', $options)->willReturn($request); $handler->send($request)->shouldBeCalled(); $handler->send($request)->willReturn($response); $this->post('foo', $options); }
public function testFetchPetBodyMatchDefinition() { $request = $this->guzzleHttpClient->createRequest('GET', 'http://petstore.swagger.io/v2/pet/findByStatus'); $request->addHeader('Accept', 'application/json'); $response = $this->guzzleHttpClient->send($request); $responseBody = $response->json(['object' => true]); $this->assertResponseBodyMatch($responseBody, self::$schemaManager, '/v2/pet/findByStatus', 'get', 200); }
/** * @param $url * @param array $params * * @return array */ public function get($url, $params = array()) { $request = $this->provider->request($url, $params); $response = $this->client->send($request); $data = $response->getBody()->getContents(); $format = $this->getFormat($params, $response); return $this->serializer->deserialize($data, null, $format); }
/** * {@inheritdoc} */ public function sendRequest(RequestInterface $request) { try { return $this->client->send($request); } catch (RequestException $e) { throw $this->createException($e); } }
/** * {@inheritdoc} */ public function perform(OperationInterface $operation, ConfigurationInterface $configuration) { $preparedRequestParams = $this->prepareRequestParams($operation, $configuration); $queryString = $this->buildQueryString($preparedRequestParams, $configuration); $uri = new Uri(sprintf($this->requestTemplate, $configuration->getCountry(), $queryString)); $request = new \GuzzleHttp\Psr7\Request('GET', $uri->withScheme($this->scheme), ['User-Agent' => 'ApaiIO [' . ApaiIO::VERSION . ']']); $result = $this->client->send($request); return $result->getBody()->getContents(); }
/** * {@inheritdoc} */ public function handle(RequestInterface $request) { $guzzleRequest = $this->createGuzzleRequestFromRequest($request); // @todo add support for exceptions $guzzleResponse = $this->client->send($guzzleRequest); /** @var \GuzzleHttp\Message\Response $guzzleResponse */ $response = $this->createResponseFromGuzzleResponse($guzzleResponse); return $response; }
/** * Send asynchronous guzzle request * * @param Psr7Request $request * @param \Tebru\Retrofit\Http\Callback $callback * @return null */ public function sendAsync(Psr7Request $request, Callback $callback) { $request = new Request($request->getMethod(), (string) $request->getUri(), $request->getHeaders(), $request->getBody(), ['future' => true]); /** @var FutureInterface $response */ $response = $this->client->send($request); $this->promises[] = $response->then(function (ResponseInterface $response) { return new Psr7Response($response->getStatusCode(), $response->getHeaders(), $response->getBody(), $response->getProtocolVersion(), $response->getReasonPhrase()); })->then($callback->success(), $callback->failure()); }
/** * @inheritdoc */ public function request(Client $client) { $tokenUrl = $client->getTokenUrl(); $queryData = ["client_id" => $client->getClientId(), "client_secret" => $client->getClientSecret(), "grant_type" => "client_credentials"]; $url = $tokenUrl . "?" . http_build_query($queryData); $request = new Request("GET", $url); $response = $this->httpClient->send($request); return $response; }
/** * @param string $method * @param string $path * @param array $options * * @return array */ public function sendRequest($method, $path, array $options = []) { try { $response = $this->handler->send($this->handler->createRequest($method, $path, $options)); } catch (ClientException $ex) { $response = $ex->getResponse(); } return $response->json(); }
private function sendRequest() { $url = self::ApiUrl . '/' . $this->request; $request = $this->client->createRequest('GET', $url); $request->addHeader('Content-Type', 'application/json'); $response = $this->client->send($request); $data = $response->json(); return new Response($data); }
/** * {@inheritdoc} */ public function sendRequest(RequestInterface $request) { $guzzleRequest = $this->createRequest($request); try { $response = $this->client->send($guzzleRequest); } catch (GuzzleExceptions\TransferException $e) { throw $this->handleException($e, $request); } return $this->createResponse($response); }
public function send(RequestInterface $request) { $request = $this->authenticationManager->authenticate($request, $this->client); $uri = (string) $request->getUri(); if (strpos($uri, $this->client->getResourceUrl()) !== 0) { $uri = new Uri($this->client->getResourceUrl() . $uri); } $request = $request->withUri($uri); return $this->httpClient->send($request); }
/** * Send the request, and handle the response * * @param Request $request The request to send via client * * @return Entity|EntityCollection */ protected function handleRequest(RequestInterface $request) { $response = $this->client->send($request); $entityData = $this->decodeResponse($response); // If the key 0 is set, then its probably an array of items if (isset($entityData[0])) { return $this->createEntityCollection($entityData); } return $this->createEntity($entityData); }
/** * {@inheritdoc} */ public function queryCircle($url, $query_args = [], $method = 'GET', $request_options = []) { if (!isset($query_args['circle-token'])) { throw new \InvalidArgumentException('The circle-token is required for all endpoints.'); } $url .= '?' . http_build_query($query_args); $request = $this->httpClient->createRequest($method, $url, $request_options); $request->addHeaders(['Accept' => 'application/json']); return $this->httpClient->send($request)->json(); }
/** * @param Request $request * * @return Response */ protected function send(Request $request) : Generator { try { return (yield $this->client->send($request, ['auth' => [$this->credentials->getUsername(), $this->credentials->getPassword()]])); } catch (RequestException $error) { if (null === $error->getResponse()) { throw new EventStoreUnreachableException(sprintf('Could not get any response from event store: %s', $error->getMessage()), $error->getCode(), $error); } throw $error; } }
/** * {@inheritdoc} */ protected function doSendInternalRequest(InternalRequestInterface $internalRequest) { try { $response = $this->client->send($this->createRequest($internalRequest)); } catch (\Exception $e) { throw HttpAdapterException::cannotFetchUrl($e->getRequest()->getUrl(), $this->getName(), $e->getMessage()); } return $this->getConfiguration()->getMessageFactory()->createResponse((int) $response->getStatusCode(), $response->getReasonPhrase(), $response->getProtocolVersion(), $response->getHeaders(), BodyNormalizer::normalize(function () use($response) { return new GuzzleHttpStream($response->getBody()); }, $internalRequest->getMethod())); }
/** * {@inheritDoc} */ public function findAddressByCep(Cep $cep) { try { $request = new Request('GET', 'http://api.postmon.com.br/v1/cep/' . $cep); $response = $this->httpClient->send($request); /* @var \string[][] $json */ $json = json_decode($response->getBody(), true); return new PartialAddress($json['estado_info']['nome'], $json['cidade'], isset($json['bairro']) ? $json['bairro'] : null, isset($json['logradouro']) ? $json['logradouro'] : null, isset($json['complemento']) ? $json['complemento'] : null); } catch (\Exception $e) { } throw CepNotFoundException::forCep($cep); }
/** * {@inheritdoc } */ public function post($url, $body, array $files = array()) { $request = $this->httpClient->createRequest('POST', $url, ['body' => $body]); /** @var PostBodyInterface $postBody */ $postBody = $request->getBody(); foreach ($files as $key => $filePath) { $file = $this->postFileFactory->create($key, $filePath); $postBody->addFile($file); } $response = $this->httpClient->send($request); return $response; }
/** * Send a HTTP request. * * @param string $url * @param string $method * @param array|string $body * @param array $headers * @param array $options * @return ResponseInterface */ public function send($url, $method, $body, $headers = [], $options = []) { if (is_array($body)) { $body = http_build_query($body); } $request = new Request($method, $url, $headers, $body); try { return new HttpResponse($this->guzzle->send($request, $options)); } catch (TransferException $e) { return new ErrorResponse(); } }
/** * Sends a psr-7 request, while dispatching request + response events * * @param RequestInterface $request * * @return ResponseInterface */ public function send(RequestInterface $request) { /** @var RequestEvent $event */ $event = new RequestEvent($request); $event = $this->eventDispatcher->dispatch(ClientEvents::REQUEST, $event); $request = $event->getRequest(); $response = $this->httpClient->send($request); /** @var ResponseEvent $event */ $event = new ResponseEvent($response); $event = $this->eventDispatcher->dispatch(ClientEvents::RESPONSE, $event); return $event->getResponse(); }
/** * @param string $endpoint * @param string $content * @param array $headers * @param array $files * @return Response */ public function send($endpoint, $content, array $headers = array(), array $files = array()) { $request = $this->client->createRequest('POST', $endpoint, array('body' => $content, 'exceptions' => false, 'headers' => $headers)); $body = new PostBody(); if ($files && $request instanceof RequestInterface) { foreach ($files as $name => $path) { $body->addFile(new PostFile($name, fopen($path, 'r'))); } } $request->setBody($body); $response = $this->client->send($request); return new Response($response->getStatusCode(), $response->getBody()); }
/** * @param string $url * @param array $params * * @return array */ private function discoverLinks($url, array $params = array()) { $request = new Psr7\Request('get', $url); $response = $this->client->send($request); $xpath = isset($params['format']) ? self::xpathForFormat($params['format']) : self::LINK_ANY_XPATH; $links = self::responseBodyOEmbedLinks($response, $xpath); if (!empty($links) && isset($params['format'])) { $links = array_filter($links, function ($link) use($params) { return $this->negotiator->getFormat($link[1]) !== null && $params['format'] === $this->negotiator->getFormat($link[1]); }); } return $links; }
/** * @param ActionInterface $method * @return mixed * @throws \badams\GoogleUrl\Exceptions\InvalidValueException * @throws \badams\GoogleUrl\Exceptions\GoogleUrlException * @throws \badams\GoogleUrl\Exceptions\InvalidKeyException * @throws GoogleUrlException */ private function execute(ActionInterface $method) { $response = $this->http->send($this->createRequest($method)); if ($response->getStatusCode() !== 200) { $json = json_decode($response->getBody()->getContents()); if (isset($json->error) && is_array($json->error->errors)) { $this->assertInvalidKey($json); $this->assertInvalidValue($json); } // @codeCoverageIgnore throw new GoogleUrlException($response->getBody()); } return $method->processResponse($response); }