<commit_before><?php
namespace ZendServerAPI;

use Guzzle\Service\Client;

class Request 
{
    /**
     * Method class for the request
     * @var \ZendServerAPI\Method
     */
	private $action = null;
	/**
	 * Useragent for the request
	 * @var string
	 */
	private $userAgent = 'HTTPFUL';
	/**
	 * Config for the connection
	 * @var \ZendServerAPI\Config
	 */
	private $config = null;
	
	/**
	 * Constructor for the request class
	 */
	public function __construct()
	{

	}
	
	/**
	 * Set method implementation object
	 * 
	 * @param \ZendServerAPI\Method $action
	 * @return \ZendServerAPI\Request
	 */
	public function setAction(\ZendServerAPI\Method $action)
	{
		$this->action = $action;
		return $this;
	}
	
	/**
	 * Set the user agent used by the request
	 * 
	 * @param string $userAgent
	 */
	public function setUserAgent($userAgent)
	{
	    $this->userAgent = $userAgent;
	}
	
	/**
	 * Get the used config object
	 * 
	 * @param \ZendServerAPI\Config $config
	 */
	public function setConfig(\ZendServerAPI\Config $config)
	{
		$this->config = $config;
	}
	
	/**
	 * Return the current action object
	 * 
	 * @return \ZendServerAPI\Method
	 */
	public function getAction()
	{
		return $this->action;
	}
	
	/**
	 * Returns the user agent 
	 * 
	 * @return string
	 */
	public function getUserAgent()
	{
	    return $this->userAgent;
	}
	
	/**
	 * Returns the currently used config
	 * 
	 * @return \ZendServerAPI\Config
	 */
	public function getConfig()
	{
		return $this->config;
	}
	
	/**
	 * This method performs the real REST call
	 * 
	 * @param \Guzzle\Service\Client $client
	 * @throws \ZendServerAPI\Exception\ClientSide
	 * @throws \ZendServerAPI\Exception\ServerSide
	 * @throws \Exception
	 */
	public function send($client = null)
	{
	    $link = 'http://' . $this->config->getHost() . ':' . $this->config->getPort() . $this->action->getLink();

	    $client = new Client('http://{host}:{port}', array('host' => $this->config->getHost(), 'port' => $this->config->getPort()));

		if($this->action->getMethod() === 'GET')
		{
		    $requests = $client->get(
                $this->action->getLink(),           
	            array(
	                    'X-Zend-Signature' => $this->config->getApiKey()->getName().';'.$this->generateRequestSignature($this->getDate()),
	                    'Accept' => 'application/vnd.zend.serverapi+xml;version=1.0',
	                    'lookInCupboard' => 'true',
	                    'Date' => $this->getDate(),
	                    'User-Agent' => $this->userAgent
	            )
            );
		}
		elseif($this->action->getMethod() === 'POST')
		{
		    $content = $this->action->getContent();
		    $requests = $client->post(
		            $this->action->getLink(),
		            array(
		                    'X-Zend-Signature' => $this->config->getApiKey()->getName().';'.$this->generateRequestSignature($this->getDate()),
		                    'Accept' => 'application/vnd.zend.serverapi+xml;version=1.0',
		                    'lookInCupboard' => 'true',
		                    'Date' => $this->getDate(),
		                    'User-Agent' => $this->userAgent,
		                    'Content-length' => strlen($content),
		                    'Content-type' => 'application/x-www-form-urlencoded'
		            ),
		            $content
		    );
		}
		
        try {
    		$response = $client->send($requests);
        } catch(\Guzzle\Http\Exception\BadResponseException $exception) {
            
            if($response->getStatusCode() >= 400 && $response->getStatusCode() <= 499)
                throw new Exception\ClientSide($exception->getMessage());
            elseif($response->code >= 500 && $response->code <= 599)
                throw new Exception\ServerSide($exception->getMessage());
            else
                throw new \Exception($response->getBody());
            
        }

        return $this->getAction()->parseResponse($response->getBody());
	}
	
	/**
	 * Get a special formatted date string, needed by the server api
	 * 
	 * @return string
	 */
	private function getDate()
	{
		$date = gmdate('D, d M Y H:i:s e');
		$date = str_replace('UTC', 'GMT', $date);
		
		return $date;
	}
	
