<commit_before><?php

namespace FilePreviews;

require '../vendor/autoload.php';

class FilePreviews {
    const VERSION = '1.0.0';
    const API_URL = 'https://api.filepreviews.io/v2/';

    public function __construct(array $config = [])
    {
        $config = array_merge([
            'api_url' => static::API_URL,
            'debug' => false,
        ], $config);

        $this->client = new FilePreviewsClient(
            $config['api_key'],
            $config['api_secret'],
            $config['api_url']
        );
    }

    public function generate($url, array $params = [])
    {
        $params = array_merge([
            'url' => $url,
            'metadata' => []
        ], $params);

        $metadata = array_unique($params['metadata']);

        if(empty($metadata))
        {
            unset($params['metadata']);
        }

        if(isset($params['size']))
        {
            $size = $params['size'];
            unset($params['size']);

            $geometry = '';
            $size = array_merge([
                'height' => null,
                'width' => null
            ], $size);

            if ($size['width'] !== null)
            {
                $geometry = "{$size['width']}";
            }

            if ($size['height'] !== null)
            {
                $geometry = "{$geometry}x{$size['height']}";
            }

            $params['sizes'] = [$geometry];
        }

        return $this->client->post('previews/', json_encode($params));
    }

    public function retrieve($preview_id)
    {
        return $this->client->get("previews/$preview_id/");
    }
}
<commit_msg>Add support for keys from environment<commit_after><?php

namespace FilePreviews;

require '../vendor/autoload.php';

class FilePreviews {
    const VERSION = '1.0.0';
    const API_URL = 'https://api.filepreviews.io/v2/';
    const API_KEY_ENV_NAME = 'FILEPREVIEWS_API_KEY';
    const API_SECRET_ENV_NAME = 'FILEPREVIEWS_API_SECRET';

    public function __construct(array $config = [])
    {
        $config = array_merge([
            'api_key' => getenv(static::API_KEY_ENV_NAME),
            'api_secret' => getenv(static::API_SECRET_ENV_NAME),
            'api_url' => static::API_URL,
            'debug' => false,
        ], $config);

        if (!$config['api_key']) {
            throw new \Exception('Required "api_key" key not supplied.');
        }

        if (!$config['api_secret']) {
            throw new \Exception('Required "api_secret" key not supplied.');
        }

        $this->client = new FilePreviewsClient(
            $config['api_key'],
            $config['api_secret'],
            $config['api_url']
        );
    }

    public function generate($url, array $params = [])
    {
        $params = array_merge([
            'url' => $url,
            'metadata' => []
        ], $params);

        $metadata = array_unique($params['metadata']);

        if(empty($metadata))
        {
            unset($params['metadata']);
        }

        if(isset($params['size']))
        {
            $size = $params['size'];
            unset($params['size']);

            $geometry = '';
            $size = array_merge([
                'height' => null,
                'width' => null
            ], $size);

            if ($size['width'] !== null)
            {
                $geometry = "{$size['width']}";
            }

            if ($size['height'] !== null)
            {
                $geometry = "{$geometry}x{$size['height']}";
            }

            $params['sizes'] = [$geometry];
        }

        return $this->client->post('previews/', json_encode($params));
    }

    public function retrieve($preview_id)
    {
        return $this->client->get("previews/$preview_id/");
    }
}

{'fraction_non_alphanumeric': 0.1684322033898305, 'fraction_numerical': 0.00211864406779661, 'mean_word_length': 1.7172661870503596, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '226716', 'n_tokens_mistral': 1112, 'n_tokens_neox': 1091, 'n_words': 187}