<commit_before><?php

class TrustedSearch_ApiRequestor{
  public $_apiPublicKey;
  public $_apiPrivateKey;
  protected $_responseBody;
  protected $_responseBodyRaw;
  protected $_responseCode;
  protected $_responseHeaders;
  protected $_authenticate = true;

  protected $_request;

  public function __construct($apiPublicKey=null, $apiPrivateKey=null){
    $this->_apiPublicKey = $apiPublicKey;
    $this->_apiPrivateKey = $apiPrivateKey;
  }

  public function request($meth, $url, $params=array(), $body=''){
    // echo "\nPARAMS: ".json_encode($params);
    // echo "\nRESOURCE: ".$url;
    // echo "\nBODY: ".json_encode($body);
    list($rbody, $rcode, $rhead) = $this->_requestRaw($meth, $url, $params, (is_array($body)?json_encode($body):''));
    //    echo "\nRespone Code: ".($rcode);
    $response = $this->_interpretResponse($rbody, $rcode, $rhead);
    return $response;
  }

  
  private function _requestRaw($meth, $url, $params = array(), $body = ''){
    $apiPublicKey = $this->_apiPublicKey;
    $apiPrivateKey = $this->_apiPrivateKey;
    //If authenticate is enabled. Then help out the developer by letting them know if they forgot to set api keys
    if($this->_authenticate){
      if (!$apiPublicKey){
        $apiPublicKey = TrustedSearch::$apiPublicKey;
      }
        
      if (!$apiPublicKey){
        throw new TrustedSearch_AuthenticationError('No API Public key provided.  (HINT: set your API key using "TrustedSearch::setApiPublicKey(<API-KEY>)".  You can generate API keys from the TrustedSearch web interface.  See https://trustedsearch.org/api for details, or email <EMAIL> if you have any questions.');
      }
        
      if (!$apiPrivateKey){
        $apiPrivateKey = TrustedSearch::$apiPrivateKey;  
      }
        
      if (!$apiPrivateKey){
        throw new TrustedSearch_AuthenticationError('No API Private key provided.  (HINT: set your API key using "TrustedSearch::setApiPrivateKey(<API-KEY>)".  You can generate API keys from the TrustedSearch web interface.  See https://trustedsearch.org/api for details, or email <EMAIL> if you have any questions.');
      }  
    }
    

    if (!TrustedSearch::getApiVersion()){
      throw new TrustedSearch_AuthenticationError('No API Version specified.  (HINT: set your API key using "TrustedSearch::setApiVersion(<API-KEY>)".  You can generate API keys from the TrustedSearch web interface.  See https://trustedsearch.org/api for details, or email <EMAIL> if you have any questions.');
    }

    $absUrl = $this->apiUrl($url);
    $params = self::_encodeObjects($params);
    
    $langVersion = phpversion();
    $uname = php_uname();
    $ua = array(
      'bindings_version' => TrustedSearch::VERSION,
		  'lang' => 'php',
		  'lang_version' => $langVersion,
		  'publisher' => 'trustedsearch',
		  'uname' => $uname
    );
    
    $headers = array(
      'X-TrustedSearch-Client-User-Agent: ' . json_encode($ua),
		  'User-Agent: TrustedSearch/v1 PhpBindings/' . TrustedSearch::VERSION,
      'Content-Type:   application/json'
    );
    
    if (TrustedSearch::$apiVersion){
      $headers[] = 'TrustedSearch-Version: ' . TrustedSearch::$apiVersion;
    }
    
    if($this->_authenticate){
      //Handle Authentication/Signature
      switch (TrustedSearch::getApiVersion()) {
        case '1':

          $timestamp = time();
          $request = array(
            'resource' => $url,
            'content' => $body,
            'timestamp' => $timestamp,
            'privateKey' => TrustedSearch::$apiPrivateKey
          );

          $signature = $this->_generateV1Signature($request);
          $params = array_merge($params, array(
            'signature'=>$signature, 
            'apikey'=>TrustedSearch::$apiPublicKey,
            'timestamp' => $timestamp<commit_msg>V2 Api uses v1 auth<commit_after><?php

class TrustedSearch_ApiRequestor{
  public $_apiPublicKey;
  public $_apiPrivateKey;
  protected $_responseBody;
  protected $_responseBodyRaw;
  protected $_responseCode;
  protected $_responseHeaders;
  protected $_authenticate = true;

  protected $_request;

  public function __construct($apiPublicKey=null, $apiPrivateKey=null){
    $this->_apiPublicKey = $apiPublicKey;
    $this->_apiPrivateKey = $apiPrivateKey;
  }

  public function request($meth, $url, $params=array(), $body=''){
    // echo "\nPARAMS: ".json_encode($params);
    // echo "\nRESOURCE: ".$url;
    // echo "\nBODY: ".json_encode($body);
    list($rbody, $rcode, $rhead) = $this->_requestRaw($meth, $url, $params, (is_array($body)?json_encode($body):''));
    //    echo "\nRespone Code: ".($rcode);
    $response = $this->_interpretResponse($rbody, $rcode, $rhead);
    return $response;
  }

  
  private function _requestRaw($meth, $url, $params = array(), $body = ''){
    $apiPublicKey = $this->_apiPublicKey;
    $apiPrivateKey = $this->_apiPrivateKey;
    //If authenticate is enabled. Then help out the developer by letting them know if they forgot to set api keys
    if($this->_authenticate){
      if (!$apiPublicKey){
        $apiPublicKey = TrustedSearch::$apiPublicKey;
      }
        
      if (!$apiPublicKey){
        throw new TrustedSearch_AuthenticationError('No API Public key provided.  (HINT: set your API key using "TrustedSearch::setApiPublicKey(<API-KEY>)".  You can generate API keys from the TrustedSearch web interface.  See https://trustedsearch.org/api for details, or email <EMAIL> if you have any questions.');
      }
        
      if (!$apiPrivateKey){
        $apiPrivateKey = TrustedSearch::$apiPrivateKey;  
      }
        
      if (!$apiPrivateKey){
        throw new TrustedSearch_AuthenticationError('No API Private key provided.  (HINT: set your API key using "TrustedSearch::setApiPrivateKey(<API-KEY>)".  You can generate API keys from the TrustedSearch web interface.  See https://trustedsearch.org/api for details, or email <EMAIL> if you have any questions.');
      }  
    }
    

    if (!TrustedSearch::getApiVersion()){
      throw new TrustedSearch_AuthenticationError('No API Version specified.  (HINT: set your API key using "TrustedSearch::setApiVersion(<API-KEY>)".  You can generate API keys from the TrustedSearch web interface.  See https://trustedsearch.org/api for details, or email <EMAIL> if you have any questions.');
    }

    $absUrl = $this->apiUrl($url);
    $params = self::_encodeObjects($params);
    
    $langVersion = phpversion();
    $uname = php_uname();
    $ua = array(
      'bindings_version' => TrustedSearch::VERSION,
		  'lang' => 'php',
		  'lang_version' => $langVersion,
		  'publisher' => 'trustedsearch',
		  'uname' => $uname
    );
    
    $headers = array(
      'X-TrustedSearch-Client-User-Agent: ' . json_encode($ua),
		  'User-Agent: TrustedSearch/v1 PhpBindings/' . TrustedSearch::VERSION,
      'Content-Type:   application/json'
    );
    
    if (TrustedSearch::$apiVersion){
      $headers[] = 'TrustedSearch-Version: ' . TrustedSearch::$apiVersion;
    }
    
    if($this->_authenticate){
      //Handle Authentication/Signature
      switch (TrustedSearch::getApiVersion()) {
        case '1':
        case '2':

          $timestamp = time();
          $request = array(
            'resource' => $url,
            'content' => $body,
            'timestamp' => $timestamp,
            'privateKey' => TrustedSearch::$apiPrivateKey
          );

          $signature = $this->_generateV1Signature($request);
          $params = array_merge($params, array(
            'signature'=>$signature, 
            'apikey'=>TrustedSearch::$apiPublicKey,
            'timestamp' => $timestamp
{'fraction_non_alphanumeric': 0.13742766964755393, 'fraction_numerical': 0.0011835875854813256, 'mean_word_length': 3.176276771004942, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 59, 'https://': 6, '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': '6868343', 'n_tokens_mistral': 2285, 'n_tokens_neox': 2255, 'n_words': 557}