<commit_before><?php

/**
 *  Weather Source API PHP SDK
 *
 *  Requires PHP version 5.3.0 or greater
 *
 *  @author     <NAME>
 *  @copyright  2012– Weather Source, LLC
 *  @version    2.4
 *  @todo       Add logic to ramp up large jobs to allow load balancers to scale
 */



/**
 *  Class to manage requests to and responses from the Weather Source API
 */
class Weather_Source_API_Requests {


    /**
     *  @access  private
     *  @static
     *  @var     string  The base API URI.
     */
    static private $base_uri;


    /**
     *  @access  private
     *  @static
     *  @var     array  The API version.
     */
    static private $version;


    /**
     *  @access  private
     *  @static
     *  @var     array  The API key
     */
    static private $key;


    /**
     *  @access  private
     *  @static
     *  @var     boolean  Return diagnostic information with response?
     */
    static private $return_diagnostics;


    /**
     *  @access  private
     *  @static
     *  @var     boolean  Suppress all HTTP response codes (i.e. force a 200 response)?
     */
    static private $suppress_response_codes;


    /**
     *  @access  private
     *  @static
     *  @var     string  Defines the unit type to return for relevant observations. Allowed
     *                   values are 'imperial' and 'metric'.
     */
    static private $distance_unit;


    /**
     *  @access  private
     *  @static
     *  @var     string  Defines the unit type to return for relevant observations. Allowed
     *                   values are 'fahrenheit' and 'celsius'.
     */
    static private $temperature_unit;


    /**
     *  @access  private
     *  @static
     *  @var     boolean  Should all errors be written to a log file?
     */
    static private $log_errors;


    /**
     *  @access  private
     *  @static
     *  @var     string  Log file directory location.
     */
    static private $error_log_directory;


    /**
     *  @access  private
     *  @static
     *  @var     array  All API field names that return results as inches.
     */
    static private $inch_fields = array( 'precip', 'precipMax', 'precipAvg', 'precipMin',
                                         'snowfall', 'snowfallMax', 'snowfallAvg', 'snowfallMin' );


    /**
     *  @access  private
     *  @static
     *  @var     array  All API field names that return results as miles per hour.
     */
    static private $mph_fields = array( 'windSpd', 'windSpdMax', 'windSpdAvg', 'windSpdMin',
                                        'prevailWindSpd' );


    /**
     *  @access  private
     *  @static
     *  @var     array  All API field names that return results as degrees fahrenheit.
     */
    static private $fahrenheit_fields = array( 'temp', 'tempMax', 'tempAvg', 'tempMin', 'dewPt',
                                               'dewPtMax', 'dewPtAvg', 'dewPtMin', 'feelsLike',
                                               'feelsLikeMax', 'feelsLikeAvg', 'feelsLikeMin',
                                               'wetBulb', 'wetBulbMax', 'wetBulbAvg', 'wetBulbMin' );


    /**
     *  @access  private
     *  @static
     *  @var     array  A key/value inverse of self::$inch_fields for fast lookups.
     */
    static private $inch_keys;


    /**
     *  @access  private
     *  @static
     *  @var     array  A key/value inverse of self::$mph_fields for fast lookups.
     */
    static private $mph_keys;


    /**
     *  @access  private
     *  @static
     *  @var     array  A key/value inverse of self::$fahrenheit_fields for fast lookups.
     */
    static private $fahrenheit_keys;


    /**
     *  @access  private
     *  @static
     *  @var     array  The unique identifier for this instances Curl_Node.
     */
    private $curl_node;


