<commit_before><?php

namespace Phpantom\Client;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\TransferException;
use Phly\Http\Response as HttpResponse;
use Psr\Http\Message\RequestInterface;

/**
 * Class Guzzle
 * @package Phantom\Client
 */
class Guzzle implements ClientInterface
{

    /**
     * @var Client
     */
    private $client;

    /**
     * @var Proxy
     */
    private $proxy;

    /**
     * Constructor
     */
    public function __construct()
    {
        if (!isset ($this->client)) {
            $this->client = new Client([
                    'defaults' => [
                        'timeout' => 10,
                        'allow_redirects' => [
                            'max' => 5,
                            'strict' => false,
                            'referer' => true,
                            'protocols' => ['http', 'https']
                        ],
                        //'proxy'   => 'tcp://localhost:8888',
                        'cookies' => true,
                        'stream' => false,
                        'future' => false
                    ]
                ]
            );
        }
    }

    public function setProxy(Proxy $proxy)
    {
        $this->proxy = $proxy;
        return $this;
    }

    /**
     * @return Proxy|null
     */
    public function getProxy()
    {
        return $this->proxy;
    }

    /**
     * @return mixed|null
     */
    public function nextProxy()
    {
        $proxy = $this->getProxy();
        return $proxy? $proxy->nextProxy() : null;
    }


    /**
     * @param RequestInterface $request
     * @return mixed|HttpResponse
     */
    public function load(RequestInterface $request)
    {
        $headers = [];
        foreach ($request->getHeaders() as $key => $val) {
            $headers[$key] = implode(", ", $val);
        }
        $request = $this->client->createRequest(
            $request->getMethod()?: 'GET',
            $request->getUri(),
            [
                'headers' => $headers,
                'proxy' => $this->nextProxy()
            ]
        );
        try {
            $guzzleResponse = $this->client->send($request);
        } catch (TransferException $e) {
            $guzzleResponse = $e->getResponse();
        }
        $httpResponse = new HttpResponse('php://memory', $guzzleResponse->getStatusCode(), $guzzleResponse->getHeaders());
        $httpResponse->getBody()
            ->write($guzzleResponse->getBody()->getContents());
        return $httpResponse;
    }
}<commit_msg>guzlle exception handler<commit_after><?php

namespace Phpantom\Client;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\TransferException;
use Phly\Http\Response as HttpResponse;
use Psr\Http\Message\RequestInterface;

/**
 * Class Guzzle
 * @package Phantom\Client
 */
class Guzzle implements ClientInterface
{

    /**
     * @var Client
     */
    private $client;

    /**
     * @var Proxy
     */
    private $proxy;

    /**
     * Constructor
     */
    public function __construct()
    {
        if (!isset ($this->client)) {
            $this->client = new Client([
                    'defaults' => [
                        'timeout' => 10,
                        'allow_redirects' => [
                            'max' => 5,
                            'strict' => false,
                            'referer' => true,
                            'protocols' => ['http', 'https']
                        ],
                        //'proxy'   => 'tcp://localhost:8888',
                        'cookies' => true,
                        'stream' => false,
                        'future' => false
                    ]
                ]
            );
        }
    }

    public function setProxy(Proxy $proxy)
    {
        $this->proxy = $proxy;
        return $this;
    }

    /**
     * @return Proxy|null
     */
    public function getProxy()
    {
        return $this->proxy;
    }

    /**
     * @return mixed|null
     */
    public function nextProxy()
    {
        $proxy = $this->getProxy();
        return $proxy? $proxy->nextProxy() : null;
    }


    /**
     * @param RequestInterface $request
     * @return mixed|HttpResponse
     */
    public function load(RequestInterface $request)
    {
        $headers = [];
        foreach ($request->getHeaders() as $key => $val) {
            $headers[$key] = implode(", ", $val);
        }
        $request = $this->client->createRequest(
            $request->getMethod()?: 'GET',
            $request->getUri(),
            [
                'headers' => $headers,
                'proxy' => $this->nextProxy()
            ]
        );
        try {
            $guzzleResponse = $this->client->send($request);
            $code = $guzzleResponse->getStatusCode();
            $headers = $guzzleResponse->getHeaders();
            $contents = $guzzleResponse->getBody()->getContents();
        } catch (TransferException $e) {
            if (is_callable([$e, 'getResponse']) && ($guzzleResponse = $e->getResponse())) {
                $code = $guzzleResponse->getStatusCode();
                $headers = $guzzleResponse->getHeaders();
                $contents = $guzzleResponse->getBody()->getContents();
            } else {
                $code = '500';
                $headers = [];
                $contents = '';
            }
        }

        $httpResponse = new HttpResponse('php://memory', $code, $headers);
        $httpResponse->getBody()
            ->write($contents);
        return $httpResponse;
    }
}
{'fraction_non_alphanumeric': 0.14421866570760655, 'fraction_numerical': 0.003057004135946772, 'mean_word_length': 1.3912295786758384, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 77, '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': '4487999', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1470, 'n_words': 282}