/** * @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; }
private function canDecompressResponseBody(Response $response) { if (!$this->hasZlib) { return false; } if (!$response->hasHeader('Content-Encoding')) { return false; } $contentEncodingHeader = trim(current($response->getHeader('Content-Encoding'))); return strcasecmp($contentEncodingHeader, 'gzip') === 0; }
private function saveNonce(Response $response) { if (!$response->hasHeader("replay-nonce")) { return; } list($nonce) = $response->getHeader("replay-nonce"); $this->nonces[] = $nonce; }
/** * @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}"); }
/** * @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; }