<commit_before><?php namespace Opbeat;

use Illuminate\Contracts\Config\Repository as Config;
use ErrorException;
use Exception;
use Opbeat\Log\Entry;
use GuzzleHttp\Client as Guzzle;

class Client
{
    const API_HTTP_METHOD     = 'POST';
    const API_HOST            = 'https://opbeat.com/api/v1';
    const API_ERRORS_ENDPOINT = '/organizations/%s/apps/%s/errors/';

    protected $organization_id;
    protected $app_id;
    protected $access_token;

    public function __construct(Config $config)
    {
        $this->organization_id  = $config->get('opbeat.organization_id');
        $this->app_id           = $config->get('opbeat.app_id');
        $this->access_token     = $config->get('opbeat.access_token');
        $enableExceptionHandler = $config->get('opbeat.enable_exception_handler', true);
        $enableErrorHandler     = $config->get('opbeat.enable_error_handler', true);

        if ($enableExceptionHandler) {
            $this->enableExceptionHandler();
        }

        if ($enableErrorHandler) {
            $this->enableErrorHandler();
        }
    }

    public function catchException(Exception $exception)
    {
        static $client;
        if (!isset($client)) {
            $client = new Guzzle;
        }

        $client->post($this->errorsEndpoint(), [
            'headers' => [
                'Authorization' => 'Bearer '.$this->access_token
            ],
            'json' => Entry::create($exception)
        ]);
    }

    public function handleError($no, $message, $filename = null, $lineNumber = null, $context = null)
    {
        throw new ErrorException($message, 0, $no, $filename, $lineNumber);
    }

    public function enableExceptionHandler()
    {
        set_exception_handler($this->exceptionHandler());
    }

    public function enableErrorHandler()
    {
        set_error_handler($this->errorHandler());
    }

    public function exceptionHandler()
    {
        return [$this, 'catchException'];
    }

    public function errorHandler()
    {
        return [$this, 'handleError'];
    }

    protected function errorsEndpoint()
    {
        static $endpoint;
        if (!isset($endpoint)) {
            $endpoint = self::API_HOST.sprintf(self::API_ERRORS_ENDPOINT, $this->organization_id, $this->app_id);
        }
        return $endpoint;
    }
}
<commit_msg>Utilize async requests through Guzzle<commit_after><?php namespace Opbeat;

use Illuminate\Contracts\Config\Repository as Config;
use ErrorException;
use Exception;
use Opbeat\Log\Entry;
use GuzzleHttp\Client as Guzzle;

class Client
{
    const API_HTTP_METHOD     = 'POST';
    const API_HOST            = 'https://opbeat.com/api/v1';
    const API_ERRORS_ENDPOINT = '/organizations/%s/apps/%s/errors/';

    protected $organization_id;
    protected $app_id;
    protected $access_token;

    public function __construct(Config $config)
    {
        $this->organization_id  = $config->get('opbeat.organization_id');
        $this->app_id           = $config->get('opbeat.app_id');
        $this->access_token     = $config->get('opbeat.access_token');
        $enableExceptionHandler = $config->get('opbeat.enable_exception_handler', true);
        $enableErrorHandler     = $config->get('opbeat.enable_error_handler', true);

        if ($enableExceptionHandler) {
            $this->enableExceptionHandler();
        }

        if ($enableErrorHandler) {
            $this->enableErrorHandler();
        }
    }

    public function catchException(Exception $exception)
    {
        static $client;
        if (!isset($client)) {
            $client = new Guzzle;
        }

        $client->post($this->errorsEndpoint(), [
            'future' => true,
            'headers' => [
                'Authorization' => 'Bearer '.$this->access_token
            ],
            'json' => Entry::create($exception)
        ]);
    }

    public function handleError($no, $message, $filename = null, $lineNumber = null, $context = null)
    {
        throw new ErrorException($message, 0, $no, $filename, $lineNumber);
    }

    public function enableExceptionHandler()
    {
        set_exception_handler($this->exceptionHandler());
    }

    public function enableErrorHandler()
    {
        set_error_handler($this->errorHandler());
    }

    public function exceptionHandler()
    {
        return [$this, 'catchException'];
    }

    public function errorHandler()
    {
        return [$this, 'handleError'];
    }

    protected function errorsEndpoint()
    {
        static $endpoint;
        if (!isset($endpoint)) {
            $endpoint = self::API_HOST.sprintf(self::API_ERRORS_ENDPOINT, $this->organization_id, $this->app_id);
        }
        return $endpoint;
    }
}

{'fraction_non_alphanumeric': 0.13055438002539146, 'fraction_numerical': 0.0008463817181548879, 'mean_word_length': 2.511887072808321, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 44, 'https://': 2, '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': '6492235', 'n_tokens_mistral': 1356, 'n_tokens_neox': 1316, 'n_words': 254}