Extracts ratelimits from a response.
@param \Psr\Http\Message\ResponseInterface  $response
@return mixed[]
18469
function extractRatelimit(\Psr\Http\Message\ResponseInterface $response) {
        $date = (new \DateTime($response->getHeader('Date')[0]))->getTimestamp();
        
        $limit = ($response->hasHeader('X-RateLimit-Limit') ? ((int) $response->getHeader('X-RateLimit-Limit')[0]) : null);
        $remaining = ($response->hasHeader('X-RateLimit-Remaining') ? ((int) $response->getHeader('X-RateLimit-Remaining')[0]) : null);
        $resetTime = ($response->hasHeader('Retry-After')
            ? ((float) \bcadd(\microtime(true), (((int) $response->getHeader('Retry-After')[0]) / 1000)))
            : ($response->hasHeader('X-RateLimit-Reset') ? ((float) \bcadd(\microtime(true), (((int) $response->getHeader('X-RateLimit-Reset')[0]) - $date))) : null)
        );
        
        return \compact('limit', 'remaining', 'resetTime');
    }
450864
public function parseResponse(ResponseInterface $response)
    {
        $body = $response->getBody()->getContents();
        return [
            'code' => $response->getStatusCode(),
            'headers' => $response->getHeaders(),
            'body' => ($this->isValidJson($body)) ? json_decode($body,
                true) : $body,
        ];
    }
877000
private function buildResults(Response $response): array
    {
        $data = $response->getData();
        $results = [];

        if (!isset($data['hits']['hits'])) {
            return $results;
        }

        foreach ($data['hits']['hits'] as $hit) {
            $results[] = new Result($hit);
        }

        return $results;
    }
1095293
protected function getMultipartBodyContent(ResponseInterface $response)
    {
        $list     = [];
        $iterator = new MultipartResponseIterator($response);

        foreach ($iterator as $response) {
            $list[] = $this->createContent($response->getHeaders(), $response->getBody());
        }

        return $list;
    }
630963
public static function getMimeType(ResponseInterface $response)
    {
        $mime = strtolower($response->getHeaderLine('Content-Type'));
        $mime = explode(';', $mime, 2);

        return trim($mime[0]);
    }
502320
private function performResponse($response)
    {
        $data = false;
        if ($response instanceof ResponseInterface) {
            $data = json_decode($response->getBody()->getContents());
            $this->getValidationResponse()->validateResponseData($data);
        }

        return $data;
    }
870616
protected function getParameters(RequestInterface $request, ResponseInterface $response = null)
    {
        $params = [];
        $result = '';

        $keys = array_intersect($this->context, $this->availableParameters);

        foreach ($keys as $key) {
            switch ($key) {
                case 'method':
                    $result = $request->getMethod();
                    break;
                case 'url':
                    $result = $request->getUri()->__toString();
                    break;
                case 'request_version':
                    $result = $request->getProtocolVersion();
                    break;
                case 'response_version':
                    $result = $response ? $response->getProtocolVersion() : 'NULL';
                    break;
                case 'host':
                    $result = $request->getUri()->getHost();
                    break;
                case 'hostname':
                    $result = gethostname();
                    break;
                case 'status_code':
                    $result = $response ? $response->getStatusCode() : 'NULL';
                    break;
                case 'phrase':
                    $result = $response ? $response->getReasonPhrase() : 'NULL';
                    break;
            }

            $params[$key] = (string) $result ?: '';
        }

        return $params;
    }
902250
private function fetchErrorMessages(ResponseInterface $response): array
    {
        $responseBody = $response->getBody();
        $responseBodyInJson = json_decode($responseBody, true);

        if ($this->doesResponseBodyContainValidJson($responseBodyInJson)) {
            return $this->fetchAllErrorDescription($responseBodyInJson);
        } else {
            return [$responseBody];
        }
    }
1342830
protected function normalizeResponse(ResponseInterface $response)
    {
        if (!is_null($this->normalizer)) {
            return $this->normalizer->normalize($response);
        }

        //default responsen is decoded json
        return $response->json();
    }
