Create new Resource instance from HTTP response.

@param ResponseInterface    $response
@param ApiProviderInterface $api
@param ApiResourceInterface $owner    = null

@return static
500623
public static function createFromResponse(ResponseInterface $response, ApiProviderInterface $api, ApiResourceInterface $owner = null)
    {
        $json = json_decode((string) $response->getBody(), true);
        $resourceType = static::resourceType();

        if (empty($json[$resourceType])) {
            static::throwNotFoundException();
        }

        return new static($api, $json[$resourceType], $owner);
    }
740145
public function implicitCreateAskResourceOwnerForApprovalResponse(
        ClientInterface $client,
        string $redirectUri = null,
        bool $isScopeModified = false,
        array $scopeList = null,
        string $state = null,
        array $extraParameters = []
    ): ResponseInterface {
        $response = $this->getIntegration()->createAskResourceOwnerForApprovalResponse(
            ResponseTypes::IMPLICIT,
            $client,
            $redirectUri,
            $isScopeModified,
            $scopeList,
            $state,
            $extraParameters
        );

        $this->logInfo('Created response asking resource owner for scope approval (implicit grant).');

        return $response;
    }
343154
public static function create(ApiClientInterface $client, UserInterface $user = null)
    {
        $accessToken = new self();
        $accessToken->setUser($user);
        $accessToken->setClient($client);

        return $accessToken;
    }
671685
public function recreateForNewApiExceptionTransformer(ApiExceptionTransformerInterface $apiExceptionTransformer): self
    {
        return new self($this->client, $this->responseTransformer, $apiExceptionTransformer, $this->accessToken);
    }
1137372
protected function createResourceOwner(array $response, AccessToken $token)
    {
        $user = new OktaResourceOwner($response);

        return $user->setDomain($this->getBaseApiUrl());
    }
554341
public function viewBlockAction($owner, $ownerId, $id)
    {
        /** @var BlockManager $manager */
        $manager = $this->get('opifer.content.block_manager');

        $object = $this->get('opifer.content.block_provider_pool')->getProvider($owner)->getBlockOwner($ownerId);

        /** @var Environment $environment */
        $environment = $this->get('opifer.content.block_environment');
        $environment->setDraft(true)->setObject($object);

        $block = $environment->getBlock($id);

        $environment->setBlockMode(Environment::MODE_MANAGE);

        /** @var AbstractBlockService $service */
        $service = $manager->getService($block);
        $service->setEnvironment($environment);

        $this->get('opifer.content.twig.content_extension')->setBlockEnvironment($environment);


        return new JsonResponse(['view' => $service->manage($block)->getContent()]);
    }
975776
public function build(ResponseInterface $response)
    {
        $rawData = json_decode((string)$response->getBody(), true) ?: [];
        $data = ModelHelper::camelize($rawData);
        $data['data'] = ArrayHelper::getValue($rawData, 'data', []);

        if ($auth = ArrayHelper::getValue($data, 'auth')) {
            $data['auth'] = new Auth($auth);
        }

        return new Response($data);
    }
236316
protected function createResourceOwner(array $response, AccessToken $token)
	{
		$user = new YouthwebResourceOwner($response);

		return $user->setDomain($this->domain);
	}
1227204
public static function create(RequestInterface $request, ResponseInterface $response = null)
    {
        $uri = $request->getUri();

        if (is_null($response)) {
            return new static("No response error, uri: '$uri'");
        }

        // Extract error message from body
        $status = $response->getStatusCode();
        $json   = json_decode($response->getBody());
        if (json_last_error() !== JSON_ERROR_NONE) {
            return new static("Cloudflare API error ($status) on: '$uri'", $status);
        }


        // Error message
        if (isset($json->errors) && count($json->errors) >= 1) {
            return new static($json->errors[0]->message . ", uri: '$uri'", $json->errors[0]->code);
        }

        // No success, but no error
        if (isset($json->success) && !$json->success) {
            return new static("Request was unsuccessful, uri: '$uri'");
        }

        // Unknown
        return new static("Unknown error, uri: '$uri'");

    }
1043648
public function setOwner(ContentInterface $owner = null)
    {
        if ($owner instanceof Content) {
            $this->content = $owner;
        } elseif ($owner instanceof Template) {
            $this->template = $owner;
        } elseif ($owner === null) {
            $this->template = null;
            $this->content = null;
        } else {
            throw new \Exception(sprintf('BlockInterface owner can only be of type Content or Template, not the provided %s type.', get_class($owner)));
        }

        return $this;
    }