	/**
	 * Calculate the request signature used for authentification
	 * 
	 * @param string $date
	 * @return string<commit_msg>Fixed exception handling with guzzle<commit_after><?php
namespace ZendServerAPI;

use Guzzle\Service\Client;

class Request 
{
    /**
     * Method class for the request
     * @var \ZendServerAPI\Method
     */
	private $action = null;
	/**
	 * Useragent for the request
	 * @var string
	 */
	private $userAgent = 'HTTPFUL';
	/**
	 * Config for the connection
	 * @var \ZendServerAPI\Config
	 */
	private $config = null;
	
	/**
	 * Constructor for the request class
	 */
	public function __construct()
	{

	}
	
	/**
	 * Set method implementation object
	 * 
	 * @param \ZendServerAPI\Method $action
	 * @return \ZendServerAPI\Request
	 */
	public function setAction(\ZendServerAPI\Method $action)
	{
		$this->action = $action;
		return $this;
	}
	
	/**
	 * Set the user agent used by the request
	 * 
	 * @param string $userAgent
	 */
	public function setUserAgent($userAgent)
	{
	    $this->userAgent = $userAgent;
	}
	
	/**
	 * Get the used config object
	 * 
	 * @param \ZendServerAPI\Config $config
	 */
	public function setConfig(\ZendServerAPI\Config $config)
	{
		$this->config = $config;
	}
	
	/**
	 * Return the current action object
	 * 
	 * @return \ZendServerAPI\Method
	 */
	public function getAction()
	{
		return $this->action;
	}
	
	/**
	 * Returns the user agent 
	 * 
	 * @return string
	 */
	public function getUserAgent()
	{
	    return $this->userAgent;
	}
	
	/**
	 * Returns the currently used config
	 * 
	 * @return \ZendServerAPI\Config
	 */
	public function getConfig()
	{
		return $this->config;
	}
	
	/**
	 * This method performs the real REST call
	 * 
	 * @param \Guzzle\Service\Client $client
	 * @throws \ZendServerAPI\Exception\ClientSide
	 * @throws \ZendServerAPI\Exception\ServerSide
	 * @throws \Exception
	 */
	public function send($client = null)
	{
	    $link = 'http://' . $this->config->getHost() . ':' . $this->config->getPort() . $this->action->getLink();

	    $client = new Client('http://{host}:{port}', array('host' => $this->config->getHost(), 'port' => $this->config->getPort()));

		if($this->action->getMethod() === 'GET')
		{
		    $requests = $client->get(
                $this->action->getLink(),           
	            array(
	                    'X-Zend-Signature' => $this->config->getApiKey()->getName().';'.$this->generateRequestSignature($this->getDate()),
	                    'Accept' => 'application/vnd.zend.serverapi+xml;version=1.0',
	                    'lookInCupboard' => 'true',
	                    'Date' => $this->getDate(),
	                    'User-Agent' => $this->userAgent
	            )
            );
		}
		elseif($this->action->getMethod() === 'POST')
		{
		    $content = $this->action->getContent();
		    $requests = $client->post(
		            $this->action->getLink(),
		            array(
		                    'X-Zend-Signature' => $this->config->getApiKey()->getName().';'.$this->generateRequestSignature($this->getDate()),
		                    'Accept' => 'application/vnd.zend.serverapi+xml;version=1.0',
		                    'lookInCupboard' => 'true',
		                    'Date' => $this->getDate(),
		                    'User-Agent' => $this->userAgent,
		                    'Content-length' => strlen($content),
		                    'Content-type' => 'application/x-www-form-urlencoded'
		            ),
		            $content
		    );
		}
		
        try {
    		$response = $client->send($requests);
        } catch(\Guzzle\Http\Exception\BadResponseException $exception) {
            
            if($exception->getCode() >= 400 && $exception->getCode() <= 499)
                throw new Exception\ClientSide($exception->getMessage());
            elseif($exception->getCode() >= 500 && $exception->getCode() <= 599)
                throw new Exception\ServerSide($exception->getMessage());
            else
                throw new \Exception($exception->getMessage());
            
        }
        
        return $this->getAction()->parseResponse($response->getBody());
	}
	
	/**
	 * Get a special formatted date string, needed by the server api
	 * 
	 * @return string
	 */
	private function getDate()
	{
		$date = gmdate('D, d M Y H:i:s e');
		$date = str_replace('UTC', 'GMT', $date);
		
		return $date;
	}
	
	/**
	 * Calculate the request signature used for authentification
	 * 
	 * @param string $date
	 * @return string
{'fraction_non_alphanumeric': 0.15928794359033638, 'fraction_numerical': 0.003698994335914923, 'mean_word_length': 2.2008879023307437, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 141, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '1438592', 'n_tokens_mistral': 2835, 'n_tokens_neox': 2497, 'n_words': 527}