<commit_before><?php

namespace Acquia\Cloud\Api;

use Acquia\Common\AcquiaServiceManagerAware;
use Acquia\Common\Json;
use Guzzle\Common\Collection;
use Guzzle\Service\Client;

class CloudApiClient extends Client implements AcquiaServiceManagerAware
{
    const BASE_PATH = '/v1';

    /**
     * {@inheritdoc}
     *
     * @return \Acquia\Cloud\Api\CloudApiClient
     */
    public static function factory($config = array())
    {
        $required = array(
            'base_url',
            'username',
            'password',
        );

        $defaults = array(
            'base_url' => 'https://cloudapi.acquia.com',
            'base_path' => self::BASE_PATH,
        );

        // Instantiate the Acquia Search plugin.
        $config = Collection::fromConfig($config, $defaults, $required);
        $client = new static($config->get('base_url'), $config);
        $client->setDefaultHeaders(array(
            'Content-Type' => 'application/json; charset=utf-8',
        ));

        // Attach the Acquia Search plugin to the client.
        $plugin = new CloudApiAuthPlugin($config->get('username'), $config->get('password'));
        $client->addSubscriber($plugin);

        return $client;
    }

    /**
     * {@inheritdoc}
     */
    public function getBuilderParams()
    {
        return array(
            'base_url' => $this->getConfig('base_url'),
            'username' => $this->getConfig('username'),
            'password' => $this->getConfig('password'),
        );
    }

    /**
     * Helper method to send a GET request and return parsed JSON.
     *
     * @param string $path
     * @param array $variables
     *   Variables used to expand the URI expressions.
     *
     * @return array
     *
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
     *
     * @see http://guzzlephp.org/http-client/uri-templates.html
     */
    public function sendGet($path, $variables = array())
    {
        return $this->get(array($path, $variables))->send()->json();
    }

    /**
     * Helper method to send a GET request and save to a file.
     *
     * @param string $path
     * @param array $variables
     *   Variables used to expand the URI expressions.
     * @param string $tofile
     *   Path to save file
     *
     * @return array
     *
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
     *
     * @see http://guzzlephp.org/http-client/uri-templates.html
     */
    public function saveGet($path, array $variables, $tofile)
    {
        return $this->get(array($path, $variables))->setResponseBody($tofile)->send();
    }

    /**
     * Helper method to send a POST request and return parsed JSON.
     *
     * The variables passed in the second parameter are used to expand the URI
     * expressions, which are usually the resource identifiers being requested.
     *
     * @param string $path
     * @param array $variables
     *   Variables used to expand the URI expressions.
     * @param mixed $body
     *   Defaults to null. If a non-string is passed then the data is converted
     *   to JSON.
     *
     * @return array
     *
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
     *
     * @see http://guzzlephp.org/http-client/uri-templates.html
     */
    public function sendPost($path, $variables = array(), $body = null)
    {
        if (!is_string($body)) {
            $body = Json::encode($body);
        }
        return $this->post(array($path, $variables), null, $body)->send()->json();
    }

    /**
     * Helper method to send a DELETE request and return parsed JSON.
     *
     * @param string $path
     * @param array $variables
     *   Variables used to expand the URI expressions.
     *
     * @return array
     *
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
     *
     * @see http://guzzlephp.org/http-client/uri-templates.html
     */
    public function sendDelete($path, $variables = array())
    {
        return $this->delete(array($path, $variables))->send()->json();
    }

    /**
     * @return \Acquia\Cloud\Api\Response\Sites
     *
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
     */
    public function sites()
    {
        $data = $this->sendGet('{+base_path}/sites.json');
        return new Response\Sites($data);
    }