    /**
     *  Initiate a class instance and add Weather Source API request to multithreaded
     *  cURL handler
     *
     *  @access  public
     *  @param   string    $method         [REQUIRED]  The HTTP method for the request
     *                                                 (allowed: 'GET', 'POST', 'PUT',
     *                                                 'DELETE')
     *  @param   string    $resource_path  [REQUIRED]  The resource path for the request (i.e.
     *                                                 'history_by_postal_code')
     *  @param   array     $parameters     [REQUIRED]  The resource parameters
     *  @param   callable  $callback       [OPTIONAL]  If provided, the user defined callback
     *                                                 function called as this individual
     *                                                 request completes. You don't need to
     *                                                 wait for everything to finish!
     *  @return  NULL
    **/
    public function __construct( $method, $resource_path, $parameters, $callback = '' ) {

        if( empty(self::$base_uri) ) {

            require_once( __DIR__ . '/config.php' );

            self::$base_uri                = defined('WSAPI_BASE_URI') ? (string) WSAPI_BASE_URI : 'https://api.weathersource.com';
            self::$version                 = defined('WSAPI_VERSION') ? (string) WSAPI_VERSION : 'v1';
            self::$key                     = defined('WSAPI_KEY') ? (string) WSAPI_KEY : '';
            self::$return_diagnostics      = defined('WSAPI_RETURN_DIAGNOSTICS') ? (boolean) WSAPI_RETURN_DIAGNOSTICS : FALSE;
            self::$suppress_response_codes = defined('WSAPI_SUPPRESS_RESPONSE_CODES') ? (boolean) WSAPI_SUPPRESS_RESPONSE_CODES : FALSE;
            self::$distance_unit           = defined('WSSDK_DISTANCE_UNIT') ? (boolean) WSSDK_DISTANCE_UNIT : 'imperial';
            self::$temperature_unit        = defined('WSSDK_TEMPERATURE_UNIT') ? (boolean) WSSDK_TEMPERATURE_UNIT : 'fahrenheit';
            self::$log_errors              = defined('WSSDK_LOG_ERRORS') ? (boolean) WSSDK_LOG_ERRORS : FALSE;
            self::$error_log_directory     = defined('WSSDK_ERROR_LOG_DIRECTORY') ? (string) WSSDK_ERROR_LOG_DIRECTORY : 'error_logs/';
            self::$inch_keys               = array_flip(self::$inch_fields);
            self::$mph_keys                = array_flip(self::$mph_fields);
            self::$fahrenheit_keys         = array_flip(self::$fahrenheit_fields);
            $max_threads                   = defined('WSSDK_MAX_THREADS') ? (integer) WSSDK_MAX_THREADS : 10;
            $max_requests_per_minute       = defined('WSSDK_MAX_REQUESTS_PER_MINUTE') ? (integer) WSSDK_MAX_REQUESTS_PER_MINUTE : 10;
            $request_retry_count           = defined('WSSDK_REQUEST_RETRY_ON_ERROR_COUNT') ? (integer) WSSDK_REQUEST_RETRY_ON_ERROR_COUNT : 5;
            $request_retry_delay           = defined('WSSDK_REQUEST_RETRY_ON_ERROR_DELAY') ? (integer) WSSDK_REQUEST_RETRY_ON_ERROR_DELAY : 2;
            $scaling_initial_requests_per_minute = defined('WSSDK_SCALING_INITIAL_REQUESTS_PER_MINUTE') ? (integer) WSSDK_SCALING_INITIAL_REQUESTS_PER_MINUTE : 1000;
            $scaling_double_capacity_minutes     = defined('WSSDK_SCALING_DOUBLE_CAPACITY_MINUTES') ? (integer) WSSDK_REQUEST_RETRY_ON_ERROR_DELAY : 7;

            Curl_Node::set_max_requests_per_minute($max_requests_per_minute);
            Curl_Node::set_max_threads($max_threads);
            Curl_Node::set_max_retries($request_retry_count);
            Curl_Node::set_scaling_initial_requests_per_minute($scaling_initial_requests_per_minute);
            Curl_Node::set_scaling_double_capacity_minutes($scaling_double_capacity_minutes);
        }


        /*  assemble our request URL  */

        $url = self::$base_uri . '/' . self::$version . '/' . self::$key . '/' . $resource_path . '.json';


        /*  append meta parameters  */

        $parameters['_method'] = strtolower($method);
        if( self::$return_diagnostics ) {
            $parameters['_diagnostics'] = '1';<commit_msg>minor bug fix<commit_after><?php

/**
 *  Weather Source API PHP SDK
 *
 *  Requires PHP version 5.3.0 or greater
 *
 *  @author     <NAME>
 *  @copyright  2012– Weather Source, LLC
 *  @version    2.4
 *  @todo       Add logic to ramp up large jobs to allow load balancers to scale
 */



/**
 *  Class to manage requests to and responses from the Weather Source API
 */
class Weather_Source_API_Requests {


    /**
     *  @access  private
     *  @static
     *  @var     string  The base API URI.
     */
    static private $base_uri;


    /**
     *  @access  private
     *  @static
     *  @var     array  The API version.
     */
    static private $version;


    /**
     *  @access  private
     *  @static
     *  @var     array  The API key
     */
    static private $key;


    /**
     *  @access  private
     *  @static
     *  @var     boolean  Return diagnostic information with response?
     */
    static private $return_diagnostics;


    /**
     *  @access  private
     *  @static
     *  @var     boolean  Suppress all HTTP response codes (i.e. force a 200 response)?
     */
    static private $suppress_response_codes;


    /**
     *  @access  private
     *  @static
     *  @var     string  Defines the unit type to return for relevant observations. Allowed
     *                   values are 'imperial' and 'metric'.
     */
    static private $distance_unit;


    /**
     *  @access  private
     *  @static
     *  @var     string  Defines the unit type to return for relevant observations. Allowed
     *                   values are 'fahrenheit' and 'celsius'.
     */
    static private $temperature_unit;


    /**
     *  @access  private
     *  @static
     *  @var     boolean  Should all errors be written to a log file?
     */
    static private $log_errors;


    /**
     *  @access  private
     *  @static
     *  @var     string  Log file directory location.
     */
    static private $error_log_directory;


    /**
     *  @access  private
     *  @static
     *  @var     array  All API field names that return results as inches.
     */
    static private $inch_fields = array( 'precip', 'precipMax', 'precipAvg', 'precipMin',
                                         'snowfall', 'snowfallMax', 'snowfallAvg', 'snowfallMin' );


    /**
     *  @access  private
     *  @static
     *  @var     array  All API field names that return results as miles per hour.
     */
    static private $mph_fields = array( 'windSpd', 'windSpdMax', 'windSpdAvg', 'windSpdMin',
                                        'prevailWindSpd' );


    /**
     *  @access  private
     *  @static
     *  @var     array  All API field names that return results as degrees fahrenheit.
     */
    static private $fahrenheit_fields = array( 'temp', 'tempMax', 'tempAvg', 'tempMin', 'dewPt',
                                               'dewPtMax', 'dewPtAvg', 'dewPtMin', 'feelsLike',
                                               'feelsLikeMax', 'feelsLikeAvg', 'feelsLikeMin',
                                               'wetBulb', 'wetBulbMax', 'wetBulbAvg', 'wetBulbMin' );


    /**
     *  @access  private
     *  @static
     *  @var     array  A key/value inverse of self::$inch_fields for fast lookups.
     */
    static private $inch_keys;


    /**
     *  @access  private
     *  @static
     *  @var     array  A key/value inverse of self::$mph_fields for fast lookups.
     */
    static private $mph_keys;


    /**
     *  @access  private
     *  @static
     *  @var     array  A key/value inverse of self::$fahrenheit_fields for fast lookups.
     */
    static private $fahrenheit_keys;


    /**
     *  @access  private
     *  @static
     *  @var     array  The unique identifier for this instances Curl_Node.
     */
    private $curl_node;


    /**
     *  Initiate a class instance and add Weather Source API request to multithreaded
     *  cURL handler
     *
     *  @access  public
     *  @param   string    $method         [REQUIRED]  The HTTP method for the request
     *                                                 (allowed: 'GET', 'POST', 'PUT',
     *                                                 'DELETE')
     *  @param   string    $resource_path  [REQUIRED]  The resource path for the request (i.e.
     *                                                 'history_by_postal_code')
     *  @param   array     $parameters     [REQUIRED]  The resource parameters
     *  @param   callable  $callback       [OPTIONAL]  If provided, the user defined callback
     *                                                 function called as this individual
     *                                                 request completes. You don't need to
     *                                                 wait for everything to finish!
     *  @return  NULL
    **/
    public function __construct( $method, $resource_path, $parameters, $callback = '' ) {

        if( empty(self::$base_uri) ) {

            require_once( __DIR__ . '/config.php' );

            self::$base_uri                = defined('WSAPI_BASE_URI') ? (string) WSAPI_BASE_URI : 'https://api.weathersource.com';
            self::$version                 = defined('WSAPI_VERSION') ? (string) WSAPI_VERSION : 'v1';
            self::$key                     = defined('WSAPI_KEY') ? (string) WSAPI_KEY : '';
            self::$return_diagnostics      = defined('WSAPI_RETURN_DIAGNOSTICS') ? (boolean) WSAPI_RETURN_DIAGNOSTICS : FALSE;
            self::$suppress_response_codes = defined('WSAPI_SUPPRESS_RESPONSE_CODES') ? (boolean) WSAPI_SUPPRESS_RESPONSE_CODES : FALSE;
            self::$distance_unit           = defined('WSSDK_DISTANCE_UNIT') ? (boolean) WSSDK_DISTANCE_UNIT : 'imperial';
            self::$temperature_unit        = defined('WSSDK_TEMPERATURE_UNIT') ? (boolean) WSSDK_TEMPERATURE_UNIT : 'fahrenheit';
            self::$log_errors              = defined('WSSDK_LOG_ERRORS') ? (boolean) WSSDK_LOG_ERRORS : FALSE;
            self::$error_log_directory     = defined('WSSDK_ERROR_LOG_DIRECTORY') ? (string) WSSDK_ERROR_LOG_DIRECTORY : 'error_logs/';
            self::$inch_keys               = array_flip(self::$inch_fields);
            self::$mph_keys                = array_flip(self::$mph_fields);
            self::$fahrenheit_keys         = array_flip(self::$fahrenheit_fields);
            $max_threads                   = defined('WSSDK_MAX_THREADS') ? (integer) WSSDK_MAX_THREADS : 10;
            $max_requests_per_minute       = defined('WSSDK_MAX_REQUESTS_PER_MINUTE') ? (integer) WSSDK_MAX_REQUESTS_PER_MINUTE : 10;
            $request_retry_count           = defined('WSSDK_REQUEST_RETRY_ON_ERROR_COUNT') ? (integer) WSSDK_REQUEST_RETRY_ON_ERROR_COUNT : 5;
            $request_retry_delay           = defined('WSSDK_REQUEST_RETRY_ON_ERROR_DELAY') ? (integer) WSSDK_REQUEST_RETRY_ON_ERROR_DELAY : 2;
            $scaling_initial_requests_per_minute = defined('WSSDK_SCALING_INITIAL_REQUESTS_PER_MINUTE') ? (integer) WSSDK_SCALING_INITIAL_REQUESTS_PER_MINUTE : 1000;
            $scaling_double_capacity_minutes     = defined('WSSDK_SCALING_DOUBLE_CAPACITY_MINUTES') ? (integer) WSSDK_SCALING_DOUBLE_CAPACITY_MINUTES : 7;

            Curl_Node::set_max_requests_per_minute($max_requests_per_minute);
            Curl_Node::set_max_threads($max_threads);
            Curl_Node::set_max_retries($request_retry_count);
            Curl_Node::set_scaling_initial_requests_per_minute($scaling_initial_requests_per_minute);
            Curl_Node::set_scaling_double_capacity_minutes($scaling_double_capacity_minutes);
        }


        /*  assemble our request URL  */

        $url = self::$base_uri . '/' . self::$version . '/' . self::$key . '/' . $resource_path . '.json';


        /*  append meta parameters  */

        $parameters['_method'] = strtolower($method);
        if( self::$return_diagnostics ) {
            $parameters['_diagnostics'] = '1';
{'fraction_non_alphanumeric': 0.10058538427645244, 'fraction_numerical': 0.0031472272927550826, 'mean_word_length': 1.9254280979561775, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5697599', 'n_tokens_mistral': 4834, 'n_tokens_neox': 4653, 'n_words': 1063}