@param ResponseInterface        $response
@param InternalRequestInterface $internalRequest
@param HttpAdapterInterface     $httpAdapter

@return HttpAdapterException
439821
private function createStatusCodeException(
        ResponseInterface $response,
        InternalRequestInterface $internalRequest,
        HttpAdapterInterface $httpAdapter
    ) {
        $exception = HttpAdapterException::cannotFetchUri(
            (string) $internalRequest->getUri(),
            $httpAdapter->getName(),
            sprintf('Status code: %d', $response->getStatusCode())
        );

        $exception->setRequest($internalRequest);
        $exception->setResponse($response);

        return $exception;
    }
511094
public function handleResponse(ResponseInterface $response, ApiResourceInterface $owner)
    {
        $className = $this->resourceClass();

        return $className::createFromResponse($response, $owner->getApi(), $owner);
    }
771729
public static function oauthException(ResponseInterface $response, $data)
    {
        return static::fromResponse(
            $response,
            isset($data['error']) ? $data['error'] : $response->getReasonPhrase()
        );
    }
968477
public function serveResponse(ResponseInterface $response)
    {
        // add increment
        $this->servedResponseCount++;
        $allowServeEmpty = $this->getConfiguration('serveEmptyResponse') === true;
        $setContentLength = $this->getConfiguration('setContentLength') === true;
        // Send response
        if (!headers_sent()) {
            // Headers
            foreach ($response->getHeaders() as $name => $values) {
                if (!$setContentLength && strtolower($name) === 'content-length') {
                    continue;
                }

                foreach ($values as $value) {
                    header(sprintf('%s: %s', $name, $value), false);
                }
            }

            // Re Set Status Header
            header(sprintf(
                'HTTP/%s %s %s',
                $response->getProtocolVersion(),
                $response->getStatusCode(),
                $response->getReasonPhrase()
            ), true, $response->getStatusCode());
        }

        // Serve Response
        if ($allowServeEmpty || ! $this->isEmptyResponseCode($response->getStatusCode())) {
            $body = $response->getBody();
            if ($body->isSeekable()) {
                $body->rewind();
            }

            $chunkSize      = $this->getNormalizeResponseChunkSize();
            $contentLength  = $setContentLength
                ? $response->getHeaderLine('Content-Length')
                : null;

            if (!$contentLength) {
                $contentLength = $body->getSize();
                // set content length
                if ($setContentLength && !headers_sent()) {
                    header(sprintf('Content-Length: %d', $contentLength));
                }
            }

            // if use buffer create buffer
            $this->addCreateBuffer();
            if ($contentLength) {
                $amountToRead = $contentLength;
                while ($amountToRead > 0 && !$body->eof()) {
                    $data = $body->read(min($chunkSize, $amountToRead));
                    echo $data;
                    $amountToRead -= strlen($data);
                    if (connection_status() != CONNECTION_NORMAL) {
                        break;
                    }
                }
            } else {
                while (!$body->eof()) {
                    echo $body->read($chunkSize);
                    if (connection_status() != CONNECTION_NORMAL) {
                        break;
                    }
                }
            }
        }
    }
277369
public static function fromResponse(ResponseInterface $response)
    {
        $responseBody = (string) $response->getBody();
        if (empty($responseBody)) {
            return new self($response->getStatusCode(), $response->getReasonPhrase());
        }

        $decodedBody = json_decode($responseBody, true);
        if (!isset($decodedBody['error'])) {
            return new self($response->getStatusCode(), $response->getReasonPhrase());
        }

        $error = $decodedBody['error'];

        return new self($response->getStatusCode(), $error['message'], $error['type'], $error);
    }
783843
public static function sendResponse(ResponseInterface $response) 
    {
		$code = $response->getStatusCode();
		$reason = $response->getReasonPhrase();
		header(
			sprintf('HTTP/%s %s %s', $response->getProtocolVersion(), $code, $reason),
			true, $code 
		);

		foreach ($response->getHeaders() as $header => $values) {
			foreach ($values as $value) {
				header("$header: $value", false);
			}
		}

		echo $response->getBody();
	}
35682
private function sendErrorDocument(
        ResponseInterface $response): ResponseInterface
        {
        if ($this->errorDocument && preg_match(
                "/^(4|5){1}[0-2]{1}[0-9]{1}$/", $response->getStatusCode())
            ) {
            $this->errorDocument = str_replace(
                "{status}", $response->getStatusCode(), $this->errorDocument
            );
            if ($this->errorDocument !== filter_input(INPUT_SERVER, "REQUEST_URI")) {
                $response = $response->withHeader(
                    Response::HEADER_REFRESH,
                    "0; " . $this->errorDocument
                );
            }
        }
        return $response;
    }
1272205
public function createFromResponse(ResponseInterface $response, UriInterface $uri = null): SitemapInterface
    {
        /* @var Sitemap $sitemap */
        $sitemap = null;
        $responseContent = $response->getBody()->getContents();

        $type = $this->identifier->getType($responseContent);

        if (empty($type)) {
            throw new \RuntimeException(
                self::EXCEPTION_UNKNOWN_TYPE_MESSAGE,
                self::EXCEPTION_UNKNOWN_TYPE_CODE
            );
        }

        $sitemap = Sitemap::createFromResponse($uri, $response, $type);

        $urlExtractor = $this->typeExtractorMap[$type];

        $sitemap->setUrlExtractor($urlExtractor);

        return $sitemap;
    }
