<commit_before><?php
namespace CkTools\Controller\Component;

use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
use CkTools\Lib\ApiReturnCode;

/**
 * Api component
 */
class ApiComponent extends Component
{

    /**
     * Used Components
     *
     * @var array
     */
    public $components = [
        'RequestHandler'
    ];

    /**
     * Default configuration.
     *
     * @var array
     */
    protected $_defaultConfig = [];

    /**
     * Should be called in the controller's beforeFilter callback
     *
     * @return void
     */
    public function setup()
    {
        $this->RequestHandler->addInputType('json', ['json_decode', true]);
        $this->RequestHandler->prefers('json');
        // Force a JSON response regardless of extension
        // $this->RequestHandler->renderAs($this->_registry->getController(), 'json');
    }

    /**
     * Returns a standartized JSON response
     *
     * @param int $httpStatusCode HTTP Status Code to send
     * @param string $returnCode A string code more specific to the result
     * @param array $data Data for the 'data' key
     * @return Response
     */
    public function response($httpStatusCode = 200, $returnCode = ApiReturnCode::SUCCESS, array $data = [])
    {
        $response = $this->_registry->getController()->response;
        $response->statusCode($httpStatusCode);

        $responseData = [
            'code' => $returnCode,
            'data' => $data
        ];
        $response->type('json');
        $response->body(json_encode($responseData));

        return $response;
    }
}<commit_msg>added api token generation<commit_after><?php
namespace CkTools\Controller\Component;

use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
use CkTools\Lib\ApiReturnCode;

/**
 * Api component
 */
class ApiComponent extends Component
{

    /**
     * Used Components
     *
     * @var array
     */
    public $components = [
        'RequestHandler'
    ];

    /**
     * Default configuration.
     *
     * @var array
     */
    protected $_defaultConfig = [];

    /**
     * Should be called in the controller's beforeFilter callback
     *
     * @return void
     */
    public function setup()
    {
        $this->RequestHandler->addInputType('json', ['json_decode', true]);
        $this->RequestHandler->prefers('json');
        // Force a JSON response regardless of extension
        // $this->RequestHandler->renderAs($this->_registry->getController(), 'json');
    }

    /**
     * Returns a standartized JSON response
     *
     * @param int $httpStatusCode HTTP Status Code to send
     * @param string $returnCode A string code more specific to the result
     * @param array $data Data for the 'data' key
     * @return Response
     */
    public function response($httpStatusCode = 200, $returnCode = ApiReturnCode::SUCCESS, array $data = [])
    {
        $response = $this->_registry->getController()->response;
        $response->statusCode($httpStatusCode);

        $responseData = [
            'code' => $returnCode,
            'data' => $data
        ];
        $response->type('json');
        $response->body(json_encode($responseData));

        return $response;
    }

    /**
     * Generates a unique API token
     *
     * @return string
     */
    public function generateApiToken()
    {
        return bin2hex(openssl_random_pseudo_bytes(16));
    }
}
{'fraction_non_alphanumeric': 0.12649373360536287, 'fraction_numerical': 0.0026231419411250363, 'mean_word_length': 2.398019801980198, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '5819233', 'n_tokens_mistral': 941, 'n_tokens_neox': 926, 'n_words': 231}