function it_handles_ivory_interface_incompatibility(Filesystem $filesystem, HttpAdapterInterface $httpAdapter, Request $request, Response $response, Stream $stream) { $httpAdapter->sendRequest($request)->willReturn($response); $response->getBody()->willReturn($stream); $stream->detach()->willReturn('text'); $filesystem->putStream('path/to/file', Argument::type('resource'))->shouldNotBeCalled(); $filesystem->put('path/to/file', 'text')->willReturn(true); $this->download($request, 'path/to/file')->shouldReturn(true); }
public function next() { if (empty($this->systems)) { return false; } $system = array_pop($this->systems); $request = RequestFactory::getRequest(new Uri($system['url']), 'GET', 'php://memory', ['Accept-Encoding' => 'gzip', 'Connection' => 'keep-alive']); $responses = $this->client->sendRequests(array($request)); return $responses[0]; }
function it_gets_the_balance(HttpAdapterInterface $adapter, Response $response, Stream $stream) { $params = ['key' => 'apiKey', 'format' => 'json', 'apiVersion' => Api::VERSION, 'method' => 'balance']; $result = ['result' => 'OK', 'balance' => 1000.0, 'currency' => 'HUF', 'balance-currency' => '1000 HUF', 'custom-services' => 0, 'monthlySpentBalance' => 0]; $stream->getContents()->willReturn(json_encode($result)); $response->getBody()->willReturn($stream); $url = sprintf('%s?%s', Api::ENDPOINT, http_build_query($params)); $adapter->get($url)->willReturn($response); $this->get()->shouldReturnAnInstanceOf('First\\Seeme\\Entity\\Balance'); }
function it_throws_an_exception_when_ip_is_no_allowed(HttpAdapterInterface $adapter, Response $response, Stream $stream) { $params = ['key' => 'apiKey', 'format' => 'json', 'apiVersion' => Api::VERSION, 'ip' => '123.213.126.123', 'method' => 'setip']; $result = ['result' => 'ERR', 'message' => 'IP is not allowed', 'code' => 1]; $stream->getContents()->willReturn(json_encode($result)); $response->getBody()->willReturn($stream); $url = sprintf('%s?%s', Api::ENDPOINT, http_build_query($params)); $adapter->get($url)->willReturn($response); $this->shouldThrow('First\\Seeme\\ApiException')->duringSet(2077589115); }
function it_throws_an_exception_when_something_bad_happens(HttpAdapterInterface $adapter, Response $response, Stream $stream) { $params = ['key' => 'apiKey', 'format' => 'json', 'apiVersion' => Api::VERSION, 'number' => 36201234657, 'message' => 'This is a message', 'sender' => 'Sender', 'reference' => 1234, 'callbackParams' => [1, 2, 3], 'callback' => 'http://callback.uri']; $result = ['result' => 'ERR', 'message' => 'Something bad happened', 'code' => 1]; $stream->getContents()->willReturn(json_encode($result)); $response->getBody()->willReturn($stream); $url = sprintf('%s?%s', Api::ENDPOINT, http_build_query($params)); $adapter->get($url)->willReturn($response); extract($params); $this->shouldThrow('First\\Seeme\\ApiException')->duringSend($number, $message, $sender, $reference, $callbackParams, $callback); }
/** * Downloads a request * * @param RequestInterface $request * * @return boolean */ public function download(RequestInterface $request, $path) { $response = $this->httpAdapter->sendRequest($request); if (!($body = $response->getBody())) { return false; } $stream = $body->detach(); if (is_resource($stream)) { return $this->filesystem->putStream($path, $stream); } return $this->filesystem->put($path, $stream); }
/** {@inheritdoc} */ public function send($endpoint, $payload) { try { $response = $this->httpAdapter->post($endpoint, ['Content-Type' => 'text/xml; charset=UTF-8'], $payload); } catch (HttpAdapterException $e) { throw TransportException::transportError($e); } if ($response->getStatusCode() !== 200) { throw HttpException::httpError($response->getReasonPhrase(), $response->getStatusCode()); } return (string) $response->getBody(); }
private function send($url, $method = InternalRequestInterface::METHOD_GET, array $data = array()) { try { return $this->adapter->send(sprintf('%s/%s', self::ENDPOINT, $url), $method, array(), $data); } catch (\Exception $e) { throw new \RuntimeException(sprintf('[Yo] something went wrong `%s` the response body was `%s` !', $e->getMessage(), $e->hasResponse() ? $e->getResponse()->getBody()->getContents() : 'not defined')); } }
/** * Makes calls to the elasticsearch server. * * All calls that are made to the server are done through this function * * @param \Elastica\Request $elasticaRequest * @param array $params Host, Port, ... * * @throws \Elastica\Exception\ConnectionException * @throws \Elastica\Exception\ResponseException * @throws \Elastica\Exception\Connection\HttpException * * @return \Elastica\Response Response object */ public function exec(ElasticaRequest $elasticaRequest, array $params) { $connection = $this->getConnection(); if ($timeout = $connection->getTimeout()) { $this->httpAdapter->getConfiguration()->setTimeout($timeout); } $httpAdapterRequest = $this->_createHttpAdapterRequest($elasticaRequest, $connection); $start = microtime(true); $httpAdapterResponse = $this->httpAdapter->sendRequest($httpAdapterRequest); $end = microtime(true); $elasticaResponse = $this->_createElasticaResponse($httpAdapterResponse, $connection); $elasticaResponse->setQueryTime($end - $start); $elasticaResponse->setTransferInfo(array('request_header' => $httpAdapterRequest->getMethod(), 'http_code' => $httpAdapterResponse->getStatusCode())); if ($elasticaResponse->hasError()) { throw new ResponseException($elasticaRequest, $elasticaResponse); } if ($elasticaResponse->hasFailedShards()) { throw new PartialShardFailureException($elasticaRequest, $elasticaResponse); } return $elasticaResponse; }
/** * @param string $method * @param string $uri With or without host * @param string|resource|array $body */ private function send($method, $uri, $body = null) { if (!$this->hasHost($uri)) { $uri = rtrim($this->httpClient->getConfiguration()->getBaseUri(), '/') . '/' . ltrim($uri, '/'); } $stream = new Stream('php://memory', 'rw'); if ($body) { $stream->write($body); } $this->request = new Request($uri, $method, $stream, $this->requestHeaders); $this->response = $this->httpClient->send($uri, $method, $this->requestHeaders, $body); // Reset headers used for the HTTP request $this->requestHeaders = array(); }
/** * {@inheritdoc} */ public function createRedirectRequest(ResponseInterface $response, InternalRequestInterface $internalRequest, HttpAdapterInterface $httpAdapter) { if ($response->getStatusCode() < 300 || $response->getStatusCode() >= 400 || !$response->hasHeader('Location')) { return false; } if ($internalRequest->getParameter(self::REDIRECT_COUNT) >= $this->max) { if ($this->throwException) { $rootRequest = $this->getRootRequest($internalRequest); $exception = HttpAdapterException::maxRedirectsExceeded((string) $rootRequest->getUri(), $this->max, $httpAdapter->getName()); $exception->setRequest($rootRequest); throw $exception; } return false; } $strict = $response->getStatusCode() === 303 || !$this->strict && $response->getStatusCode() <= 302; $headers = $internalRequest->getHeaders(); foreach ($headers as $key => $value) { if (strtolower($key) === 'host') { unset($headers[$key]); } } $redirect = $httpAdapter->getConfiguration()->getMessageFactory()->createInternalRequest($response->getHeaderLine('Location'), $strict ? InternalRequestInterface::METHOD_GET : $internalRequest->getMethod(), $internalRequest->getProtocolVersion(), $headers, $strict ? array() : $internalRequest->getDatas(), $strict ? array() : $internalRequest->getFiles(), $internalRequest->getParameters()); if ($strict) { $redirect = $redirect->withoutHeader('Content-Type')->withoutHeader('Content-Length'); } else { $redirect = $redirect->withBody($internalRequest->getBody()); } return $redirect->withParameter(self::PARENT_REQUEST, $internalRequest)->withParameter(self::REDIRECT_COUNT, $internalRequest->getParameter(self::REDIRECT_COUNT) + 1); }
/** * {@inheritdoc} */ public function createRedirectRequest(ResponseInterface $response, InternalRequestInterface $internalRequest, HttpAdapterInterface $httpAdapter) { if ($response->getStatusCode() < 300 || $response->getStatusCode() >= 400 || !$response->hasHeader('Location')) { return false; } if ($internalRequest->getParameter(self::REDIRECT_COUNT) >= $this->max) { if ($this->throwException) { throw HttpAdapterException::maxRedirectsExceeded((string) $this->getRootRequest($internalRequest)->getUrl(), $this->max, $httpAdapter->getName()); } return false; } $redirect = $httpAdapter->getConfiguration()->getMessageFactory()->cloneInternalRequest($internalRequest); if ($response->getStatusCode() === 303 || !$this->strict && $response->getStatusCode() <= 302) { $redirect->setMethod(InternalRequestInterface::METHOD_GET); $redirect->removeHeaders(array('Content-Type', 'Content-Length')); $redirect->clearRawDatas(); $redirect->clearDatas(); $redirect->clearFiles(); } $redirect->setUrl($response->getHeader('Location')); $redirect->setParameter(self::PARENT_REQUEST, $internalRequest); $redirect->setParameter(self::REDIRECT_COUNT, $internalRequest->getParameter(self::REDIRECT_COUNT) + 1); return $redirect; }
/** * Logs error. * * @param \Ivory\HttpAdapter\HttpAdapterInterface $httpAdapter The http adapter. * @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception. * * @return \Ivory\HttpAdapter\Message\InternalRequestInterface The logged request. */ private function error(HttpAdapterInterface $httpAdapter, HttpAdapterException $exception) { $request = $this->getTimer()->stop($exception->getRequest()); $this->logger->error(sprintf('Unable to send "%s %s".', $exception->getRequest()->getMethod(), (string) $exception->getRequest()->getUri()), array('adapter' => $httpAdapter->getName(), 'exception' => $this->getFormatter()->formatException($exception), 'request' => $this->getFormatter()->formatRequest($request), 'response' => $exception->hasResponse() ? $this->getFormatter()->formatResponse($exception->getResponse()) : null)); return $request; }
/** * Collects an exception. * * @param \Ivory\HttpAdapter\HttpAdapterInterface $httpAdapter The http adapter. * @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception. */ private function collectException(HttpAdapterInterface $httpAdapter, HttpAdapterException $exception) { $this->getTimer()->stop($exception->getRequest()); $this->datas['exceptions'][] = array('adapter' => $httpAdapter->getName(), 'exception' => $this->getFormatter()->formatException($exception), 'request' => $this->getFormatter()->formatRequest($exception->getRequest()), 'response' => $exception->hasResponse() ? $this->getFormatter()->formatResponse($exception->getResponse()) : null); }
/** * Gets the stopwatch name. * * @param \Ivory\HttpAdapter\HttpAdapterInterface $httpAdapter The http adapter. * @param \Ivory\HttpAdapter\Message\InternalRequestInterface $internalRequest The internal request. * * @return string The stopwatch name. */ private function getStopwatchName(HttpAdapterInterface $httpAdapter, InternalRequestInterface $internalRequest) { return sprintf('ivory.http_adapter.%s (%s)', $httpAdapter->getName(), (string) $internalRequest->getUri()); }
/** * {@inheritdoc} */ public function getName() { return $this->httpAdapter->getName(); }
/** * Creates a status code exception. * * @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response. * @param \Ivory\HttpAdapter\Message\InternalRequestInterface $internalRequest The internal request. * @param \Ivory\HttpAdapter\HttpAdapterInterface $httpAdapter The http adapter. * * @return \Ivory\HttpAdapter\HttpAdapterException The status code exception. */ private function createStatusCodeException(ResponseInterface $response, InternalRequestInterface $internalRequest, HttpAdapterInterface $httpAdapter) { $exception = HttpAdapterException::cannotFetchUri((string) $internalRequest->getUri(), $httpAdapter->getName(), sprintf('Status code: %d', $response->getStatusCode())); $exception->setRequest($internalRequest); $exception->setResponse($response); return $exception; }