/** * Actual health check logic. * * @param HealthBuilder $builder * * @throws \Exception any Exception that should create a Status::DOWN * system status. */ protected function doHealthCheck(HealthBuilder $builder) { /** @var Response $response */ $response = $this->request->send(); $builder->withDetail('statusCode', $response->getStatusCode()); if (!$response->isSuccessful()) { $builder->down()->withDetail('body', $response->getBody(true)); return; } $builder->up(); }
/** * @param Request $request * @return array|null * @throws BadResponseException * @throws FormatException */ protected function proceedResponse(Request $request) { try { $response = $request->send(); } catch (ClientErrorResponseException $e) { $response = $e->getResponse(); } $body = $response->getBody(true); if (empty($body) && $response->getStatusCode() >= 200 && $response->getStatusCode() < 300) { return null; } $result = json_decode($body, true); if ($result === null) { # When response is just a sting. $result = $body; } if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) { return $result; } if (!$result || !$result['error']) { throw new FormatException('Not valid error response', 3, null, $request, $response); } $message = $result['error']['message']; $code = $result['error']['code']; if ($response->getStatusCode() == 429) { throw new RateLimitException($message, $code, null, $request, $response); } $e = new BadResponseException($message, $code, null, $request, $response); $e->setError($result['error']); throw $e; }
private function resolveRequest(\Guzzle\Http\Message\Request $request) { try { $this->lastResponse = $request->send(); } catch (\Guzzle\Http\Exception\TooManyRedirectsException $tooManyRedirectsException) { if ($this->hasRequestHistory($request)) { $this->lastResponse = $this->getRequestHistory($request)->getLastResponse(); } else { return $request->getUrl(); } } catch (\Guzzle\Http\Exception\BadResponseException $badResponseException) { if ($this->getConfiguration()->getRetryWithUrlEncodingDisabled() && !$this->getConfiguration()->getHasRetriedWithUrlEncodingDisabled()) { $this->getConfiguration()->setHasRetriedWithUrlEncodingDisabled(true); return $this->resolveRequest($this->deEncodeRequestUrl($request)); } else { $this->lastResponse = $badResponseException->getResponse(); } } if ($this->getConfiguration()->getHasRetriedWithUrlEncodingDisabled()) { $this->getConfiguration()->setHasRetriedWithUrlEncodingDisabled(false); } if ($this->getConfiguration()->getFollowMetaRedirects()) { $metaRedirectUrl = $this->getMetaRedirectUrlFromLastResponse(); if (!is_null($metaRedirectUrl) && !$this->isLastResponseUrl($metaRedirectUrl)) { return $this->resolve($metaRedirectUrl); } } return $this->lastResponse->getEffectiveUrl(); }
/** * Function to send the request to the remote API * * @return \Trucker\Responses\Response */ public function sendRequest() { try { $response = $this->request->send(); } catch (\Guzzle\Http\Exception\BadResponseException $e) { $response = $e->getResponse(); } return $this->app->make('trucker.response')->newInstance($this->app, $response); }
public function request(Request $request) { try { $response = $request->send(); } catch (\Exception $e) { throw new ApiException($e->getMessage(), $e->getCode(), $e); } $apiResponse = new ApiResponse($response->getBody(), $response->getStatusCode(), $response->getContentType()); return $apiResponse; }
public function send() { try { return parent::send(); } catch (BadResponseException $e) { $response = $e->getResponse(); $data = json_decode($response->getBody()); throw new GroongaException($data[0][3]); } }
/** * Executing service description defined operations returns the result * @group unit */ public function testMagicOperationsReturnResult() { $responseData = array("this" => "that"); Phockito::when($this->mockResponse->getStatusCode())->return(200); Phockito::when($this->mockResponse->json())->return($this->testDescription); Phockito::when($this->mockRequest->send())->return($this->mockResponse); Phockito::when($this->client)->get(anything())->return($this->mockRequest); Phockito::when($this->client)->callParent(anything(), anything())->return($responseData); $result = $this->client->SomeOperation(array()); $this->assertEquals($result, $responseData); }
/** * @covers Guzzle\Http\Message\Request::getResponse * @covers Guzzle\Http\Message\Request::processResponse * @covers Guzzle\Http\Message\Request::getResponseBody */ public function testRequestThrowsExceptionOnBadResponse() { try { $this->request->setResponse(new Response(404, array('Content-Length' => 3), 'abc'), true); $this->request->send(); $this->fail('Expected exception not thrown'); } catch (BadResponseException $e) { $this->assertInstanceOf('Guzzle\\Http\\Message\\RequestInterface', $e->getRequest()); $this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $e->getResponse()); $this->assertContains('Client error response', $e->getMessage()); } }
/** * Load request sources * * @param $source * @return Export */ public function loadSource($source = '') { empty($source) === false ? $this->setSource($source) : null; $client = new Client($this->source, $this->options); $this->request = $client->createRequest(); try { $this->response = $this->request->send(); } catch (ResponseException $e) { throw new BadResponseException($e->getMessage(), $e->getCode()); } return $this; }
/** * Function to send the request to the remote API * * @return \Trucker\Responses\Response */ public function sendRequest() { try { $response = $this->request->send(); } catch (\Guzzle\Http\Exception\BadResponseException $e) { $response = $e->getResponse(); } if (Config::get('request.debug', false)) { echo 'Request Method: ' . $this->request->getMethod() . "\n"; echo "Body: " . $response->getBody() . "\n"; echo "Status Code: " . $response->getStatusCode() . "\n"; echo "URL: " . $this->request->getUrl() . "\n\n"; } return $this->app->make('trucker.response')->newInstance($this->app, $response); }
/** * @return \Sokil\Rest\Client\Response */ public function send() { try { // disbatch beforeSend event $this->_request->getEventDispatcher()->dispatch('onBeforeSend', new \Guzzle\Common\Event(array('request' => $this))); // create response $this->_response = new Response($this->_request->send(), $this->_structureClassName); // trigger event $this->_request->getEventDispatcher()->dispatch('onParseResponse', new \Guzzle\Common\Event(array('request' => $this, 'response' => $this->_response))); // log if ($this->hasLogger()) { $this->getLogger()->debug((string) $this->_request . PHP_EOL . (string) $this->_request->getResponse()); } return $this->_response; } catch (\Exception $e) { if ($this->hasLogger()) { $this->getLogger()->debug((string) $e); } throw $e; } }
/** * @When /^I request "(GET|PUT|POST|DELETE|PATCH) ([^"]*)"$/ */ public function iRequest($httpMethod, $resource) { // process any %battles.last.id% syntaxes $resource = $this->processReplacements($resource); $this->resource = $resource; // reset the response payload $this->responsePayload = null; $method = strtolower($httpMethod); try { switch ($httpMethod) { case 'PUT': case 'POST': case 'PATCH': // process any %user.weaverryan.id% syntaxes $payload = $this->processReplacements($this->requestPayload); $this->lastRequest = $this->client->{$method}($resource, null, $payload); break; default: $this->lastRequest = $this->client->{$method}($resource); } if ($this->authUser) { $this->lastRequest->setAuth($this->authUser, $this->authPassword); } foreach ($this->headers as $key => $val) { $this->lastRequest->setHeader($key, $val); } $this->response = $this->lastRequest->send(); } catch (BadResponseException $e) { $response = $e->getResponse(); // Sometimes the request will fail, at which point we have // no response at all. Let Guzzle give an error here, it's // pretty self-explanatory. if ($response === null) { throw $e; } $this->response = $e->getResponse(); } }
/** * @param \Guzzle\Http\Message\Request $request * @return \Guzzle\Http\Message\Response */ public function send(\Guzzle\Http\Message\Request $request) { $token = $this->getToken(); $request->setHeader('X-Auth-Token', $token['token'])->setHeader('Content-Type', 'application/json')->setHeader('X-Auth-UserId', $token['user_id']); return $request->send(); }
/** * @param string $uri * @param array $headers * @param string $postBody * @param array $options * @return array|\Guzzle\Http\Message\Response */ public function post($uri, $headers = [], $postBody = null, $options = []) { $this->request = $this->client->post($uri, $headers, $postBody, $options); $this->response = $this->request->send(); return $this->response; }
/** * Get the response from the prepared request. * * @param \Guzzle\Http\Message\Request $request * * @return array */ protected function getResponse($request) { try { $response = json_decode($request->send()->getBody(), true); return $response['response']; } catch (BadResponseException $bre) { return; } }
/** * @param \Guzzle\Http\Message\Request $xml */ public function __construct(Request $request) { $this->client = $request->getClient(); $this->xml = $request->send()->xml(); }
/** * @param Request $request * * @param string $body * * @throws \Elasticsearch\Common\Exceptions\TransportException * @return \Guzzle\Http\Message\Response */ private function sendRequest(Request $request, $body) { try { $request->send(); } catch (ServerErrorResponseException $exception) { $this->process5xxError($request, $exception, $body); } catch (ClientErrorResponseException $exception) { $this->process4xxError($request, $exception, $body); } catch (CurlException $exception) { $this->processCurlError($exception); } catch (\Exception $exception) { $error = 'Unexpected error: ' . $exception->getMessage(); $this->log->critical($error); throw new TransportException($error); } $this->processSuccessfulRequest($request, $body); return $request->getResponse(); }
/** * @covers Guzzle\Http\Plugin\CookiePlugin */ public function testAddsCookiesToRequests() { ArrayCookieJarTest::addCookies($this->storage); $this->storage->save(array('domain' => '.y.example.com', 'path' => '/acme/', 'cookie' => array('secure', 'sec'), 'expires' => Guzzle::getHttpDate('+1 day'), 'secure' => true)); // Add a cookie that is only set on a specific port, so it wont be // added to the following requests $this->storage->save(array('domain' => '.y.example.com', 'path' => '/acme/', 'cookie' => array('test', 'port'), 'expires' => Guzzle::getHttpDate('+1 day'), 'secure' => false, 'port' => array(8192))); $request1 = new Request('GET', 'https://a.y.example.com/acme/'); $request1->setClient(new Client()); $request2 = new Request('GET', 'https://a.y.example.com/acme/'); $request2->setClient(new Client()); $request3 = new Request('GET', 'http://a.y.example.com/acme/'); $request3->setClient(new Client()); $request4 = new Request('GET', 'http://a.y.example.com/acme/'); $request4->setClient(new Client()); $request1->getEventDispatcher()->addSubscriber($this->plugin); $request2->getEventDispatcher()->addSubscriber($this->plugin); $request3->getEventDispatcher()->addSubscriber($this->plugin); $request4->getEventDispatcher()->addSubscriber($this->plugin); // Set a secure cookie $response1 = Response::fromMessage("HTTP/1.1 200 OK\r\nSet-Cookie: a=b; c=d; Max-Age=86400; domain=.example.com; secure;\r\n\r\n"); // Set a regular cookie $response2 = Response::fromMessage("HTTP/1.1 200 OK\r\nSet-Cookie: e=f h; discard; domain=.example.com;\r\n\r\n"); $response3 = Response::fromMessage("HTTP/1.1 200 OK\r\n\r\n"); $request1->setResponse($response1, true); $request2->setResponse($response2, true); $request3->setResponse($response3, true); $request1->send(); $request2->send(); $request3->send(); $this->assertEquals('muppet=cookie_monster;secure=sec', (string) $request1->getCookie()); $this->assertEquals('muppet=cookie_monster;secure=sec;a=b;c=d', (string) $request2->getCookie()); $this->assertEquals('muppet=cookie_monster;e=f h', (string) $request3->getCookie()); // Clear the e=f h temporary cookie $this->plugin->clearTemporaryCookies(); $request4->setResponse($response3, true); $request4->send(); $this->assertEquals('muppet=cookie_monster', (string) $request4->getCookie()); }
function it_should_return_wgs84_information(Response $response, Request $request) { // mocks $request->send()->willReturn($response); $this->client->get(Argument::any())->willReturn($request); $response->getStatusCode()->willReturn(200); $response->getBody(true)->willReturn('{"success":true,"resource":{"street":"Wilhelminapark", "house_number":21, "postcode":"5041EB", "town":"Tilburg", "municipality":"Tilburg", "province":"Noord-Brabant", "latitude":51.9401, "longitude":5.61531, "x":133504, "y":397543}}'); $latitude = 51.5664; $longitude = 5.07718; $this->wgs84($latitude, $longitude)->shouldReturnAnInstanceOf('Niborb\\PostcodeAPI\\Client\\Response'); }
public function send(Request $request) { try { return $request->send()->json(); } catch (GuzzleHttpException $ex) { throw SiftHttpException::fromGuzzleException($ex); } }
/** * Returns a Response object * * @param Guzzle\Http\Message\Request $request * * @return array|Guzzle\Http\Message\Response */ private function doRequest(Request $request) { try { return $request->send(); } catch (ClientErrorResponseException $e) { return $e->getResponse(); } }
protected function sendRequest(Request $request) { try { return $request->send()->json(); } catch (BadResponseException $e) { $response = $e->getResponse()->getBody(true); throw new CapsideaException("Error in capsidea response: {$response}", 0, $e); } }
/** * @param \Guzzle\Http\Client $client * @param \Guzzle\Http\Message\Request $request * @param \Guzzle\Http\Message\Response $response */ function let($client, $request, $response) { $this->beConstructedWith('*****@*****.**', 'password', $client); $request->send()->willReturn($response); }