83044
public function codeCreateAskResourceOwnerForApprovalResponse(
        ClientInterface $client,
        string $redirectUri = null,
        bool $isScopeModified = false,
        array $scopeList = null,
        string $state = null,
        array $extraParameters = []
    ): ResponseInterface {
        $this->logDebug('Asking resource owner for scope approval (code grant).');
        return $this->getIntegration()->createAskResourceOwnerForApprovalResponse(
            ResponseTypes::AUTHORIZATION_CODE,
            $client,
            $redirectUri,
            $isScopeModified,
            $scopeList,
            $state,
            $extraParameters
        );
    }
245063
public static function factory(RequestInterface $request, Response $response)
    {
        $message = 'Client error response' . PHP_EOL . implode(PHP_EOL, array(
            '[status code] ' . $response->getStatusCode(),
            '[reason phrase] ' . $response->getReasonPhrase(),
            '[url] ' . $request->getUrl(),
        ));

        $e = new static($message);
        $e->setResponse($response);
        $e->setRequest($request);

        return $e;
    }
41516
public function create($owner, $repo, $content, $encoding = 'utf-8')
	{
		// Build the request path.
		$path = '/repos/' . $owner . '/' . $repo . '/git/blobs';

		$data = array(
			'content'  => $content,
			'encoding' => $encoding
		);

		return $this->processResponse(
			$this->client->post($this->fetchUrl($path), json_encode($data)),
			201
		);
	}
1272000
public static function create(CloseIoResponse $response): self
    {
        switch ($response->getHttpStatusCode()) {
            case StatusCodeInterface::STATUS_UNAUTHORIZED:
                return new CloseIoAuthenticationException($response);
            case StatusCodeInterface::STATUS_TOO_MANY_REQUESTS:
                return new CloseIoThrottleException($response);
            case StatusCodeInterface::STATUS_NOT_FOUND:
                return new CloseIoResourceNotFoundException($response);
            case StatusCodeInterface::STATUS_BAD_REQUEST:
                return new CloseIoBadRequestException($response);
            default:
                return new static($response);
        }
    }
1288129
public static function create(DmmResponse $response)
    {
        $data = $response->getDecodedBody();

        $code = isset($data['status']) ? $data['status'] : null;
        $message = isset($data['message']) ? $data['message'] : 'Unknown error from API.';

        // All others
        return new static($response, new DmmOtherException($message, $code));
    }
270261
public function createTokenResponse(
        ServerRequestInterface $request,
        Client $client = null,
        TokenOwnerInterface $owner = null
    ): ResponseInterface {
        $postParams = $request->getParsedBody();

        $code = $postParams['code'] ?? null;

        if (null === $code) {
            throw OAuth2Exception::invalidRequest('Could not find the authorization code in the request');
        }

        /* @var \ZfrOAuth2\Server\Model\AuthorizationCode $authorizationCode */
        $authorizationCode = $this->authorizationCodeService->getToken($code);

        if (null === $authorizationCode || $authorizationCode->isExpired()) {
            throw OAuth2Exception::invalidGrant('Authorization code cannot be found or is expired');
        }

        $clientId = $postParams['client_id'] ?? null;

        if ($authorizationCode->getClient()->getId() !== $clientId) {
            throw OAuth2Exception::invalidRequest(
                'Authorization code\'s client does not match with the one that created the authorization code'
            );
        }

        // If owner is null, we reuse the same as the authorization code
        $owner = $owner ?: $authorizationCode->getOwner();

        // Everything is okey, let's start the token generation!
        $scopes = $authorizationCode->getScopes(); // reuse the scopes from the authorization code

        $accessToken = $this->accessTokenService->createToken($owner, $client, $scopes);

        // Before generating a refresh token, we must make sure the authorization server supports this grant
        $refreshToken = null;

        if ($this->authorizationServer->hasGrant(RefreshTokenGrant::GRANT_TYPE)) {
            $refreshToken = $this->refreshTokenService->createToken($owner, $client, $scopes);
        }

        return $this->prepareTokenResponse($accessToken, $refreshToken);
    }
353346
protected function createApiException(Request $request, Response $response)
    {
        //$errors = json_decode((string) $response->getBody());

        return new HttpRequestException(
            $response->getCode(),
            $response->getBody(),
            $request,
            $response
        );
    }