Example #1
0
 private function getMessage(HttpResponse $response) : string
 {
     $dom = domdocument_load_html($response->getBody());
     if ($dom->getElementsByTagName('description')->length === 0) {
         return 'I dun goofed';
     }
     preg_match('/([^:]+)/', $dom->getElementsByTagName('description')->item(2)->textContent, $before);
     preg_match('/\\:(.*)/', $dom->getElementsByTagName('description')->item(2)->textContent, $after);
     return '**[' . $before[0] . '](http://www.dictionary.com/browse/' . str_replace(" ", "-", $before[0]) . ')**' . $after[0];
 }
Example #2
0
 private function getMessageFromSearch2(Response $response) : string
 {
     $internalErrors = libxml_use_internal_errors(true);
     $dom = new \DOMDocument();
     $dom->loadHTML($response->getBody());
     libxml_use_internal_errors($internalErrors);
     $xpath = new \DOMXPath($dom);
     $firstResult = $dom->getElementById('quickref_functions')->getElementsByTagName('li')->item(0);
     var_dump($firstResult);
     return sprintf('[ [%s](%s) ] %s', $firstResult->textContent, 'https://php.net' . $firstResult->getElementsByTagName('a')->item(0)->getAttribute('href'), 'foo');
 }
Example #3
0
 private function getMessage(Response $response) : string
 {
     $internalErrors = libxml_use_internal_errors(true);
     $dom = new \DOMDocument();
     $dom->loadHTML($response->getBody());
     libxml_use_internal_errors($internalErrors);
     if ($dom->getElementsByTagName('resultset')->length === 0) {
         return 'I cannot find that title.';
     }
     $result = $dom->getElementsByTagName('imdbentity')->item(0);
     return sprintf('[ [%s](%s) ] %s', $result->firstChild->wholeText, 'http://www.imdb.com/title/' . $result->getAttribute('id'), $result->getElementsByTagName('description')->item(0)->textContent);
 }
Example #4
0
 private function getMessage(HttpResponse $response) : string
 {
     $dom = domdocument_load_html($response->getBody());
     if ($dom->getElementsByTagName('resultset')->length === 0) {
         return 'I cannot find that title.';
     }
     /** @var \DOMElement $result */
     $result = $dom->getElementsByTagName('imdbentity')->item(0);
     /** @var \DOMText $titleNode */
     $titleNode = $result->firstChild;
     return sprintf('[ [%s](%s) ] %s', $titleNode->wholeText, 'http://www.imdb.com/title/' . $result->getAttribute('id'), $result->getElementsByTagName('description')->item(0)->textContent);
 }
 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 #6
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 #7
0
 /**
  * @param Response $response
  * @return \GithubService\RateLimit|null
  */
 public static function createFromResponse(Response $response)
 {
     $limit = null;
     $remaining = null;
     $resetTime = null;
     if ($response->hasHeader('X-RateLimit-Limit') == true) {
         $limitHeaders = $response->getHeader('X-RateLimit-Limit');
         foreach ($limitHeaders as $value) {
             $limit = $value;
         }
     }
     if ($response->hasHeader('X-RateLimit-Remaining') == true) {
         $remainingHeaders = $response->getHeader('X-RateLimit-Remaining');
         foreach ($remainingHeaders as $value) {
             $remaining = $value;
         }
     }
     if ($response->hasHeader('X-RateLimit-Reset') == true) {
         $resetTimeHeaders = $response->getHeader('X-RateLimit-Reset');
         foreach ($resetTimeHeaders as $value) {
             $resetTime = $value;
         }
     }
     if ($limit !== null && $remaining !== null && $resetTime !== null) {
         return new RateLimit($limit, $remaining, $resetTime);
     }
     return null;
 }
Example #8
0
 public function testGetAndSetReason()
 {
     $request = new Response();
     $request->setReason("I'M A LITTLE TEAPOT");
     $this->assertEquals("I'M A LITTLE TEAPOT", $request->getReason());
 }
Example #9
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);
 }
Example #10
0
 private function getMessageFromSearch(HttpResponse $response) : \Generator
 {
     try {
         $dom = domdocument_load_html($response->getBody());
         /** @var \DOMElement $firstResult */
         $firstResult = $dom->getElementById("quickref_functions")->getElementsByTagName("li")->item(0);
         /** @var \DOMElement $anchor */
         $anchor = $firstResult->getElementsByTagName("a")->item(0);
         $response = (yield $this->httpClient->request(self::URL_BASE . $anchor->getAttribute("href")));
         return $this->getMessageFromMatch($response);
     } catch (\Throwable $e) {
         return 'Something went badly wrong with that lookup... ' . $e->getMessage();
     }
 }
 /**
  * Dispatch the request for this operation and process the response. Allows you to
  * modify the request before it is sent.
  *
  * @return mixed
  * @param \Amp\Artax\Response $response The HTTP response.
  */
 public function processResponse(\Amp\Artax\Response $response)
 {
     return $response->getBody();
 }
 /**
  * @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());
 }
 /**
  * @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;
 }
 protected function extractData(Response $response)
 {
     $contentType = null;
     if ($response->hasHeader('Content-Type') === false) {
         throw new GithubArtaxServiceException("Content-Type header not set in response.");
     }
     $contentTypeHeaders = $response->getHeader('Content-Type');
     foreach ($contentTypeHeaders as $contentTypeHeader) {
         $parts = explode(';', $contentTypeHeader);
         foreach ($parts as $part) {
             switch ($part) {
                 case 'application/x-www-form-urlencoded':
                     $formEncoded = $response->getBody();
                     $data = [];
                     parse_str($formEncoded, $data);
                     return $data;
                 case 'application/json':
                     return $this->decodeJson($response);
             }
         }
     }
     $headersString = implode(',', $contentTypeHeaders);
     throw new GithubArtaxServiceException("Could not determine how to extract data from {$headersString}");
 }
Example #15
0
 private function getStatus(HttpResponse $response) : string
 {
     return sprintf('%s %s', $response->getStatus(), $response->getReason());
 }
Example #16
0
 private function saveNonce(Response $response)
 {
     if (!$response->hasHeader("replay-nonce")) {
         return;
     }
     list($nonce) = $response->getHeader("replay-nonce");
     $this->nonces[] = $nonce;
 }
 /**
  * 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;
 }
Example #18
0
 private function inflateGzipBody(Response $response)
 {
     $src = $response->getBody();
     if (is_resource($src)) {
         $destination = fopen('php://memory', 'r+');
         fseek($src, 10, SEEK_SET);
         stream_filter_prepend($src, 'zlib.inflate', STREAM_FILTER_READ);
         stream_copy_to_stream($src, $destination);
         rewind($destination);
         $response->setBody($destination);
     } elseif (strlen($src)) {
         $body = gzdecode($src);
         $response->setBody($body);
     }
 }
 /**
  * @param Response $response
  * @return GithubPaginator|null
  */
 public static function constructFromResponse(Response $response)
 {
     //TODO - make this not nullable
     if ($response->hasHeader('Link') == false) {
         return null;
     }
     $linkHeaders = $response->getHeader('Link');
     $instance = new self($linkHeaders);
     return $instance;
 }