<commit_before><?php
/**
 * @author <NAME> <<EMAIL>>
 * @license MIT
 */
namespace Jeancsil\FlightSpy\Api\Http;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\BadResponseException;
use Jeancsil\FlightSpy\Api\DataTransfer\SessionParameters;

class Transport {
    const LIVE_PRICING = '/apiservices/pricing/v1.0';
//    const LIVE_PRICING = '/xep6c1xe';

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

    /**
     * @var string
     */
    private $pollUrl;

    public function __construct(ClientInterface $client) {
        $this->client = $client;
    }

    public function findQuotes(SessionParameters $parameters) {
        $this->createSession($parameters);
        return $this->poll();
    }

    private function createSession(SessionParameters $parameters) {
        try {
            $parametersArray = $parameters->toArray();

            $request = $this
                ->client
                ->post(static::LIVE_PRICING, ['form_params' => $parametersArray]);

            if (!isset($request->getHeaders()['Location'][0])) {
                throw new \Exception('Location not found for the poll');
            }

            $this->setPollUrl($request->getHeaders()['Location'][0], $parametersArray['apiKey']);
            sleep(1);
        } catch (BadResponseException $e) {
            //TODO
            echo $e->getMessage();die;
        }
    }

    private function poll() {
        try {
            $request = $this
                ->client
                ->get($this->pollUrl);

            //TODO FIXME
            return \GuzzleHttp\json_decode($request->getBody()->getContents());
        } catch (BadResponseException $e) {
            //print_r($e->getResponse());
        }
    }

    /**
     * @param $url
     * @param $apiKey
     */
    private function setPollUrl($url, $apiKey) {
        $this->pollUrl = "$url?apiKey=$apiKey&sorttype=price&sortorder=desc";
    }
}<commit_msg>Add echo messages to help during development time.<commit_after><?php
/**
 * @author <NAME> <<EMAIL>>
 * @license MIT
 */
namespace Jeancsil\FlightSpy\Api\Http;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\BadResponseException;
use Jeancsil\FlightSpy\Api\DataTransfer\SessionParameters;

class Transport {
    const LIVE_PRICING = '/apiservices/pricing/v1.0';

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

    /**
     * @var string
     */
    private $pollUrl;

    /**
     * @param ClientInterface $client
     */
    public function __construct(ClientInterface $client) {
        $this->client = $client;
    }

    /**
     * @param SessionParameters $parameters
     * @return array
     */
    public function findQuotes(SessionParameters $parameters) {
        $this->createSession($parameters);

        return $this->poll();
    }

    /**
     * @param SessionParameters $parameters
     * @throws \Exception
     */
    private function createSession(SessionParameters $parameters) {
        try {
            $parametersArray = $parameters->toArray();

            $request = $this
                ->client
                ->post(static::LIVE_PRICING, ['form_params' => $parametersArray]);

            if (!isset($request->getHeaders()['Location'][0])) {
                throw new \Exception('Location not found for the poll');
            }

            $this->setPollUrl($request->getHeaders()['Location'][0], $parametersArray['apiKey']);
            sleep(1);
        } catch (BadResponseException $e) {
            echo "createSession::BadResponseException: " . $e->getMessage();
            die;
        }
    }

    /**
     * @return array
     */
    private function poll() {
        try {
            $request = $this
                ->client
                ->get($this->pollUrl);

            //TODO FIXME
            return \GuzzleHttp\json_decode($request->getBody()->getContents());
        } catch (BadResponseException $e) {
            echo "pool::BadResponseException:";
            print_r($e->getResponse());
            die;
        }
    }

    /**
     * @param $url
     * @param $apiKey
     */
    private function setPollUrl($url, $apiKey) {
        $this->pollUrl = "$url?apiKey=$apiKey&sorttype=price&sortorder=desc";
    }
}
{'fraction_non_alphanumeric': 0.14158253111058933, 'fraction_numerical': 0.002817562808170932, 'mean_word_length': 2.1094890510948905, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 45, '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': '2981927', 'n_tokens_mistral': 1231, 'n_tokens_neox': 1200, 'n_words': 225}