<commit_before><?php

namespace LizardsAndPumpkins\MagentoConnector\Api;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

class Api
{
    const API_ENDPOINT_CATALOG_IMPORT = 'catalog_import/';
    const API_ENDPOINT_STOCK_UPDATE = 'multiple_product_stock_quantity/';
    const API_ENDPOINT_CONTENT_BLOCK_UPDATE = 'content_blocks/';

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

    /**
     * @param string $url
     */
    public function __construct($url)
    {
        $this->checkHost($url);

        $this->url = rtrim($url, '/') . '/';
    }

    /**
     * @param string $filename
     */
    public function triggerProductImport($filename)
    {
        $headers = ['Accept' => 'application/vnd.lizards-and-pumpkins.catalog_import.v1+json'];

        $url = $this->url . self::API_ENDPOINT_CATALOG_IMPORT;
        $this->sendApiRequestWithFilename($filename, $url, $headers);
    }

    /**
     * @param string $filename
     */
    public function triggerProductStockImport($filename)
    {
        $headers = ['Accept' => 'application/vnd.lizards-and-pumpkins.multiple_product_stock_quantity.v1+json'];

        $url = $this->url . self::API_ENDPOINT_STOCK_UPDATE;
        $this->sendApiRequestWithFilename($filename, $url, $headers);
    }

    /**
     * @param string $id
     * @param string $content
     * @param string[] $context
     * @param string[] $keyGeneratorParameters
     */
    public function triggerCmsBlockUpdate($id, $content, array $context, array $keyGeneratorParameters)
    {
        if (!is_string($id)) {
            throw new InvalidUrlException();
        }

        $headers = ['Accept' => 'application/vnd.lizards-and-pumpkins.content_blocks.v1+json'];
        $url = $this->url . self::API_ENDPOINT_CONTENT_BLOCK_UPDATE . $id;
        $body = json_encode(array_merge(['content' => $content, 'context' => $context], $keyGeneratorParameters));

        $this->sendApiRequest($url, $headers, $body);
    }

    /**
     * @param string $url
     */
    private function checkHost($url)
    {
        if (!is_string($url)) {
            throw new InvalidHostException('Host must be of type string.');
        }

        $urlParts = parse_url($url);
        if ($urlParts === false) {
            throw new InvalidHostException('URL seems to be  seriously malformed.');
        }

        if (empty($urlParts['scheme']) || $urlParts['scheme'] !== 'https') {
            // TODO comment
            #throw new InvalidHostException('Host should be called via HTTPS!');
        }

        if (empty($urlParts['host'])) {
            throw new InvalidHostException('Domain must be specified.');
        }
    }

    /**
     * @param string $method
     * @param string $url
     * @param string[] $headers
     * @param string $body
     * @return Request
     */
    private function createHttpRequest($method, $url, $headers, $body)
    {
        return new Request($method, $url, $headers, $body);
    }

    /**
     * @param string $filename
     * @param string $url
     * @param string[] $headers
     */
    private function sendApiRequestWithFilename($filename, $url, $headers)
    {
        $this->validateFilename($filename);
        $body = json_encode(['fileName' => $filename]);
        $this->sendApiRequest($url, $headers, $body);
    }

    /**
     * @param string $url
     * @param string[] $headers
     * @param string $body
     */
    private function sendApiRequest($url, $headers, $body)
    {
        $request = $this->createHttpRequest('PUT', $url, $headers, $body);
        $client = new Client();
        $response = $client->send($request);
        if (json_decode($response->getBody()) != 'OK') {
            throw new RequestFailedException("Unexpected response body from $url:\n" . $response->getBody());
        }
    }

    /**
     * @param string $filename
     */
    private function validateFilename($filename)
    {
        $dir = dirname($filename);
        if ($dir != '.') {
            throw new \UnexpectedValueException(
                sprintf('Filename "%s" should be a filename, no path.', $filename)
            );
        }
    }<commit_msg>Issue #298: Change API call success verification from response body to status code<commit_after><?php

namespace LizardsAndPumpkins\MagentoConnector\Api;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

class Api
{
    const API_ENDPOINT_CATALOG_IMPORT = 'catalog_import/';
    const API_ENDPOINT_STOCK_UPDATE = 'multiple_product_stock_quantity/';
    const API_ENDPOINT_CONTENT_BLOCK_UPDATE = 'content_blocks/';

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

