<commit_before><?php

/*
 * eduVPN - End-user friendly VPN.
 *
 * Copyright: 2016-2018, The Commons Conservancy eduVPN Programme
 * SPDX-License-Identifier: AGPL-3.0+
 */

namespace SURFnet\VPN\Common\HttpClient;

use SURFnet\VPN\Common\HttpClient\Exception\ApiException;
use SURFnet\VPN\Common\HttpClient\Exception\HttpClientException;

class ServerClient
{
    /** @var HttpClientInterface */
    private $httpClient;

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

    /**
     * @param string $baseUri
     */
    public function __construct(HttpClientInterface $httpClient, $baseUri)
    {
        $this->httpClient = $httpClient;
        $this->baseUri = $baseUri;
    }

    /**
     * @param string $requestPath
     *
     * @return array|bool
     */
    public function get($requestPath, array $getData = [])
    {
        $requestUri = sprintf('%s/%s', $this->baseUri, $requestPath);
        if (0 !== count($getData)) {
            $requestUri = sprintf('%s?%s', $requestUri, http_build_query($getData));
        }

        return self::responseHandler(
            'GET',
            $requestPath,
            $this->httpClient->get($requestUri)
        );
    }

    /**
     * @param string $requestPath
     *
     * @return array|bool
     */
    public function post($requestPath, array $postData)
    {
        $requestUri = sprintf('%s/%s', $this->baseUri, $requestPath);

        return self::responseHandler(
            'POST',
            $requestPath,
            $this->httpClient->post($requestUri, $postData)
        );
    }

    /**
     * @param string $requestMethod
     * @param string $requestPath
     *
     * @return true|array
     */
    private static function responseHandler($requestMethod, $requestPath, array $clientResponse)
    {
        list($statusCode, $responseData) = $clientResponse;
        self::validateClientResponse($requestMethod, $requestPath, $statusCode, $responseData);

        if (400 <= $statusCode) {
            // either we sent an incorrect request, or there is a server error
            throw new HttpClientException(sprintf('[%d] %s "/%s": %s', $statusCode, $requestMethod, $requestPath, $responseData['error']));
        }

        // the request was correct, and there was not a server error
        if ($responseData[$requestPath]['ok']) {
            // our request was handled correctly
            if (array_key_exists('data', $responseData[$requestPath])) {
                return $responseData[$requestPath]['data'];
            }

            return true;
        }

        // our request was not handled correctly, something went wrong...
        throw new ApiException($responseData[$requestPath]['error']);
    }

    /**
     * @param string $requestMethod
     * @param string $requestPath
     * @param int    $statusCode
     * @param array  $responseData
     *
     * @return void<commit_msg>ServerClient now return null instead of true when no data was sent back<commit_after><?php

/*
 * eduVPN - End-user friendly VPN.
 *
 * Copyright: 2016-2018, The Commons Conservancy eduVPN Programme
 * SPDX-License-Identifier: AGPL-3.0+
 */

namespace SURFnet\VPN\Common\HttpClient;

use SURFnet\VPN\Common\HttpClient\Exception\ApiException;
use SURFnet\VPN\Common\HttpClient\Exception\HttpClientException;

class ServerClient
{
    /** @var HttpClientInterface */
    private $httpClient;

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

    /**
     * @param string $baseUri
     */
    public function __construct(HttpClientInterface $httpClient, $baseUri)
    {
        $this->httpClient = $httpClient;
        $this->baseUri = $baseUri;
    }

    /**
     * @param string $requestPath
     *
     * @return null|array
     */
    public function get($requestPath, array $getData = [])
    {
        $requestUri = sprintf('%s/%s', $this->baseUri, $requestPath);
        if (0 !== count($getData)) {
            $requestUri = sprintf('%s?%s', $requestUri, http_build_query($getData));
        }

        return self::responseHandler(
            'GET',
            $requestPath,
            $this->httpClient->get($requestUri)
        );
    }

    /**
     * @param string $requestPath
     *
     * @return null|array
     */
    public function post($requestPath, array $postData)
    {
        $requestUri = sprintf('%s/%s', $this->baseUri, $requestPath);

        return self::responseHandler(
            'POST',
            $requestPath,
            $this->httpClient->post($requestUri, $postData)
        );
    }

    /**
     * @param string $requestMethod
     * @param string $requestPath
     *
     * @return null|array
     */
    private static function responseHandler($requestMethod, $requestPath, array $clientResponse)
    {
        list($statusCode, $responseData) = $clientResponse;
        self::validateClientResponse($requestMethod, $requestPath, $statusCode, $responseData);

        if (400 <= $statusCode) {
            // either we sent an incorrect request, or there is a server error
            throw new HttpClientException(sprintf('[%d] %s "/%s": %s', $statusCode, $requestMethod, $requestPath, $responseData['error']));
        }

        // the request was correct, and there was not a server error
        if ($responseData[$requestPath]['ok']) {
            // our request was handled correctly
            if (!array_key_exists('data', $responseData[$requestPath])) {
                return null;
            }

            return $responseData[$requestPath]['data'];
        }

        // our request was not handled correctly, something went wrong...
        throw new ApiException($responseData[$requestPath]['error']);
    }

    /**
     * @param string $requestMethod
     * @param string $requestPath
     * @param int    $statusCode
     * @param array  $responseData
     *
     * @return void
{'fraction_non_alphanumeric': 0.13059893598764372, 'fraction_numerical': 0.004805217092843659, 'mean_word_length': 2.398250728862974, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 19, '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': '3807322', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1631, 'n_words': 383}