<commit_before><?php

namespace Modularity\Helper;

class Curl
{
    /**
     * Curl request
     * @param  string $type        Request type
     * @param  string $url         Request url
     * @param  array $data         Request data
     * @param  string $contentType Content type
     * @param  array $headers      Request headers
     * @return string              The request response
     */
    public static function request($type, $url, $data = null, $contentType = 'json', $headers = null)
    {
        $arguments = null;

        switch (strtoupper($type)) {
            /**
             * Method: GET
             */
            case 'GET':
                // Append $data as querystring to $url
                if (is_array($data)) {
                    $url .= '?' . http_build_query($data);
                }

                // Set curl options for GET
                $arguments = array(
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_HEADER         => false,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_SSL_VERIFYPEER => false,
                    CURLOPT_SSL_VERIFYHOST => false,
                    CURLOPT_URL            => $url
                );

                break;

            /**
             * Method: POST
             */
            case 'POST':
                // Set curl options for POST
                $arguments = array(
                    CURLOPT_RETURNTRANSFER => 1,
                    CURLOPT_URL            => $url,
                    CURLOPT_POST           => 1,
                    CURLOPT_HEADER         => false,
                    CURLOPT_POSTFIELDS     => http_build_query($data)
                );

                break;
        }

        /**
         * Set up headers if given
         */
        if ($headers) {
            $arguments[CURLOPT_HTTPHEADER] = $headers;
        }

        /**
         * Set timeout
         */
        $arguments[CURLOPT_CONNECTTIMEOUT_MS] = 2000;

        /**
         * Do the actual curl
         */
        $ch = curl_init();
        curl_setopt_array($ch, $arguments);
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $response = curl_exec($ch);
        curl_close($ch);

        return $response;
    }
}<commit_msg>Caching applied to curl class.<commit_after><?php

namespace Modularity\Helper;

class Curl
{
    /**
     * Curl request
     * @param  string $type        Request type
     * @param  string $url         Request url
     * @param  array $data         Request data
     * @param  string $contentType Content type
     * @param  array $headers      Request headers
     * @return string              The request response
     */

    private $cacheKey;

    public static function request($type, $url, $data = null, $contentType = 'json', $headers = null)
    {

        //Create cache key as a reference
        $this->cacheKey = $this->createCacheKey($type, $url, $data, $contentType, $headers);

        //Return cached data
        if($this->getCachedResponse() !== false) {
            return $this->getCachedResponse();
        }

        //Arguments are stored here
        $arguments = null;

        switch (strtoupper($type)) {
            /**
             * Method: GET
             */
            case 'GET':
                // Append $data as querystring to $url
                if (is_array($data)) {
                    $url .= '?' . http_build_query($data);
                }

                // Set curl options for GET
                $arguments = array(
                    CURLOPT_RETURNTRANSFER      => true,
                    CURLOPT_HEADER              => false,
                    CURLOPT_FOLLOWLOCATION      => true,
                    CURLOPT_SSL_VERIFYPEER      => false,
                    CURLOPT_SSL_VERIFYHOST      => false,
                    CURLOPT_URL                 => $url,
                    CURLOPT_CONNECTTIMEOUT_MS]  => 1500
                );

                break;

            /**
             * Method: POST
             */
            case 'POST':
                // Set curl options for POST
                $arguments = array(
                    CURLOPT_RETURNTRANSFER      => 1,
                    CURLOPT_URL                 => $url,
                    CURLOPT_POST                => 1,
                    CURLOPT_HEADER              => false,
                    CURLOPT_POSTFIELDS          => http_build_query($data),
                    CURLOPT_CONNECTTIMEOUT_MS]  => 3000
                );

                break;
        }

        /**
         * Set up headers if given
         */
        if ($headers) {
            $arguments[CURLOPT_HTTPHEADER] = $headers;
        }

        /**
         * Do the actual curl
         */
        $ch = curl_init();
        curl_setopt_array($ch, $arguments);
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $response = curl_exec($ch);
        curl_close($ch);

        /**
         * Cache response
         */
        $this->storeResponse($response);

        /**
         * Return the response
         */
        return $response;
    }

    public function createCacheKey($type, $url, $data = null, $contentType = 'json', $headers = null) {
        $this->cacheKey = "curl_cache".md5($type.$url.$data.$contentType.$headers);
        return $cacheKey;
    }

    public function getCachedResponse () {
        return get_transient($this->cacheKey);
    }

    public function storeResponse($reponse,$minutes = 15) {
        return set_transient($this->cacheKey,$response,60*$minutes);
    }
}
{'fraction_non_alphanumeric': 0.10069382672122398, 'fraction_numerical': 0.0037359900373599006, 'mean_word_length': 1.2221343873517787, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '893887', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1435, 'n_words': 334}