<?php
declare(strict_types=1);

namespace App\Framework\Api;

use App\Framework\HttpClient\ClientResponse;
use RuntimeException;

class ApiException extends RuntimeException
{
    public function __construct(
        string $message,
        int $code,
        private readonly ClientResponse $response
    ) {
        parent::__construct($message, $code);
    }

    public function getResponse(): ClientResponse
    {
        return $this->response;
    }

    public function getResponseData(): ?array
    {
        try {
            return json_decode($this->response->body, true, 512, JSON_THROW_ON_ERROR);
        } catch (\JsonException) {
            return null;
        }
    }
}
