function it_expands_a_url(RequestFactory $requestFactory, RequestInterface $request, HttpClient $httpClient, ResponseInterface $response, StreamInterface $body) { $requestFactory->createRequest('GET', 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=' . urlencode('http://goo.gl/shortened'))->willReturn($request); $httpClient->sendRequest($request)->willReturn($response); $response->getBody()->willReturn($body); $body->__toString()->willReturn('{"longUrl": "http://any.url"}'); $this->expand('http://goo.gl/shortened')->shouldReturn("http://any.url"); }
/** * {@inheritdoc} */ public function expand($url) { $url = sprintf('%s/expand?%s', self::ENDPOINT, http_build_query(['access_token' => $this->accessToken, 'shortUrl' => trim($url)])); $request = $this->requestFactory->createRequest('GET', $url); $response = $this->httpClient->sendRequest($request); $response = json_decode((string) $response->getBody()); return $response->data->expand[0]->long_url; }
function it_expands_a_url(RequestFactory $requestFactory, RequestInterface $request, HttpClient $httpClient, ResponseInterface $response, StreamInterface $body) { $requestFactory->createRequest('GET', 'https://api-ssl.bitly.com/v3/expand?access_token=access_token&shortUrl=' . urlencode('http://bit.ly/shortened'))->willReturn($request); $httpClient->sendRequest($request)->willReturn($response); $response->getBody()->willReturn($body); $body->__toString()->willReturn('{"data": {"expand": [{"long_url": "http://any.url"}]}}'); $this->expand('http://bit.ly/shortened')->shouldReturn("http://any.url"); }
/** * {@inheritdoc} */ public function expand($url) { $url = sprintf('%s?%s', self::ENDPOINT, http_build_query(['key' => $this->apiKey, 'shortUrl' => $url])); $request = $this->requestFactory->createRequest('GET', $url); $response = $this->httpClient->sendRequest($request); $response = json_decode((string) $response->getBody()); return $response->longUrl; }
function it_throws_exception_when_request_returns_not_found_error(HttpClientInterface $httpClient, RequestFactoryInterface $requestFactory, RequestInterface $request, ResponseInterface $response, HttpException $httpException) { $apiHttpPathAndQuery = ApiHttpPathAndQuery::createForPath(self::API_PATH); $response->getStatusCode()->willReturn(404); $requestFactory->createRequest(Argument::any(), Argument::any(), Argument::any())->willReturn($request); $httpException->getResponse()->willReturn($response); $httpClient->sendRequest($request)->willThrow($httpException->getWrappedObject()); $this->shouldThrow(ApiHttpClientNotFoundException::class)->duringGet($apiHttpPathAndQuery); }
function it_expands_a_url(RequestFactory $requestFactory, RequestInterface $request, HttpClient $httpClient, ResponseInterface $response, StreamInterface $body) { $params = array_merge($this->params, ['m' => 'expand', 'shortUrl' => 'http://tiny.cc/shortened']); $requestFactory->createRequest('GET', sprintf('http://tiny.cc?%s', http_build_query($params)))->willReturn($request); $httpClient->sendRequest($request)->willReturn($response); $response->getBody()->willReturn($body); $body->__toString()->willReturn('{"results": {"longUrl": "http://any.url"}}'); $this->expand('http://tiny.cc/shortened')->shouldReturn("http://any.url"); }
/** * {@inheritdoc} */ public function get(ApiHttpPathAndQuery $apiHttpPathAndQuery) { try { $request = $this->requestFactory->createRequest(self::GET_REQUEST, $this->apiHttpClientConfiguration->buildUri($apiHttpPathAndQuery), $this->apiHttpClientConfiguration->buildHeaders()); $response = $this->httpClient->sendRequest($request); } catch (Exception $exception) { if ($this->isNotFoundError($exception)) { throw new ApiHttpClientNotFoundException($this->apiHttpClientConfiguration->buildUri($apiHttpPathAndQuery)); } throw new ApiHttpClientException($exception->getMessage(), $exception->getCode(), $exception); } return $response->getBody()->getContents(); }
/** * Perform actual HTTP request to service. * * @param string $method HTTP method. * @param string $uri URI. * @param Document $document Document to send to the server. * * @return Document * * @throws \InvalidArgumentException If given document is not supported. * @throws \Mekras\Atom\Exception\RuntimeException In case of XML errors. * @throws \Mekras\OData\Client\Exception\ClientErrorException On client error. * @throws \Mekras\OData\Client\Exception\NetworkException In case of network error. * @throws \Mekras\OData\Client\Exception\RuntimeException On other errors. * @throws \Mekras\OData\Client\Exception\ServerErrorException On server error. * * @since 0.3 */ public function sendRequest($method, $uri, Document $document = null) { $headers = ['DataServiceVersion' => '2.0', 'MaxDataServiceVersion' => '3.0', 'Accept' => 'application/atom+xml,application/atomsvc+xml,application/xml']; if ($document) { $headers['Content-type'] = 'application/atom+xml'; } $uri = str_replace($this->getServiceRootUri(), '', $uri); $uri = '/' . ltrim($uri, '/'); $request = $this->requestFactory->createRequest($method, $this->getServiceRootUri() . $uri, $headers, $document ? $document->getDomDocument()->saveXML() : null); try { $response = $this->httpClient->sendRequest($request); } catch (HttpClientException\NetworkException $e) { throw new NetworkException($e->getMessage(), $e->getCode(), $e); } catch (\Exception $e) { throw new RuntimeException($e->getMessage(), 0, $e); } $version = $response->getHeaderLine('DataServiceVersion'); if ('' === $version) { throw new ServerErrorException('DataServiceVersion header missed'); } $doc = $this->documentFactory->parseXML((string) $response->getBody()); $this->checkResponseForErrors($response, $doc); return $doc; }
/** * Sends a request with any HTTP method. * * @param string $method HTTP method to use * @param string|UriInterface $uri * @param array $headers * @param string|StreamInterface|null $body * * @throws Exception * * @return ResponseInterface */ public function send($method, $uri, array $headers = [], $body = null) { return $this->sendRequest($this->requestFactory->createRequest($method, $uri, $headers, $body)); }
function it_returns_delete_request_created_by_the_http_client(RequestFactory $requestFactory, RequestInterface $request) { $requestFactory->createRequest('DELETE', 'http://example.com/xapi/statements', array('X-Experience-API-Version' => '1.0.1', 'Content-Type' => 'application/json'), null)->willReturn($request); $this->createRequest('delete', '/statements')->shouldReturn($request); $this->createRequest('DELETE', '/statements')->shouldReturn($request); }
/** * @param string $method * @param string $url * @param PyStringNode $string * * @When I send a ":method" request to ":url" with body: */ public function sendWith($method, $url, PyStringNode $string) { $this->request = $this->requestFactory->createRequest($method, $this->prepareUrl($url), $this->headers, trim($string->getRaw())); $this->sendRequest(); }