You are here

abstract class Github_HttpClient in Bibliography Module 7.2

Performs requests on GitHub API. API documentation should be self-explanatory.

@author Thibault Duplessis <thibault.duplessis at gmail dot com> @license MIT License

Hierarchy

Expanded class hierarchy of Github_HttpClient

File

modules/CiteProc/Github/HttpClient.php, line 9

View source
abstract class Github_HttpClient implements Github_HttpClientInterface {

  /**
   * The http client options
   * @var array
   */
  protected $options = array(
    'protocol' => 'http',
    'url' => ':protocol://github.com/api/v2/:format/:path',
    'format' => 'json',
    'user_agent' => 'php-github-api (http://github.com/ornicar/php-github-api)',
    'http_port' => 80,
    'timeout' => 10,
    'login' => null,
    'token' => null,
  );
  protected static $history = array();

  /**
   * Instanciate a new http client
   *
   * @param  array   $options  http client options
   */
  public function __construct(array $options = array()) {
    $this->options = array_merge($this->options, $options);
  }

  /**
   * Send a request to the server, receive a response
   *
   * @param  string   $url           Request url
   * @param  array    $parameters    Parameters
   * @param  string   $httpMethod    HTTP method to use
   * @param  array    $options        Request options
   *
   * @return string   HTTP response
   */
  protected abstract function doRequest($url, array $parameters = array(), $httpMethod = 'GET', array $options);

  /**
   * Send a GET request
   *
   * @param  string   $path            Request path
   * @param  array    $parameters     GET Parameters
   * @param  string   $httpMethod     HTTP method to use
   * @param  array    $options        Request options
   *
   * @return array                    Data
   */
  public function get($path, array $parameters = array(), array $options = array()) {
    return $this
      ->request($path, $parameters, 'GET', $options);
  }

  /**
   * Send a POST request
   *
   * @param  string   $path            Request path
   * @param  array    $parameters     POST Parameters
   * @param  string   $httpMethod     HTTP method to use
   * @param  array    $options        reconfigure the request for this call only
   *
   * @return array                    Data
   */
  public function post($path, array $parameters = array(), array $options = array()) {
    return $this
      ->request($path, $parameters, 'POST', $options);
  }

  /**
   * Send a request to the server, receive a response,
   * decode the response and returns an associative array
   *
   * @param  string   $path            Request API path
   * @param  array    $parameters     Parameters
   * @param  string   $httpMethod     HTTP method to use
   * @param  array    $options        Request options
   *
   * @return array                    Data
   */
  public function request($path, array $parameters = array(), $httpMethod = 'GET', array $options = array()) {
    $this
      ->updateHistory();
    $options = array_merge($this->options, $options);

    // create full url
    $url = strtr($options['url'], array(
      ':protocol' => $options['protocol'],
      ':format' => $options['format'],
      ':path' => trim($path, '/'),
    ));

    // get encoded response
    $response = $this
      ->doRequest($url, $parameters, $httpMethod, $options);

    // decode response
    $response = $this
      ->decodeResponse($response, $options);
    return $response;
  }

  /**
   * Get a JSON response and transform it to a PHP array
   *
   * @return  array   the response
   */
  protected function decodeResponse($response, array $options) {
    if ('text' === $options['format']) {
      return $response;
    }
    elseif ('json' === $options['format']) {
      return json_decode($response, true);
    }
    throw new Exception(__CLASS__ . ' only supports json & text format, ' . $options['format'] . ' given.');
  }

  /**
   * Change an option value.
   *
   * @param string $name   The option name
   * @param mixed  $value  The value
   *
   * @return Github_HttpClientInterface The current object instance
   */
  public function setOption($name, $value) {
    $this->options[$name] = $value;
    return $this;
  }

  /**
   * Records the requests times
   * When 30 request have been sent in less than a minute,
   * sleeps for two second to prevent reaching GitHub API limitation.
   *
   * @access protected
   * @return void
   */
  protected function updateHistory() {
    self::$history[] = time();
    if (30 === count(self::$history)) {
      if (reset(self::$history) >= time() - 35) {
        sleep(2);
      }
      array_shift(self::$history);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Github_HttpClient::$history protected static property
Github_HttpClient::$options protected property The http client options
Github_HttpClient::decodeResponse protected function Get a JSON response and transform it to a PHP array
Github_HttpClient::doRequest abstract protected function Send a request to the server, receive a response 1
Github_HttpClient::get public function Send a GET request Overrides Github_HttpClientInterface::get
Github_HttpClient::post public function Send a POST request Overrides Github_HttpClientInterface::post
Github_HttpClient::request public function Send a request to the server, receive a response, decode the response and returns an associative array
Github_HttpClient::setOption public function Change an option value. Overrides Github_HttpClientInterface::setOption
Github_HttpClient::updateHistory protected function Records the requests times When 30 request have been sent in less than a minute, sleeps for two second to prevent reaching GitHub API limitation.
Github_HttpClient::__construct public function Instanciate a new http client