<commit_before><?php

namespace Vision\Request;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Vision\Feature;
use Vision\Image;
use Vision\Hydrator\AnnotateImageHydrator;
use Vision\Response\AnnotateImageResponse;

class VisionRequest
{
    const VISION_ANNOTATE_PREFIX = 'https://vision.googleapis.com/v1/images:annotate?key=';

    /**
     * @var string
     */
    protected $apiKey;

    /**
     * @var Feature[]
     */
    protected $features = [];

    /**
     * @var Image
     */
    protected $image;

    /**
     * @var string
     */
    protected $rawResponse;

    /**
     * @var ClientException
     */
    protected $clientException;

    /**
     * @param string $apiKey
     * @param Image $image
     * @param Feature[] $features
     */
    public function __construct($apiKey, Image $image, array $features)
    {
        $this->apiKey = $apiKey;
        $this->features = $features;
        $this->image = $image;
    }

    /**
     * @return array
     */
    protected function getPayload()
    {
        return [
            'requests' => [
                [
                    'image' => [
                        'content' => $this->image->getImage(),
                    ],
                    'features' => $this->getMappedFeatures(),
                ],
            ],
        ];
    }

    /**
     * @return array
     */
    protected function getMappedFeatures()
    {
        return array_map(
            function (Feature $feature) {
                return [
                    'type' => $feature->getFeature(),
                    'maxResults' => $feature->getMaxResults(),
                ];
            },
            $this->features
        );
    }

    public function send()
    {
        try {
            $client = new Client;
            $response = $client->post(
                self::VISION_ANNOTATE_PREFIX.$this->apiKey,
                [
                    'content-type' => 'application/json',
                    'body' => json_encode($this->getPayload()),
                ]
            );

            $this->rawResponse = $response->getBody()->getContents();
        } catch (ClientException $e) {
            $this->clientException = $e;
        }
    }

    /**
     * @return AnnotateImageResponse
     */
    public function getAnnotateImageResponse()
    {
        if (!$this->clientException) {
            return $this->getResponseFromException($this->clientException);
        }
        
        $content = json_decode($this->rawResponse, true);
        return $this->getResponseFromArray($content['responses'][0]);
    }

    /**
     * @return string
     */
    public function getRawResponse()
    {
        return $this->rawResponse;
    }

    /**
     * @param array $response
     * @return AnnotateImageResponse
     */<commit_msg>Bug fix. Invert statement<commit_after><?php

namespace Vision\Request;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Vision\Feature;
use Vision\Image;
use Vision\Hydrator\AnnotateImageHydrator;
use Vision\Response\AnnotateImageResponse;

class VisionRequest
{
    const VISION_ANNOTATE_PREFIX = 'https://vision.googleapis.com/v1/images:annotate?key=';

    /**
     * @var string
     */
    protected $apiKey;

    /**
     * @var Feature[]
     */
    protected $features = [];

    /**
     * @var Image
     */
    protected $image;

    /**
     * @var string
     */
    protected $rawResponse;

    /**
     * @var ClientException
     */
    protected $clientException;

    /**
     * @param string $apiKey
     * @param Image $image
     * @param Feature[] $features
     */
    public function __construct($apiKey, Image $image, array $features)
    {
        $this->apiKey = $apiKey;
        $this->features = $features;
        $this->image = $image;
    }

    /**
     * @return array
     */
    protected function getPayload()
    {
        return [
            'requests' => [
                [
                    'image' => [
                        'content' => $this->image->getImage(),
                    ],
                    'features' => $this->getMappedFeatures(),
                ],
            ],
        ];
    }

    /**
     * @return array
     */
    protected function getMappedFeatures()
    {
        return array_map(
            function (Feature $feature) {
                return [
                    'type' => $feature->getFeature(),
                    'maxResults' => $feature->getMaxResults(),
                ];
            },
            $this->features
        );
    }

    public function send()
    {
        try {
            $client = new Client;
            $response = $client->post(
                self::VISION_ANNOTATE_PREFIX.$this->apiKey,
                [
                    'content-type' => 'application/json',
                    'body' => json_encode($this->getPayload()),
                ]
            );

            $this->rawResponse = $response->getBody()->getContents();
        } catch (ClientException $e) {
            $this->clientException = $e;
        }
    }

    /**
     * @return AnnotateImageResponse
     */
    public function getAnnotateImageResponse()
    {
        if ($this->clientException) {
            return $this->getResponseFromException($this->clientException);
        }
        
        $content = json_decode($this->rawResponse, true);
        return $this->getResponseFromArray($content['responses'][0]);
    }

    /**
     * @return string
     */
    public function getRawResponse()
    {
        return $this->rawResponse;
    }

    /**
     * @param array $response
     * @return AnnotateImageResponse
     */
{'fraction_non_alphanumeric': 0.13232447650888615, 'fraction_numerical': 0.0007038535984515221, 'mean_word_length': 1.5615141955835963, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, 'https://': 2, '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': '1183429', 'n_tokens_mistral': 1593, 'n_tokens_neox': 1534, 'n_words': 277}