850042
protected function handleResponse(ResponseInterface $response)
    {
        $this->rateLimit = new RateLimit;
        $this->rateLimit->dailyLimit = (integer) $response->getHeaderLine('RateLimit-DailyLimit');
        $this->rateLimit->remaining = (integer) $response->getHeaderLine('RateLimit-Remaining');
        $this->rateLimit->resetTime = $response->getHeaderLine('RateLimit-ResetTime');

        return $response;
    }
495605
private function getTtl(ResponseInterface $response)
    {
        $ttl = 0;

        if ($cacheControl = $response->getHeader('Cache-Control')) {
            $maxAge = Utils::getDirective($response, 'max-age');
            if (is_numeric($maxAge)) {
                $ttl += $maxAge;
            }

            // According to RFC5861 stale headers are *in addition* to any
            // max-age values.
            $stale = Utils::getDirective($response, 'stale-if-error');
            if (is_numeric($stale)) {
                $ttl += $stale;
            }
        } elseif ($expires = $response->getHeader('Expires')) {
            $ttl += strtotime($expires) - time();
        }

        return $ttl ?: $this->defaultTtl;
    }
590385
protected function getCacheTtl(Response $response)
    {
        if ($this->useHeaderTtl && $response->hasHeader('Cache-Control')) {
            $cacheControl =  $response->getHeader('Cache-Control')[0];

            if (preg_match('`max-age=(\d+)`', $cacheControl, $match)) {
                return intval($match[1]);
            }
        }

        return $this->defaultTtl;
    }
856422
public function getRange(DispatcherResponseInterface $response)
    {
        if(!isset($this->_range))
        {
            $length = $this->getFileSize($response);
            $range  = $length - 1;

            if($response->getRequest()->isStreaming())
            {
                $ranges = $response->getRequest()->getRanges();

                if (!empty($ranges[0]['last'])) {
                    $range = (int) $ranges[0]['last'];
                }

                if($range > $length - 1) {
                    $range = $length - 1;
                }
            }

            $this->_range = $range;
        }

        return $this->_range;
    }
531458
public function response(ResponseInterface $response)
    {
        $this->lastResponse = $response;

        $content = $response->getBody()->getContents();

        $decoded = json_decode($content); // parse as object
        $data = $decoded;

        if (!empty($decoded)) {
            reset($decoded);
            $data = current($decoded); // get first attribute from array, e.g.: subscription, subscriptions, errors.
        }

        $this->checkRateLimit($response)
            ->checkForErrors($response, $data);

        return $data;
    }
22046
protected function parseCurlResponse($response)
    {
        $pattern = '|HTTP/\d\.\d.*?$.*?\r\n\r\n|ims';
        preg_match($pattern, $response, $matches);
        $header = $matches[0];
        // Remove headers from the response body
        $body = str_replace($header, '', $response);

        return [
            'header' => $header,
            'body' => $body
        ];
    }
683532
protected function getResponseAsJson( ResponseInterface $response ) {
        if( $response->hasHeader('Content-Type') ) {
            $typeHeader = $response->getHeader('Content-Type');
            $contentType = array_shift($typeHeader);

            if( stripos( $contentType, 'application/json' ) === 0 ) {
                return json_decode($response->getBody(), false);
            }
        }

        return null;
    }
989981
private function getCspHeaders(Response $response)
    {
        $headers = [];

        if ($response->headers->has('Content-Security-Policy')) {
            $headers['Content-Security-Policy'] = $this->parseDirectives($response->headers->get('Content-Security-Policy'));
        }

        if ($response->headers->has('Content-Security-Policy-Report-Only')) {
            $headers['Content-Security-Policy-Report-Only'] = $this->parseDirectives($response->headers->get('Content-Security-Policy-Report-Only'));
        }

        if ($response->headers->has('X-Content-Security-Policy')) {
            $headers['X-Content-Security-Policy'] = $this->parseDirectives($response->headers->get('X-Content-Security-Policy'));
        }

        return $headers;
    }