static function createFromResponse(Response $response, Operation $operation)
 {
     if ($response->getStatus() == 204) {
         $instance = new static();
         $instance->iStarred = true;
         return $instance;
     } else {
         if ($response->getStatus() == 404) {
             $instance = new static();
             $instance->iStarred = false;
             return $instance;
         }
     }
     throw new BadResponseException("Unexpected status of " . $response->getStatus());
 }
Example #2
0
 private function getMessage(Response $response) : string
 {
     if ($response->getStatus() !== 200) {
         return "Something seems to be not working fine. Please visit https://tryhaskell.org/ directly.";
     }
     $data = json_decode($response->getBody(), true);
     if (isset($data["success"])) {
         $output = str_replace("`", "\\`", preg_replace("~\\s~", " ", implode(" ", $data["success"]["stdout"])));
         return sprintf("Return value: `%s` — Output: %s", str_replace("`", "\\`", $data["success"]["value"]), $output ? "`{$output}`" : "*none*");
     } else {
         if (isset($data["error"])) {
             return sprintf("Error: `%s`", str_replace("`", "\\`", preg_replace("~\\s~", " ", $data["error"])));
         }
     }
     return "Something went wrong, it has to be fixed in code.";
 }
Example #3
0
 public function testGetAndSetStatus()
 {
     $request = new Response();
     $request->setStatus(200);
     $this->assertEquals(200, $request->getStatus());
 }
Example #4
0
 /**
  * Generates a new exception using the response to provide details.
  *
  * @param Response $response HTTP response to generate the exception from
  * @return AcmeException exception generated from the response body
  */
 private function generateException(Response $response)
 {
     $body = $response->getBody();
     $status = $response->getStatus();
     $info = json_decode($body);
     $uri = $response->getRequest()->getUri();
     if (isset($info->type, $info->detail)) {
         return new AcmeException("Invalid response: {$info->detail}.\nRequest URI: {$uri}.", $info->type);
     }
     return new AcmeException("Invalid response: {$body}.\nRequest URI: {$uri}.", $status);
 }
 /**
  * Inspect the response and return an exception if it is an error response.
  *      * Exceptions should extend \ArtaxServiceBuilder\BadResponseException
  *
  * @return BadResponseException
  */
 public function translateResponseToException(\Amp\Artax\Response $response)
 {
     $status = $response->getStatus();
     if ($status < 200 || $status >= 300) {
         return new BadResponseException("Status {$status} is not treated as OK.", $response);
     }
     return null;
 }
 /**
  * @param Request $request
  * @param Response $response
  * @return BadResponseException|OneTimePasswordAppException|OneTimePasswordSMSException|null|string
  */
 public function translateResponseToException(Response $response)
 {
     $status = $response->getStatus();
     if ($status == 401 || $status == 406) {
         //@TODO - find a list of what the status codes are meant to be.
         if ($response->hasHeader('X-GitHub-OTP')) {
             $otpArray = $response->getHeader('X-GitHub-OTP');
             foreach ($otpArray as $otp) {
                 if (stripos($otp, "sms") !== false) {
                     return new OneTimePasswordSMSException("SMS OTP required", $response);
                 }
                 if (stripos($otp, "app") !== false) {
                     return new OneTimePasswordAppException("App OTP required", $response);
                 }
             }
         }
     }
     try {
         $newRateLimit = \GithubService\RateLimit::createFromResponse($response);
         if ($newRateLimit) {
             if ($newRateLimit->remaining <= 0) {
                 $resetsInSeconds = $newRateLimit->resetTime - time();
                 return new BadResponseException("Request rate limit has been exceeded, try again in {$resetsInSeconds} seconds.", $response);
             }
         }
     } catch (\Exception $e) {
         // Something went wrong when creating the ratelimit object
         // We don't care, the user only cares about the actual request.
     }
     if ($status < 200 || $status >= 300 && $status != 304) {
         return new BadResponseException("Status {$status} is not treated as OK.", $response);
     }
     return null;
 }
Example #7
0
 private function getStatus(HttpResponse $response) : string
 {
     return sprintf('%s %s', $response->getStatus(), $response->getReason());
 }
 /**
  * @return ResponseInterface
  */
 protected function convertResponse(Response $artaxResponse, RequestInterface $request, array $requestOptions)
 {
     return new \GuzzleHttp\Psr7\Response($artaxResponse->getStatus(), $artaxResponse->getAllHeaders(), $artaxResponse->getBody(), $artaxResponse->getProtocol(), $artaxResponse->getReason());
 }