    /**
     * @param string $url
     */
    public function __construct($url)
    {
        $this->checkHost($url);

        $this->url = rtrim($url, '/') . '/';
    }

    /**
     * @param string $filename
     */
    public function triggerProductImport($filename)
    {
        $headers = ['Accept' => 'application/vnd.lizards-and-pumpkins.catalog_import.v1+json'];

        $url = $this->url . self::API_ENDPOINT_CATALOG_IMPORT;
        $this->sendApiRequestWithFilename($filename, $url, $headers);
    }

    /**
     * @param string $filename
     */
    public function triggerProductStockImport($filename)
    {
        $headers = ['Accept' => 'application/vnd.lizards-and-pumpkins.multiple_product_stock_quantity.v1+json'];

        $url = $this->url . self::API_ENDPOINT_STOCK_UPDATE;
        $this->sendApiRequestWithFilename($filename, $url, $headers);
    }

    /**
     * @param string $id
     * @param string $content
     * @param string[] $context
     * @param string[] $keyGeneratorParameters
     */
    public function triggerCmsBlockUpdate($id, $content, array $context, array $keyGeneratorParameters)
    {
        if (!is_string($id)) {
            throw new InvalidUrlException();
        }

        $headers = ['Accept' => 'application/vnd.lizards-and-pumpkins.content_blocks.v1+json'];
        $url = $this->url . self::API_ENDPOINT_CONTENT_BLOCK_UPDATE . $id;
        $body = json_encode(array_merge(['content' => $content, 'context' => $context], $keyGeneratorParameters));

        $this->sendApiRequest($url, $headers, $body);
    }

    /**
     * @param string $url
     */
    private function checkHost($url)
    {
        if (!is_string($url)) {
            throw new InvalidHostException('Host must be of type string.');
        }

        $urlParts = parse_url($url);
        if ($urlParts === false) {
            throw new InvalidHostException('URL seems to be  seriously malformed.');
        }

        if (empty($urlParts['scheme']) || $urlParts['scheme'] !== 'https') {
            // TODO comment
            #throw new InvalidHostException('Host should be called via HTTPS!');
        }

        if (empty($urlParts['host'])) {
            throw new InvalidHostException('Domain must be specified.');
        }
    }

    /**
     * @param string $method
     * @param string $url
     * @param string[] $headers
     * @param string $body
     * @return Request
     */
    private function createHttpRequest($method, $url, $headers, $body)
    {
        return new Request($method, $url, $headers, $body);
    }

    /**
     * @param string $filename
     * @param string $url
     * @param string[] $headers
     */
    private function sendApiRequestWithFilename($filename, $url, $headers)
    {
        $this->validateFilename($filename);
        $body = json_encode(['fileName' => $filename]);
        $this->sendApiRequest($url, $headers, $body);
    }

    /**
     * @param string $url
     * @param string[] $headers
     * @param string $body
     */
    private function sendApiRequest($url, $headers, $body)
    {
        $request = $this->createHttpRequest('PUT', $url, $headers, $body);
        $client = new Client();
        $response = $client->send($request);
        if ($response->getStatusCode() !== 202) {
            throw new RequestFailedException("Unexpected response body from $url:\n" . $response->getBody());
        }
    }

    /**
     * @param string $filename
     */
    private function validateFilename($filename)
    {
        $dir = dirname($filename);
        if ($dir != '.') {
            throw new \UnexpectedValueException(
                sprintf('Filename "%s" should be a filename, no path.', $filename)
            );
        }
    }
{'fraction_non_alphanumeric': 0.1440373563218391, 'fraction_numerical': 0.0016762452107279694, 'mean_word_length': 2.4876826722338206, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, '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': '4654223', 'n_tokens_mistral': 2456, 'n_tokens_neox': 2411, 'n_words': 526}