<commit_before><?php

namespace seekat\API\Call;

use http\Client\Response;
use http\Header;
use seekat\API;
use seekat\Exception\RequestException;

class Result
{
	private $api;

	function __construct(API $api) {
		$this->api = $api;
	}

	function __invoke(Response $response) : API {
		$url = $this->api->getUrl();
		$log = $this->api->getLogger();
		$log->info(($response->getHeader("X-Cache-Time") ? "cached" : "enqueued")." -> response", [
			"url" => (string) $url,
			"info" => $response->getInfo(),
		]);

		if ($response->getResponseCode() >= 400) {
			$e = new RequestException($response);

			$log->critical(__FUNCTION__.": ".$e->getMessage(), [
				"url" => (string) $url,
			]);

			throw $e;
		}

		if (!($type = $response->getHeader("Content-Type", Header::class))) {
			$e = new RequestException($response);
			$log->error(
				__FUNCTION__.": Empty Content-Type -> ".$e->getMessage(), [
				"url" => (string) $url,
			]);
			throw $e;
		}

		try {
			$type = new API\ContentType($type);
			$data = $type->parseBody($response->getBody());

			if (($link = $response->getHeader("Link", Header::class))) {
				$links = new API\Links($link);
			} else {
				$links = null;
			}

			$this->api = $this->api->with(compact("type", "data", "links"));
		} catch (\Exception $e) {
			$log->error(__FUNCTION__.": ".$e->getMessage(), [
				"url" => (string) $url
			]);

			throw $e;
		}

		return $this->api;
	}
}<commit_msg>refactor<commit_after><?php

namespace seekat\API\Call;

use http\Client\Response;
use http\Header;
use seekat\API;
use seekat\Exception\RequestException;

final class Result
{
	private $api;

	function __construct(API $api) {
		$this->api = $api;
	}

	function __invoke(Response $response) : API {
		$hit = $response->getHeader("X-Cache-Time") ? "cached" : "enqueued";
		$this->api->getLogger()->info("$hit -> response", [
			"url"  => (string) $this->api->getUrl(),
			"info" => $response->getInfo(),
		]);

		$links = $this->checkResponseMeta($response);
		$type = $this->checkResponseType($response);

		try {
			$data = $type->parseBody($response->getBody());
		} catch (\Exception $e) {
			$this->api->getLogger()->error("response -> error: ".$e->getMessage(), [
				"url" => (string) $this->api->getUrl(),
			]);

			throw $e;
		}

		$this->api = $this->api->with(compact("type", "data", "links"));

		return $this->api;
	}

	/**
	 * @param Response $response
	 * @return null|API\Links
	 * @throws RequestException
	 */
	private function checkResponseMeta(Response $response) {
		if ($response->getResponseCode() >= 400) {
			$e = new RequestException($response);

			$this->api->getLogger()->critical("response -> error: ".$e->getMessage(), [
				"url" => (string) $this->api->getUrl(),
			]);

			throw $e;
		}

		if (($link = $response->getHeader("Link", Header::class))) {
			$links = new API\Links($link);
		} else {
			$links = null;
		}

		return $links;
	}

	private function checkResponseType(Response $response) {
		if (!($type = $response->getHeader("Content-Type", Header::class))) {
			$e = new RequestException($response);

			$this->api->getLogger()->error("response -> error: Empty Content-Type -> ".$e->getMessage(), [
				"url" => (string) $this->api->getUrl(),
			]);

			throw $e;
		}

		return new API\ContentType($type);
	}
}
{'fraction_non_alphanumeric': 0.22002434570906879, 'fraction_numerical': 0.0018259281801582471, 'mean_word_length': 3.675675675675676, 'pattern_counts': {'":': 3, '<': 5, '<?xml version=': 0, '>': 82, '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': '2063497', 'n_tokens_mistral': 1296, 'n_tokens_neox': 1129, 'n_words': 220}