/** * Returns a Response object. * * @route GET /?method=example.response&format=json * @route GET /?method=example.response&format=xml * @route GET /example/response.json * @route GET /example/response.xml * * @param Request $request * @return Response */ public function responseAction($request) { // accept JSON and XML $request->acceptContentTypes(array('json', 'xml')); $response = new Response(); $response->statusCode = $response->getCode(); $response->contentType = $request->getContentType(); $response->key = 'value'; return $response; }
public function run() { try { $request = $this->requestFactory->createRequest(); $controller = $this->controllerFactory->create($request); $response = $controller->run($request); } catch (AuthorizationException $e) { $response = new Response(['message' => $e->getMessage()], 401); } catch (ApiException $e) { $response = new Response(['message' => $e->getMessage()], $e->getCode() ?: 400); } http_response_code($response->getCode()); header('Content-Type: application/json'); $body = json_encode($response->getData()); header('X-Api-Signature: ' . hash_hmac('sha256', $body, $this->configuration->getPrivateKey())); echo $body; }
/** * Throw exception * * @param Response $response * * @throws Exception\ExceptionInterface * * @codeCoverageIgnore */ protected function throwExceptions(Response $response) { if ($response->hasErrors()) { throw $response->getLastError(); } $message = 'Error while execute request, code: ' . $response->getCode(); switch ($response->getCode()) { case 200: case 201: break; case 204: $message = 'No content'; break; case 400: $message = 'Bad Request'; break; case 401: $message = 'Unauthorized'; break; case 404: $message = 'Not Found'; break; case 405: $message = 'Method Not Allowed'; break; case 500: $message = 'Internal Server Error'; break; } throw new Exception\HttpException($message, $response->getCode()); }
public function testGetCode() { $response = new Response('response data', 1234); $this->assertSame(1234, $response->getCode()); }
public function testGetCode() { $response = new Response(array('_no_use' => 1)); $this->assertEquals(-1, $response->getCode()); $this->assertFalse($response->isOk()); }
/** * Get a code describing the status of this response. * * @return string|null code */ public function getCode() { return $this->isSuccessful() ? $this->getStatus() : parent::getCode(); }
/** * @param Request $request * @param bool $redirectsEnabled * @param int $maxRedirects * @return Response * @throws RequestException */ public function send(Request $request, $redirectsEnabled = false, $maxRedirects = 20) { /// Is there curl_init function if (!function_exists('curl_init')) { throw new RequestException('curl_init doesn\'t exists. Is curl extension instaled and enabled?'); } /// Dont send request without url if (!$request->getUrl()) { return new Response(); } $url = $request->getUrl(); if (!empty($this->params)) { $url .= '?' . http_build_query($this->params); } @curl_setopt($this->handle, CURLOPT_URL, $url); if ($request->getJSON() !== null) { $request->addHeaders(array('Content-Type' => 'application/json')); } $this->setCookies($request->getCookies()); $this->setHeaders($request->getHeaders()); $this->setMethod($request->getMethod()); /// Set post fields to CURL handle if (count($request->getPostFields()) > 0 || $request->getJSON()) { $fields = $request->getJSON() ? json_encode($request->getJSON()) : $request->getPostFields(); $this->setPostFields($fields, $request->getMethod(), $request->getHeader('Content-Type')); $request->setJSON(null); } /// Execute $response = curl_exec($this->handle); /// Remove content type header to not be used in further requests $request->unsetHeader('Content-Type'); /// Handle CURL error if ($response == FALSE) { throw new RequestException("CURL error [" . curl_errno($this->handle) . "]: " . curl_error($this->handle), curl_errno($this->handle)); } /// Separate response header and body /// Http 100 workaround $parts = explode("\r\n\r\nHTTP/", $response); $parts = (count($parts) > 1 ? 'HTTP/' : '') . array_pop($parts); list($headers, $body) = explode("\r\n\r\n", $parts, 2); $response = new Response(curl_getinfo($this->handle, CURLINFO_HTTP_CODE), $headers, $body); /// If cookiesEnabled then call addCookies with response cookies if ($request->isCookiesEnabled()) { $request->addCookies($response->getCookies()); } /// Are redirects enabled? (Also check redirects count) if ($redirectsEnabled && ($response->getCode() == 301 || $response->getCode() == 302 || $response->getCode() == 303) && $this->redirectCount < $maxRedirects) { $response = $this->doFollow($request, $response, $maxRedirects); } else { if ($this->redirectCount == $maxRedirects) { throw new RequestException("Maximum of " . $maxRedirects . " redirects reached."); } $this->redirectCount = 0; } return $response; }
public function getResponseCode() { return Response::getCode(); }
public function handleCommand($command, $data = null) { switch (strtolower($command)) { // string case 'summary': case 'description': $this->{$command} = $data; return $this; // string[] // string[] case 'tags': case 'schemes': $this->{$command} = array_merge($this->{$command}, self::words_split($data)); return $this; // MIME[] // MIME[] case 'consumes': case 'produces': $this->{$command} = array_merge($this->{$command}, self::translateMimeTypes(self::words_split($data))); return $this; // boolean // boolean case 'deprecated': $this->deprecated = true; return $this; case 'error': $code = Response::getCode(self::words_shift($data)); $description = $data; $Error = new Error($this, $code, $description); $this->responses[$code] = $Error; return $Error; case 'errors': foreach (self::words_split($data) as $code) { $code = Response::getCode($code); $this->responses[$code] = new Error($this, $code); } return $this; case 'path': case 'path?': case 'query': case 'query?': case 'header': case 'header?': case 'form': case 'form?': $in = rtrim($command, '?'); $Parameter = new Parameter($this, $in, $data, substr($command, -1) !== '?'); $this->parameters[] = $Parameter; return $Parameter; case 'body': case 'body?': $in = rtrim($command, '?'); $Parameter = new BodyParameter($this, $data, substr($command, -1) !== '?'); $this->parameters[] = $Parameter; return $Parameter; case 'response': $code = Response::getCode(strtolower(self::words_shift($data))); $definition = self::words_shift($data); $description = $data; $Response = new Response($this, $code, $definition, $description); $this->responses[$code] = $Response; return $Response; case 'require': $name = self::words_shift($data); $this->security[$name] = self::words_split($data); return $this; //@todo operationId } return parent::handleCommand($command, $data); }
/** * Get the response code after the request has been sent. * If redirects were allowed and several responses were received, the data references the last received response. * @return int */ public function getResponseCode() { return $this->response->getCode(); }
/** * Get the response. * @throws ApiException * @return mixed JSON response */ public function getResponse() { if ($this->status !== 200) { throw new \Exception('Something went wrong... Status: ' . $this->status . ' Body: ' . $this->body); } $jsonRawResponse = json_decode($this->body, true); $jsonResponse = new Response($jsonRawResponse); if ($jsonResponse->getCode() !== 0) { throw new ApiException($jsonResponse->getError(), $jsonResponse->getContext(), $jsonResponse->getCode()); } return $jsonResponse->getContext(); }