<commit_before><?php
class Crowdmap
{
    const API_SCHEME = 'https';
    const API_HOST = 'api.crowdmap.com';
    const API_PATH = '/v1';

    protected $_publicKey;

    protected $_privateKey;

    protected $_session;

    public function __construct($publicKey, $privateKey)
    {
        $this->_publicKey = $publicKey;
        $this->_privateKey = $privateKey;
    }

    protected function _generateSignature($method, $resource)
    {
        $date = time();
        return 'A' . $this->_publicKey . hash_hmac('sha1', "{$method}\n{$date}\n{$resource}\n", $this->_privateKey);
    }

    protected function _request($method, $resource, $data = array())
    {
        $url = self::API_SCHEME . '://' . self::API_HOST . self::API_PATH . $resource;
        $userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 Crowdmap Client';
        $headers = array('User-Agent: ' . $userAgent);

        switch ($method)
        {
            case 'GET':
            case 'DELETE':
                $options = array(
                    'method'  => $method,
                    'header'  => implode("\r\n", $headers),
                    'ignore_errors' => true,
                );
                $url .= '?' . http_build_query($data);
            break;

            case 'POST':
            case 'PUT':
                $headers[] = 'Content-type: application/x-www-form-urlencoded';
                $options = array(
                    'method'  => $method,
                    'header'  => implode("\r\n", $headers),
                    'ignore_errors' => true,
                    'content' => http_build_query($data),
                );
            break;

            default:
                throw new Exception(sprintf('Invalid method: "%s"', $method));
            break;
        }

        
        $context = stream_context_create(array('http' => $options));
        $result = @file_get_contents($url, false, $context);

        if (!$result) {
            throw new Exception(sprintf('Request Error: ', $http_response_header[0]));
        }

        $data = @json_decode($result);

        if (!$data) {
            throw new Exception(sprintf('API Error: "%s"', $result));
        }

        return $data;
    }
<commit_msg>Fix error handling in case of empty body (return HTTP response code in exception).<commit_after><?php
class Crowdmap
{
    const API_SCHEME = 'https';
    const API_HOST = 'api.crowdmap.com';
    const API_PATH = '/v1';

    protected $_publicKey;

    protected $_privateKey;

    protected $_session;

    public function __construct($publicKey, $privateKey)
    {
        $this->_publicKey = $publicKey;
        $this->_privateKey = $privateKey;
    }

    protected function _generateSignature($method, $resource)
    {
        $date = time();
        return 'A' . $this->_publicKey . hash_hmac('sha1', "{$method}\n{$date}\n{$resource}\n", $this->_privateKey);
    }

    protected function _request($method, $resource, $data = array())
    {
        $url = self::API_SCHEME . '://' . self::API_HOST . self::API_PATH . $resource;
        $userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 Crowdmap Client';
        $headers = array('User-Agent: ' . $userAgent);

        switch ($method)
        {
            case 'GET':
            case 'DELETE':
                $options = array(
                    'method'  => $method,
                    'header'  => implode("\r\n", $headers),
                    'ignore_errors' => true,
                );
                $url .= '?' . http_build_query($data);
            break;

            case 'POST':
            case 'PUT':
                $headers[] = 'Content-type: application/x-www-form-urlencoded';
                $options = array(
                    'method'  => $method,
                    'header'  => implode("\r\n", $headers),
                    'ignore_errors' => true,
                    'content' => http_build_query($data),
                );
            break;

            default:
                throw new Exception(sprintf('Invalid method: "%s"', $method));
            break;
        }

        
        $context = stream_context_create(array('http' => $options));
        $result = @file_get_contents($url, false, $context);

        if (!$result) {
            throw new Exception(sprintf('Request Error: %s', $http_response_header[0]));
        }

        $data = @json_decode($result);

        if (!$data) {
            throw new Exception(sprintf('API Error: "%s"', $result));
        }

        return $data;
    }

{'fraction_non_alphanumeric': 0.15001083893344894, 'fraction_numerical': 0.0021677866897897246, 'mean_word_length': 1.791288566243194, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5853198', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1299, 'n_words': 272}