Overview

Namespaces

  • Hook

Classes

  • Client
  • Collection
  • Keys
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: namespace Hook;
  3: 
  4: class Client {
  5:     protected static $instance;
  6: 
  7:     public $keys;
  8:     protected $credentials;
  9: 
 10:     /**
 11:      * getInstance
 12:      * @return Hook\Client
 13:      */
 14:     public static function getInstance() {
 15:         return self::$instance;
 16:     }
 17: 
 18:     /**
 19:      * configure
 20:      * @static
 21:      * @param array $credentials
 22:      */
 23:     public static function configure($credentials = array()) {
 24:         return self::$instance = new static($credentials);
 25:     }
 26: 
 27:     /**
 28:      * __construct
 29:      * @param array $credentials
 30:      */
 31:     function __construct($credentials = array()) {
 32:         if (isset($credentials['url'])) { $credentials['endpoint'] = $credentials['url']; }
 33: 
 34:         // set default dl-api endpoint
 35:         if (!isset($credentials['endpoint'])) {
 36:             $credentials['endpoint'] = 'https://dl-api.heroku.com/';
 37:         }
 38: 
 39:         $this->credentials = $credentials;
 40:         $this->keys = new Keys($this);
 41:     }
 42: 
 43:     /**
 44:      * collection
 45:      * @param string $name
 46:      */
 47:     public function collection($name) {
 48:         return new Collection(array(
 49:             'name' => $name,
 50:             'client' => $this
 51:         ));
 52:     }
 53: 
 54:     /**
 55:      * get
 56:      * @param mixed $segments
 57:      * @param array $headers
 58:      */
 59:     public function get($segments, $params = array(), $headers = array()) {
 60:         return $this->request('get', $segments, $params, $headers);
 61:     }
 62: 
 63:     /**
 64:      * remove
 65:      * @param mixed $segments
 66:      * @param array $headers
 67:      */
 68:     public function remove($segments, $headers = array()) {
 69:         return $this->request('delete', $segments, array(), $headers);
 70:     }
 71: 
 72:     /**
 73:      * put
 74:      * @param mixed $segments
 75:      * @param array $data
 76:      * @param array $headers
 77:      */
 78:     public function put($segments, $data = array(), $headers = array()) {
 79:         return $this->request('put', $segments, $data, $headers);
 80:     }
 81: 
 82:     /**
 83:      * post
 84:      * @param mixed $segments
 85:      * @param array $data
 86:      * @param array $headers
 87:      */
 88:     public function post($segments, $data = array(), $headers = array()) {
 89:         return $this->request('post', $segments, $data, $headers);
 90:     }
 91: 
 92:     protected function request($method, $segments, $data = array(), $headers = array()) {
 93:         $client = new \GuzzleHttp\Client();
 94:         $method = strtoupper($method);
 95:         $body = null;
 96: 
 97:         if ($method === "GET" && !empty($data)) {
 98:             $segments .= '?' . urlencode(json_encode($data));
 99: 
100:         } elseif ($method !== "GET" && !empty($data)) {
101:             $body = json_encode($data);
102:         }
103: 
104:         return $client->{$method}($this->credentials['endpoint'] . $segments, array(
105:             'headers' => $this->getHeaders(),
106:             'body' => $body,
107:         ))->json();
108:     }
109: 
110:     protected function getHeaders($concat = array()) {
111:         return array_merge(array(
112:             'Content-Type' => 'application/json',
113:             'X-App-Id' => $this->credentials['app_id'],
114:             'X-App-Key' => $this->credentials['key'],
115:             'User-Agent' => 'hook-php'
116:         ), $concat);
117: 
118:     }
119: 
120: }
121: 
API documentation generated by ApiGen 2.8.0