1: <?php
2: namespace Hook;
3:
4: class Client {
5: protected static $instance;
6:
7: public $keys;
8: protected $credentials;
9:
10: 11: 12: 13:
14: public static function getInstance() {
15: return self::$instance;
16: }
17:
18: 19: 20: 21: 22:
23: public static function configure($credentials = array()) {
24: return self::$instance = new static($credentials);
25: }
26:
27: 28: 29: 30:
31: function __construct($credentials = array()) {
32: if (isset($credentials['url'])) { $credentials['endpoint'] = $credentials['url']; }
33:
34:
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: 45: 46:
47: public function collection($name) {
48: return new Collection(array(
49: 'name' => $name,
50: 'client' => $this
51: ));
52: }
53:
54: 55: 56: 57: 58:
59: public function get($segments, $params = array(), $headers = array()) {
60: return $this->request('get', $segments, $params, $headers);
61: }
62:
63: 64: 65: 66: 67:
68: public function remove($segments, $headers = array()) {
69: return $this->request('delete', $segments, array(), $headers);
70: }
71:
72: 73: 74: 75: 76: 77:
78: public function put($segments, $data = array(), $headers = array()) {
79: return $this->request('put', $segments, $data, $headers);
80: }
81:
82: 83: 84: 85: 86: 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 ($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: