<commit_before><?php
namespace Czim\Service\Services;

use Czim\Service\Contracts\ServiceInterpreterInterface;
use Czim\Service\Contracts\ServiceRequestDefaultsInterface;
use Czim\Service\Contracts\ServiceRequestInterface;
use Czim\Service\Exceptions\CouldNotConnectException;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception as GuzzleException;

class RestService extends AbstractService
{
    const USER_AGENT = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

    const METHOD_GET  = 'GET';
    const METHOD_POST = 'POST';


    /**
     * The method to use for the HTTP call
     *
     * @var string
     */
    protected $method = self::METHOD_POST;

    /**
     * Whether to use basic authentication
     *
     * @var bool
     */
    protected $basicAuth = false;

    /**
     * HTTP headers
     *
     * @var array
     */
    protected $headers = [];

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


    /**
     * @param ServiceRequestDefaultsInterface $defaults
     * @param ServiceInterpreterInterface     $interpreter
     * @param array                           $guzzleConfig     default config to pass into the guzzle client
     */
    public function __construct(ServiceRequestDefaultsInterface $defaults = null, ServiceInterpreterInterface $interpreter = null, array $guzzleConfig = [])
    {
        $this->client = new Client($guzzleConfig);

        parent::__construct($defaults, $interpreter);
    }

    /**
     * Performs raw REST call
     *
     * @param ServiceRequestInterface $request
     * @return mixed
     * @throws CouldNotConnectException
     * @throws Exception
     */
    protected function callRaw(ServiceRequestInterface $request)
    {
        $url = rtrim($request->getLocation(), '/') . '/' . $request->getMethod();

        $options = [
            'http_errors' => false,
        ];


        // handle authentication

        $credentials = $request->getCredentials();

        if (    $this->basicAuth
            &&  ! empty($credentials['name'])
            &&  ! empty($credentials['password'])
        ) {
            $options['auth'] = [ $credentials['name'], $credentials['password'] ];
        }


        // handle parameters and body

        switch ($this->method) {

            case static::METHOD_POST:
                $options['form_params'] = $request->getBody();

                $parameters = $request->getParameters();

                if ( ! empty($parameters)) {
                    $options['query'] = $request->getParameters();
                }
                break;

            case static::METHOD_GET:
                $options['query'] = $request->getbody() ?: [];
                break;

            // default omitted on purpose
        }

        // headers

        $headers = $request->getHeaders();

        if (count($headers)) {
            $options['headers'] = $headers;
        }

        // perform request<commit_msg>added PUT/PATCH to methods in RestService<commit_after><?php
namespace Czim\Service\Services;

use Czim\Service\Contracts\ServiceInterpreterInterface;
use Czim\Service\Contracts\ServiceRequestDefaultsInterface;
use Czim\Service\Contracts\ServiceRequestInterface;
use Czim\Service\Exceptions\CouldNotConnectException;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception as GuzzleException;

class RestService extends AbstractService
{
    const USER_AGENT = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

    const METHOD_GET   = 'GET';
    const METHOD_PATCH = 'PATCH';
    const METHOD_POST  = 'POST';
    const METHOD_PUT   = 'PUT';


    /**
     * The method to use for the HTTP call
     *
     * @var string
     */
    protected $method = self::METHOD_POST;

    /**
     * Whether to use basic authentication
     *
     * @var bool
     */
    protected $basicAuth = false;

    /**
     * HTTP headers
     *
     * @var array
     */
    protected $headers = [];

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


    /**
     * @param ServiceRequestDefaultsInterface $defaults
     * @param ServiceInterpreterInterface     $interpreter
     * @param array                           $guzzleConfig     default config to pass into the guzzle client
     */
    public function __construct(ServiceRequestDefaultsInterface $defaults = null, ServiceInterpreterInterface $interpreter = null, array $guzzleConfig = [])
    {
        $this->client = new Client($guzzleConfig);

        parent::__construct($defaults, $interpreter);
    }

    /**
     * Performs raw REST call
     *
     * @param ServiceRequestInterface $request
     * @return mixed
     * @throws CouldNotConnectException
     * @throws Exception
     */
    protected function callRaw(ServiceRequestInterface $request)
    {
        $url = rtrim($request->getLocation(), '/') . '/' . $request->getMethod();

        $options = [
            'http_errors' => false,
        ];


        // handle authentication

        $credentials = $request->getCredentials();

        if (    $this->basicAuth
            &&  ! empty($credentials['name'])
            &&  ! empty($credentials['password'])
        ) {
            $options['auth'] = [ $credentials['name'], $credentials['password'] ];
        }


        // handle parameters and body

        switch ($this->method) {

            case static::METHOD_PATCH:
            case static::METHOD_POST:
            case static::METHOD_PUT:
                $options['form_params'] = $request->getBody();

                $parameters = $request->getParameters();

                if ( ! empty($parameters)) {
                    $options['query'] = $request->getParameters();
                }
                break;

            case static::METHOD_GET:
                $options['query'] = $request->getbody() ?: [];
                break;

            // default omitted on purpose
        }

        // headers

        $headers = $request->getHeaders();

        if (count($headers)) {
            $options['headers'] = $headers;
        }

        // perform request
{'fraction_non_alphanumeric': 0.11980883322346737, 'fraction_numerical': 0.0023071852340145024, 'mean_word_length': 2.0885496183206107, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '1049956', 'n_tokens_mistral': 1676, 'n_tokens_neox': 1589, 'n_words': 358}