<commit_before><?php

namespace Vssl\Render;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use Journey\Cache\CacheAdapterInterface;
use Psr\Http\Message\RequestInterface;

class PageApi
{
    /**
     * Guzzle HTTP interface.
     *
     * @var \GuzzleHTTP\Client
     */
    protected $http;

    /**
     * A cache adapter for storing results.
     *
     * @var \Journey\Cache\CacheAdapterInterface
     */
    protected $cache;

    /**
     * Number of seconds responses should be cached for.
     *
     * @var integer
     */
    protected $ttl;

    /**
     * Initialize the page api methods.
     */
    public function __construct(RequestInterface $request, array $config)
    {
        $this->ttl = $config['cache_ttl'];
        $this->cache = $config['cache'];
        $this->http = new Client([
            'base_uri' => $config['base_uri'],
            'headers' => [
                'X-Render-Host' => $request->getUri()->getHost(),
            ]
        ]);
    }

    /**
     * Get a particular page from the api.
     *
     * @param  string $path path of the page
     * @return array|false
     */
    public function getPage($path)
    {
        return $this->call("get", "/api/pages?slug=" . $path);
    }

    /**
     * Get a particular page from the api.
     *
     * @param  array $ids unique ids of pages to fetch data for.
     * @return array|false
     */
    public function getPagesById($ids)
    {
        $ids = array_filter(array_map(function ($id) {
            return is_numeric($id) ? (integer) $id : null;
        }, $ids));
        return $this->call("get", "/api/pages?ids=" . implode(",", $ids));
    }

    /**
     * Call a particular api endpoint.
     * @param string $method
     * @param string $url
     * @return \Psr\Http\Message\ResponseInterface|false
     */
    public function call($method, $url)
    {
        $cacheKey = strtoupper($method) . "::" . $url;
        try {
            if (!$value = $this->cache->get($cacheKey)) {
                $response = $this->http->request(strtoupper($method), $url);
            }
        } catch (ClientException $e) {
            $response = $e->getResponse();
        } catch (ConnectException $e) {
            // Suppress network errors at this level.
        }
        if (!isset($response) && !empty($value)) {
            $response = \GuzzleHttp\Psr7\parse_response($value);
        } elseif ($this->ttl !== false && !empty($response)) {
            $this->cache->set($cacheKey, \GuzzleHttp\Psr7\str($response), $this->ttl ? time() + $this->ttl : 0);
        }<commit_msg>Adds method to get a single VSSL page by page id<commit_after><?php

namespace Vssl\Render;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use Journey\Cache\CacheAdapterInterface;
use Psr\Http\Message\RequestInterface;

class PageApi
{
    /**
     * Guzzle HTTP interface.
     *
     * @var \GuzzleHTTP\Client
     */
    protected $http;

    /**
     * A cache adapter for storing results.
     *
     * @var \Journey\Cache\CacheAdapterInterface
     */
    protected $cache;

    /**
     * Number of seconds responses should be cached for.
     *
     * @var integer
     */
    protected $ttl;

    /**
     * Initialize the page api methods.
     */
    public function __construct(RequestInterface $request, array $config)
    {
        $this->ttl = $config['cache_ttl'];
        $this->cache = $config['cache'];
        $this->http = new Client([
            'base_uri' => $config['base_uri'],
            'headers' => [
                'X-Render-Host' => $request->getUri()->getHost(),
            ]
        ]);
    }

    /**
     * Get a particular page from the api.
     *
     * @param  string $path path of the page
     * @return array|false
     */
    public function getPage($path)
    {
        return $this->call("get", "/api/pages?slug=" . $path);
    }

    /**
     * Get a particular page from the api.
     *
     * @param  array $ids unique ids of pages to fetch data for.
     * @return array|false
     */
    public function getPageById($id)
    {
        return $this->call("get", "/api/pages?ids=" . $id);
    }

    /**
     * Get a set of pages from the api.
     *
     * @param  array $ids unique ids of pages to fetch data for.
     * @return array|false
     */
    public function getPagesById($ids)
    {
        $ids = array_filter(array_map(function ($id) {
            return is_numeric($id) ? (integer) $id : null;
        }, $ids));
        return $this->call("get", "/api/pages?ids=" . implode(",", $ids));
    }

    /**
     * Call a particular api endpoint.
     * @param string $method
     * @param string $url
     * @return \Psr\Http\Message\ResponseInterface|false
     */
    public function call($method, $url)
    {
        $cacheKey = strtoupper($method) . "::" . $url;
        try {
            if (!$value = $this->cache->get($cacheKey)) {
                $response = $this->http->request(strtoupper($method), $url);
            }
        } catch (ClientException $e) {
            $response = $e->getResponse();
        } catch (ConnectException $e) {
            // Suppress network errors at this level.
        }
        if (!isset($response) && !empty($value)) {
            $response = \GuzzleHttp\Psr7\parse_response($value);
        } elseif ($this->ttl !== false && !empty($response)) {
            $this->cache->set($cacheKey, \GuzzleHttp\Psr7\str($response), $this->ttl ? time() + $this->ttl : 0);
        }
{'fraction_non_alphanumeric': 0.14989255014326647, 'fraction_numerical': 0.0010744985673352436, 'mean_word_length': 2.033677349266703, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 44, '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': '2306192', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1679, 'n_words': 404}