87274
protected function getResponseWithHeaders(HttpResponse $responseConfig, ResponseInterface $httpResponse)
    {
        if ($responseConfig->getHeaders()) {
            /** @var \Mcustiel\Phiremock\Domain\Http\Header $header */
            foreach ($responseConfig->getHeaders() as $header) {
                $httpResponse = $httpResponse->withHeader(
                    $header->getName()->asString(),
                    $header->getValue()->asString()
                );
            }
        }

        return $httpResponse;
    }
1201536
private function serializeInternalRequest(InternalRequestInterface $internalRequest)
    {
        $formattedInternalRequest = $this->formatter->formatRequest($internalRequest);
        unset($formattedInternalRequest['parameters']);

        return $this->serialize($formattedInternalRequest);
    }
1337846
public function processResponse(ResponseInterface $response)
    {

        $body = $response->getBody();

        $object = json_decode($body);
        if (json_last_error() === JSON_ERROR_NONE) {
            // JSON is valid
            if (property_exists($object, '_embedded') && property_exists($object->_embedded, 'items')) {
                $return = $object->_embedded->items;
            } elseif (property_exists($object, 'error')) {
                if (is_array($object->message)) {
                    foreach ($object->message as $message) {
                        $output .= $message;
                    }
                    throw new \Exception($output);
                } else {
                    throw new \Exception($object->message);
                }
            } else {
                $return = $object;
            }
        } else {
            $return = $body;
        }

        return $return;
    }
125590
protected function checkResponseErrors(ResponseInterface $response)
	{
		$status = intval($response->getStatusCode());

		if ($status === 200) return;

		switch ($status) {
			case 401:
				throw new AuthorizationException($response);
			default:
				throw new RequestException($response);
		}
	}
664624
public function checkError(ResponseInterface $response)
    {
        if ($response->getStatusCode() < 400) {
            return $response;
        }

        //check if any limit was hit
        $this->checkUserRateLimit($response);
        $this->checkClientRateLimit($response);
        $this->checkPostRateLimit($response);

        $body = (string) $response->getBody();
        $responseData = json_decode($body, true);
        if (JSON_ERROR_NONE !== json_last_error()) {
            $responseData = $body;
        }

        if (\is_array($responseData) && isset($responseData['data']) && isset($responseData['data']['error'])) {
            $errorMessage = $responseData['data']['error'];
            // when uploading too fast, error is an array
            if (\is_array($responseData['data']['error'])) {
                $errorMessage = '';

                if (isset($responseData['data']['error']['message'])) {
                    $errorMessage = $responseData['data']['error']['message'];
                } elseif (isset($responseData['data']['error']['code'])) {
                    $errorMessage = 'Error code: ' . $responseData['data']['error']['code'];
                }
            }

            $message = 'Request failed with: "' . $errorMessage . '"';
            if (isset($responseData['data']['request'])) {
                $message = 'Request to: ' . $responseData['data']['request'] . ' failed with: "' . $errorMessage . '"';
            }

            throw new ErrorException($message, $response->getStatusCode());
        }

        throw new RuntimeException(\is_array($responseData) && isset($responseData['message']) ? $responseData['message'] : $responseData, $response->getStatusCode());
    }
604894
@Override
  public HttpResponse encode(Supplier<HttpResponse> factory, Response response) {
    int status = response.getStatus() == ErrorCodes.SUCCESS
                 ? HTTP_OK
                 : HTTP_INTERNAL_ERROR;

    byte[] data = json.toJson(getValueToEncode(response)).getBytes(UTF_8);

    HttpResponse httpResponse = factory.get();
    httpResponse.setStatus(status);
    httpResponse.setHeader(CACHE_CONTROL, "no-cache");
    httpResponse.setHeader(EXPIRES, "Thu, 01 Jan 1970 00:00:00 GMT");
    httpResponse.setHeader(CONTENT_LENGTH, String.valueOf(data.length));
    httpResponse.setHeader(CONTENT_TYPE, JSON_UTF_8.toString());
    httpResponse.setContent(data);

    return httpResponse;
  }
827553
protected function checkResponse(ResponseInterface $response, $data)
    {
        // Standard error response format
        if (!empty($data['meta']['error_type'])) {
            throw InstagramIdentityProviderException::clientException($response, $data);
        }

        // OAuthException error response format
        if (!empty($data['error_type'])) {
            throw InstagramIdentityProviderException::oauthException($response, $data);
        }
    }
237233
private function fromPsrResponse(ResponseInterface $response)
    {
        return new Response(
            $response->getStatusCode(),
            $response->getHeaders(),
            $response->getBody(),
            $response->getProtocolVersion(),
            $response->getReasonPhrase()
        );
    }
31595
protected function sendInternalRequests(array $internalRequests, $success, $error)
    {
        foreach ($internalRequests as $internalRequest) {
            try {
                $response = $this->sendInternalRequest($internalRequest);
                $response = $response->withParameter('request', $internalRequest);
                call_user_func($success, $response);
            } catch (HttpAdapterException $e) {
                $e->setRequest($internalRequest);
                $e->setResponse(isset($response) ? $response : null);
                call_user_func($error, $e);
            }
        }
    }