/** * Pretend to do a POST request * * @param $str_url * @param array $arr_params * @return \GuzzleHttp\Psr7\Response */ public function post($str_url, array $arr_params) { // echo $str_url, '::', print_r($arr_params, true), PHP_EOL; $this->str_url = $str_url; $this->arr_params = $arr_params; $obj_response = new \GuzzleHttp\Psr7\Response(); return $obj_response->withBody(\GuzzleHttp\Psr7\stream_for(json_encode($this->obj_fake_response))); }
private function startConnection() { $loop = \EventLoop\getLoop(); $dnsResolverFactory = new Factory(); $dnsResolver = $dnsResolverFactory->createCached('8.8.8.8', $loop); $factory = new \React\HttpClient\Factory(); $client = $factory->create($loop, $dnsResolver); $cNegotiator = new ClientNegotiator($this->url, $this->subProtocols); $headers = $cNegotiator->getRequest()->getHeaders(); $flatHeaders = []; foreach ($headers as $k => $v) { $flatHeaders[$k] = $v[0]; } $request = $client->request("GET", $this->url, $flatHeaders, '1.1'); $request->on('response', function (Response $response, Request $request) use($cNegotiator) { if ($response->getCode() !== 101) { throw new \Exception("Unexpected response code " . $response->getCode()); } // TODO: Should validate response //$cNegotiator->validateResponse($response); $subprotoHeader = ""; $psr7Response = new \GuzzleHttp\Psr7\Response($response->getCode(), $response->getHeaders(), null, $response->getVersion()); if (count($psr7Response->getHeader('Sec-WebSocket-Protocol')) == 1) { $subprotoHeader = $psr7Response->getHeader('Sec-WebSocket-Protocol')[0]; } parent::onNext(new MessageSubject(new AnonymousObservable(function (ObserverInterface $observer) use($response) { $response->on('data', function ($data) use($observer) { $observer->onNext($data); }); $response->on('error', function ($e) use($observer) { $observer->onError($e); }); $response->on('close', function () use($observer) { $observer->onCompleted(); }); $response->on('end', function () use($observer) { $observer->onCompleted(); // complete the parent observer - we only do 1 connection parent::onCompleted(); }); return new CallbackDisposable(function () use($response) { // commented this out because disposal was causing the other // end (the request) to close also - which causes the pending messages // to get tossed //$response->close(); }); }), new CallbackObserver(function ($x) use($request) { $request->write($x); }, function ($e) use($request) { $request->close(); }, function () use($request) { $request->end(); }), true, $this->useMessageObject, $subprotoHeader, $cNegotiator->getRequest(), $psr7Response)); }); $request->writeHead(); }
/** * Handle any errors in the API response * * If the response doesn't contain JSON, we will fail to decode and throw a * MalFormedResponseException. * * If the response is JSON, but the status code is >= 400, then we return * the appropriate error depending on the code * * @param GuzzleHttp\Psr7\Response $response The raw API response */ private function handleErrors($response) { $json = json_decode($response->getBody()); if ($json === null) { $msg = "Malformed response received from server"; throw new Exception\MalformedResponseException($msg, $response); } if ($response->getStatusCode() < 400) { return null; } $error = $json->error; $exception_class = (string) ApiException::getErrorForType($error->type); $exception_class = 'GoCardlessPro\\Core\\Exception\\' . $exception_class; throw new $exception_class($error); }
/** * Get all papertrail events * @return array */ public function getEvents() { return json_decode((string) $this->response->getBody())->events; }
/** * @param GuzzleHttp\Psr7\Response $response * @return array */ private function convertResponse($response) { $responseHeader = $response->getHeaderLine('content-type'); if (false == preg_match('/application\\/json/', $responseHeader)) { throw new ResponseFormatException(); } return json_decode($response->getBody(), true); }