<commit_before><?php

namespace GrapheneNodeClient\Connectors\WebSocket;

use GrapheneNodeClient\Connectors\ConnectorInterface;
use WebSocket\Client;

abstract class WSConnectorAbstract implements ConnectorInterface
{
    /**
     * @var string
     */
    protected $platform;

    /**
     * wss or ws server, for example 'wss://ws.golos.io'
     *
     * @var string
     */
    protected $nodeURL;

    /**
     * waiting answer from Node during $wsTimeoutSeconds seconds
     *
     * @var int
     */
    protected $wsTimeoutSeconds = 5;

    protected static $connection;
    protected static $currentId;

    public function getConnection()
    {
        if (self::$connection === null) {
            self::$connection = new Client($this->nodeURL, ['timeout' => $this->wsTimeoutSeconds]);
        }

        return self::$connection;
    }

    public function getCurrentId()
    {
        if (self::$currentId === null) {
            self::$currentId = 1;
        }

        return self::$currentId;
    }

    public function getPlatform()
    {
        return $this->platform;
    }

    public function setCurrentId($id)
    {
        self::$currentId = $id;
    }

    public function getNextId()
    {
        $next = $this->getCurrentId() + 1;
        $this->setCurrentId($next);

        return $next;
    }

    /**
     * @param string $apiName
     * @param array  $data
     * @param string $answerFormat
     *
     * @return array|object
     */
    public function doRequest($apiName, array $data, $answerFormat = self::ANSWER_FORMAT_ARRAY)
    {
        $data = [
            'id'     => $this->getNextId(),
            'method' => 'call',
            'params' => [
                $apiName,
                $data['method'],
                $data['params']
            ]
        ];
        $connection = $this->getConnection();
        $connection->send(json_encode($data));

        $data = $connection->receive();
        $answer = json_decode($data, self::ANSWER_FORMAT_ARRAY === $answerFormat);

        return $answer;
    }
}<commit_msg>- WSConnectorAbstract.php added tries for api call before throw exception<commit_after><?php

namespace GrapheneNodeClient\Connectors\WebSocket;

use GrapheneNodeClient\Connectors\ConnectorInterface;
use WebSocket\Client;
use WebSocket\ConnectionException;

abstract class WSConnectorAbstract implements ConnectorInterface
{
    /**
     * @var string
     */
    protected $platform;

    /**
     * wss or ws server, for example 'wss://ws.golos.io'
     *
     * @var string
     */
    protected $nodeURL;

    /**
     * waiting answer from Node during $wsTimeoutSeconds seconds
     *
     * @var int
     */
    protected $wsTimeoutSeconds = 5;

    /**
     * max number of tries to get answer from API
     *
     * @var int
     */
    protected $maxNumberOfTriesToCallApi = 3;

    protected static $connection;
    protected static $currentId;

    public function getConnection()
    {
        if (self::$connection === null) {
            self::$connection = new Client($this->nodeURL, ['timeout' => $this->wsTimeoutSeconds]);
        }

        return self::$connection;
    }

    public function getCurrentId()
    {
        if (self::$currentId === null) {
            self::$currentId = 1;
        }

        return self::$currentId;
    }

    public function getPlatform()
    {
        return $this->platform;
    }

    public function setCurrentId($id)
    {
        self::$currentId = $id;
    }

    public function getNextId()
    {
        $next = $this->getCurrentId() + 1;
        $this->setCurrentId($next);

        return $next;
    }

    /**
     * @param string $apiName
     * @param array  $data
     * @param string $answerFormat
     * @param int $try_number Try number of getting answer from api
     *
     * @return array|object
     * @throws ConnectionException
     * @throws \WebSocket\BadOpcodeException
     */
    public function doRequest($apiName, array $data, $answerFormat = self::ANSWER_FORMAT_ARRAY, $try_number = 1)
    {
        $data = [
            'id'     => $this->getNextId(),
            'method' => 'call',
            'params' => [
                $apiName,
                $data['method'],
                $data['params']
            ]
        ];
        try {
            $connection = $this->getConnection();
            $connection->send(json_encode($data));

            $data = $connection->receive();
            $answer = json_decode($data, self::ANSWER_FORMAT_ARRAY === $answerFormat);
        } catch (ConnectionException $e) {
            //if got WS Exception, try to get answer again
            if ($try_number < $this->maxNumberOfTriesToCallApi) {
                $answer = $this->doRequest($apiName, $data, $answerFormat, $try_number + 1);
            } else {
                throw $e;
            }
        }

        return $answer;
    }
}
{'fraction_non_alphanumeric': 0.12530662305805396, 'fraction_numerical': 0.001839738348323794, 'mean_word_length': 2.0, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '517830', 'n_tokens_mistral': 1417, 'n_tokens_neox': 1378, 'n_words': 316}