<?php
// Do not edit, this file was generated by github.com/apex/rpc.

class Client {
  protected $url;
  protected $authToken;

  /**
   * Create a new API client.
   *
   * @param string $url The endpoint URL.
   * @param string $authToken The authentication token [optional].
   */

  public function __construct($url, $authToken = null) {
    $this->url = $url;
    $this->authToken = $authToken;
  }

  /**
   * addItem adds an item to the list.
   *
   * @param array $params The input parameters.
   */
  public function addItem(array $params) {
    return $this->call("add_item", $params);
  }

  /**
   * getItems returns all items in the list.
   *
   * @return array
   */
  public function getItems() {
    return $this->call("get_items", null);
  }

  /**
   * removeItem removes an item from the to-do list.
   *
   * @param array $params The input parameters.
   * @return array
   */
  public function removeItem(array $params) {
    return $this->call("remove_item", $params);
  }

  private function call($method, $body) {
    $header = "Content-type: application/json\r\n";

    if (isset($this->authToken)) {
      $header .= "Authorization: Bearer $this->authToken\r\n";
    }

    $options = array(
      'http' => array(
        'header'  => $header,
        'method'  => 'POST',
        'content' => json_encode($body)
      )
    );

    $url = $this->url . "/" . $method;
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

    // TODO: how to check the status code and parse error from the body?

    return json_decode($result);
  }
}
