<?php
namespace Eadesigndev\xCurier\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Framework\HTTP\ClientInterface;

#[\AllowDynamicProperties]
class Api extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected ClientInterface $_client;

    const API_KEY = "9d6a51fee01193779427";
    const API_KEY_FIELD = "cheie_acces";
    const API_URL = "https://app.xcurier.ro/api/";

    public function __construct(
        ClientInterface $client,
        Context $context
    )
    {
        $this->_client = $client;
        parent::__construct($context);
    }

    public function call($method, $dataToSend = [])
    {
        if (!isset($dataToSend[self::API_KEY_FIELD])) {
            $dataToSend[self::API_KEY_FIELD] = self::API_KEY;
        }

        $this->_client->addHeader("Content-Type", "application/json");
        $this->_client->post(self::API_URL . $method, json_encode($dataToSend));

        $response = $this->_client->getBody();

        return $response;
    }
}