    /**
     * @param string $site
     *
     * @return \Acquia\Cloud\Api\Response\Site<commit_msg>Fixes 404ing doc links<commit_after><?php

namespace Acquia\Cloud\Api;

use Acquia\Common\AcquiaServiceManagerAware;
use Acquia\Common\Json;
use Guzzle\Common\Collection;
use Guzzle\Service\Client;

class CloudApiClient extends Client implements AcquiaServiceManagerAware
{
    const BASE_PATH = '/v1';

    /**
     * {@inheritdoc}
     *
     * @return \Acquia\Cloud\Api\CloudApiClient
     */
    public static function factory($config = array())
    {
        $required = array(
            'base_url',
            'username',
            'password',
        );

        $defaults = array(
            'base_url' => 'https://cloudapi.acquia.com',
            'base_path' => self::BASE_PATH,
        );

        // Instantiate the Acquia Search plugin.
        $config = Collection::fromConfig($config, $defaults, $required);
        $client = new static($config->get('base_url'), $config);
        $client->setDefaultHeaders(array(
            'Content-Type' => 'application/json; charset=utf-8',
        ));

        // Attach the Acquia Search plugin to the client.
        $plugin = new CloudApiAuthPlugin($config->get('username'), $config->get('password'));
        $client->addSubscriber($plugin);

        return $client;
    }

    /**
     * {@inheritdoc}
     */
    public function getBuilderParams()
    {
        return array(
            'base_url' => $this->getConfig('base_url'),
            'username' => $this->getConfig('username'),
            'password' => $this->getConfig('password'),
        );
    }

    /**
     * Helper method to send a GET request and return parsed JSON.
     *
     * @param string $path
     * @param array $variables
     *   Variables used to expand the URI expressions.
     *
     * @return array
     *
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
     *
     * @see http://docs.guzzlephp.org/en/latest/http-client/uri-templates.html
     */
    public function sendGet($path, $variables = array())
    {
        return $this->get(array($path, $variables))->send()->json();
    }

    /**
     * Helper method to send a GET request and save to a file.
     *
     * @param string $path
     * @param array $variables
     *   Variables used to expand the URI expressions.
     * @param string $tofile
     *   Path to save file
     *
     * @return array
     *
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
     *
     * @see http://docs.guzzlephp.org/en/latest/http-client/uri-templates.html
     */
    public function saveGet($path, array $variables, $tofile)
    {
        return $this->get(array($path, $variables))->setResponseBody($tofile)->send();
    }

    /**
     * Helper method to send a POST request and return parsed JSON.
     *
     * The variables passed in the second parameter are used to expand the URI
     * expressions, which are usually the resource identifiers being requested.
     *
     * @param string $path
     * @param array $variables
     *   Variables used to expand the URI expressions.
     * @param mixed $body
     *   Defaults to null. If a non-string is passed then the data is converted
     *   to JSON.
     *
     * @return array
     *
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
     *
     * @see http://docs.guzzlephp.org/en/latest/http-client/uri-templates.html
     */
    public function sendPost($path, $variables = array(), $body = null)
    {
        if (!is_string($body)) {
            $body = Json::encode($body);
        }
        return $this->post(array($path, $variables), null, $body)->send()->json();
    }

    /**
     * Helper method to send a DELETE request and return parsed JSON.
     *
     * @param string $path
     * @param array $variables
     *   Variables used to expand the URI expressions.
     *
     * @return array
     *
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
     *
     * @see http://docs.guzzlephp.org/en/latest/http-client/uri-templates.html
     */
    public function sendDelete($path, $variables = array())
    {
        return $this->delete(array($path, $variables))->send()->json();
    }

    /**
     * @return \Acquia\Cloud\Api\Response\Sites
     *
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
     */
    public function sites()
    {
        $data = $this->sendGet('{+base_path}/sites.json');
        return new Response\Sites($data);
    }

    /**
     * @param string $site
     *
     * @return \Acquia\Cloud\Api\Response\Site
{'fraction_non_alphanumeric': 0.13219997763113744, 'fraction_numerical': 0.0007829101890168885, 'mean_word_length': 2.2540029112081514, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 57, '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': '658859', 'n_tokens_mistral': 2641, 'n_tokens_neox': 2573, 'n_words': 620}