<commit_before><?php

namespace Rezzza\RestApiBehatExtension\Rest;

use Guzzle\Http\Exception\BadResponseException;
use Guzzle\Http\ClientInterface as HttpClient;

class RestApiBrowser
{
    /** @var HttpClient */
    private $httpClient;

    /** @var array|\Guzzle\Http\Message\RequestInterface */
    private $request;

    /** @var \Guzzle\Http\Message\Response|array */
    private $response;

    /** @var array */
    private $requestHeaders = array();

    /** @var ResponseStorage */
    private $responseStorage;

    public function __construct(HttpClient $httpClient)
    {
        $this->httpClient = $httpClient;
    }

    public function enableResponseStorage(ResponseStorage $responseStorage)
    {
        $this->responseStorage = $responseStorage;
    }

    public function getResponse()
    {
        return $this->response;
    }

    public function getRequest()
    {
        return $this->request;
    }

    public function getRequestHeaders()
    {
        return $this->requestHeaders;
    }

    /**
     * @param string $method
     * @param string $url
     * @param PyStringNode $body
     */
    public function sendRequest($method, $url, $body = null)
    {
        $this->createRequest($method, $url, $body);

        try {
            $this->response = $this->httpClient->send($this->request);
        } catch (BadResponseException $e) {
            $this->response = $e->getResponse();

            if (null === $this->response) {
                throw $e;
            }
        }

        if (null !== $this->responseStorage) {
            $this->responseStorage->writeRawContent($this->response->getBody(true));
        }
    }

    /**
     * @param string $name
     * @param string $value
     */
    public function addRequestHeader($name, $value)
    {
        if (isset($this->requestHeaders[$name])) {
            if (!is_array($this->requestHeaders[$name])) {
                $this->requestHeaders[$name] = array($this->requestHeaders[$name]);
            }
            $this->requestHeaders[$name][] = $value;
        } else {
            $this->requestHeaders[$name] = $value;
        }
    }

    /**
     * @param string $name
     * @param string $value
     */
    public function setRequestHeader($name, $value)
    {
        $this->removeRequestHeader($name);
        $this->addRequestHeader($name, $value);
    }

    private function createRequest($method, $url, $body = null)
    {
        $this->request = $this->httpClient->createRequest($method, $url, $this->requestHeaders, $body);
        // Reset headers used for the HTTP request
        $this->requestHeaders = array();
    }

    private function removeRequestHeader($headerName)
    {
        if (array_key_exists($headerName, $this->requestHeaders)) {
            unset($this->requestHeaders[$headerName]);
        }
    }
}<commit_msg>Allow to pass options to guzzle when sending request<commit_after><?php

namespace Rezzza\RestApiBehatExtension\Rest;

use Guzzle\Http\Exception\BadResponseException;
use Guzzle\Http\ClientInterface as HttpClient;

class RestApiBrowser
{
    /** @var HttpClient */
    private $httpClient;

    /** @var array|\Guzzle\Http\Message\RequestInterface */
    private $request;

    /** @var \Guzzle\Http\Message\Response|array */
    private $response;

    /** @var array */
    private $requestHeaders = array();

    /** @var ResponseStorage */
    private $responseStorage;

    public function __construct(HttpClient $httpClient)
    {
        $this->httpClient = $httpClient;
    }

    public function enableResponseStorage(ResponseStorage $responseStorage)
    {
        $this->responseStorage = $responseStorage;
    }

    public function getResponse()
    {
        return $this->response;
    }

    public function getRequest()
    {
        return $this->request;
    }

    public function getRequestHeaders()
    {
        return $this->requestHeaders;
    }

    /**
     * @param string $method
     * @param string $url
     * @param PyStringNode $body
     * @param array $options
     */
    public function sendRequest($method, $url, $body = null, array $options = array())
    {
        $this->createRequest($method, $url, $body, $options);

        try {
            $this->response = $this->httpClient->send($this->request);
        } catch (BadResponseException $e) {
            $this->response = $e->getResponse();

            if (null === $this->response) {
                throw $e;
            }
        }

        if (null !== $this->responseStorage) {
            $this->responseStorage->writeRawContent($this->response->getBody(true));
        }
    }

    /**
     * @param string $name
     * @param string $value
     */
    public function addRequestHeader($name, $value)
    {
        if (isset($this->requestHeaders[$name])) {
            if (!is_array($this->requestHeaders[$name])) {
                $this->requestHeaders[$name] = array($this->requestHeaders[$name]);
            }
            $this->requestHeaders[$name][] = $value;
        } else {
            $this->requestHeaders[$name] = $value;
        }
    }

    /**
     * @param string $name
     * @param string $value
     */
    public function setRequestHeader($name, $value)
    {
        $this->removeRequestHeader($name);
        $this->addRequestHeader($name, $value);
    }

    private function createRequest($method, $url, $body = null, array $options = array())
    {
        $this->request = $this->httpClient->createRequest($method, $url, $this->requestHeaders, $body, $options);
        // Reset headers used for the HTTP request
        $this->requestHeaders = array();
    }

    private function removeRequestHeader($headerName)
    {
        if (array_key_exists($headerName, $this->requestHeaders)) {
            unset($this->requestHeaders[$headerName]);
        }
    }
}
{'fraction_non_alphanumeric': 0.13965811965811967, 'fraction_numerical': 0.0, 'mean_word_length': 2.3549311926605503, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, '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': '3487715', 'n_tokens_mistral': 1649, 'n_tokens_neox': 1604, 'n_words': 329}