<commit_before><?php

namespace LasseRafn\Economic\Utils;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use LasseRafn\Economic\Exceptions\EconomicClientException;
use LasseRafn\Economic\Exceptions\EconomicRequestException;

class Request
{
	public $curl;

	protected $stripNull;

	public $beforeRequestHooks = [];

	public function __construct($agreementToken = '', $apiSecret = '', $stripNull = false)
	{
		$this->curl = new Client([
			'base_uri'        => config('economic.request_endpoint'),
			'headers'         => [
				'X-AppSecretToken'      => $apiSecret,
				'X-AgreementGrantToken' => $agreementToken,
				'Content-Type'          => 'application/json',
			],
			'allow_redirects' => ['strict' => true],
		]);

		$this->stripNull = $stripNull;
	}

	public function formatData($data)
	{
		if ($this->stripNull) {
			return array_filter($data, static function ($item) { return $item !== null; });
		}

		return $data;
	}

	public function handleWithExceptions($callback)
	{
		try {
			return $callback();
		} catch (ClientException $exception) {
			$message = $exception->getMessage();
			$code    = $exception->getCode();

			if ($exception->hasResponse()) {
				$message = $exception->getResponse()->getBody()->getContents();
				$code    = $exception->getResponse()->getStatusCode();
			}

			throw new EconomicRequestException($message, $code);
		} catch (ServerException $exception) {
			$message = $exception->getMessage();
			$code    = $exception->getCode();

			if ($exception->hasResponse()) {
				$message = $exception->getResponse()->getBody()->getContents();
				$code    = $exception->getResponse()->getStatusCode();
			}

			throw new EconomicRequestException($message, $code);
		} catch (\Exception $exception) {
			$message = $exception->getMessage();
			$code    = $exception->getCode();

			throw new EconomicClientException($message, $code);
		}
	}

	public function doRequest($method, $path, $options = [])
	{
		foreach ($this->beforeRequestHooks as $hook) {
			$hook($method, $path, $options);<commit_msg>Add hooks feature<commit_after><?php

namespace LasseRafn\Economic\Utils;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use LasseRafn\Economic\Exceptions\EconomicClientException;
use LasseRafn\Economic\Exceptions\EconomicRequestException;

class Request
{
	public $curl;

	protected $stripNull;

	public $beforeRequestHooks = [];

	public function __construct($agreementToken = '', $apiSecret = '', $stripNull = false)
	{
		$this->curl = new Client([
			'base_uri'        => config('economic.request_endpoint'),
			'headers'         => [
				'X-AppSecretToken'      => $apiSecret,
				'X-AgreementGrantToken' => $agreementToken,
				'Content-Type'          => 'application/json',
			],
			'allow_redirects' => ['strict' => true],
		]);

		$this->stripNull = $stripNull;
	}

	public function formatData($data)
	{
		if ($this->stripNull) {
			return array_filter($data, static function ($item) { return $item !== null; });
		}

		return $data;
	}

	public function handleWithExceptions($callback)
	{
		try {
			return $callback();
		} catch (ClientException $exception) {
			$message = $exception->getMessage();
			$code    = $exception->getCode();

			if ($exception->hasResponse()) {
				$message = $exception->getResponse()->getBody()->getContents();
				$code    = $exception->getResponse()->getStatusCode();
			}

			throw new EconomicRequestException($message, $code);
		} catch (ServerException $exception) {
			$message = $exception->getMessage();
			$code    = $exception->getCode();

			if ($exception->hasResponse()) {
				$message = $exception->getResponse()->getBody()->getContents();
				$code    = $exception->getResponse()->getStatusCode();
			}

			throw new EconomicRequestException($message, $code);
		} catch (\Exception $exception) {
			$message = $exception->getMessage();
			$code    = $exception->getCode();

			throw new EconomicClientException($message, $code);
		}
	}

	public function doRequest($method, $path, $options = [])
	{
		try {
			foreach ($this->beforeRequestHooks as $hook) {
				$hook($method, $path, $options);
			}
		} catch (\Throwable $exception) {
			// silence hooks!!!
{'fraction_non_alphanumeric': 0.16356255969436484, 'fraction_numerical': 0.0, 'mean_word_length': 4.040914560770156, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, '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': '2059605', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1260, 'n_words': 247}