-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathNylas.php
More file actions
232 lines (192 loc) · 8.1 KB
/
Nylas.php
File metadata and controls
232 lines (192 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
namespace Nylas;
use Nylas\Models;
use GuzzleHttp\Client as GuzzleClient;
class Nylas {
protected $apiServer = 'https://api.nylas.com';
protected $apiClient;
protected $apiToken;
public $apiRoot = '';
public function __construct($appID, $appSecret, $token=NULL, $apiServer=NULL) {
$this->appID = $appID;
$this->appSecret = $appSecret;
$this->apiToken = $token;
$this->apiClient = $this->createApiClient();
if($apiServer) {
$this->apiServer = $apiServer;
}
}
protected function createHeaders() {
$token = 'Basic '.base64_encode($this->apiToken.':');
$headers = array('headers' => ['Authorization' => $token,
'X-Nylas-API-Wrapper' => 'php']);
return $headers;
}
private function createApiClient() {
return new GuzzleClient(['base_url' => $this->apiServer]);
}
public function createAuthURL($redirect_uri, $login_hint=NULL) {
$args = array("client_id" => $this->appID,
"redirect_uri" => $redirect_uri,
"response_type" => "code",
"scope" => "email",
"login_hint" => $login_hint,
"state" => $this->generateId());
return $this->apiServer.'/oauth/authorize?'.http_build_query($args);
}
public function getAuthToken($code) {
$args = array("client_id" => $this->appID,
"client_secret" => $this->appSecret,
"grant_type" => "authorization_code",
"code" => $code);
$url = $this->apiServer.'/oauth/token';
$payload = array();
$payload['headers']['Content-Type'] = 'application/x-www-form-urlencoded';
$payload['headers']['Accept'] = 'text/plain';
$payload['body'] = $args;
$response = $this->apiClient->post($url, $payload)->json();
if(array_key_exists('access_token', $response)) {
$this->apiToken = $response['access_token'];
}
return $this->apiToken;
}
public function account() {
$apiObj = new NylasAPIObject();
$nsObj = new Models\Account();
$accountData = $this->getResource('', $nsObj, '', array());
$account = $apiObj->_createObject($accountData->klass, NULL, $accountData->data);
return $account;
}
public function threads() {
$msgObj = new Models\Thread($this);
return new NylasModelCollection($msgObj, $this, NULL, array(), 0, array());
}
public function messages() {
$msgObj = new Models\Message($this);
return new NylasModelCollection($msgObj, $this, NULL, array(), 0, array());
}
public function drafts() {
$msgObj = new Models\Draft($this);
return new NylasModelCollection($msgObj, $this, NULL, array(), 0, array());
}
public function labels() {
$msgObj = new Models\Label($this);
return new NylasModelCollection($msgObj, $this, NULL, array(), 0, array());
}
public function files() {
$msgObj = new Models\File($this);
return new NylasModelCollection($msgObj, $this, NULL, array(), 0, array());
}
public function contacts() {
$msgObj = new Models\Contact($this);
return new NylasModelCollection($msgObj, $this, NULL, array(), 0, array());
}
public function calendars() {
$msgObj = new Models\Calendar($this);
return new NylasModelCollection($msgObj, $this, NULL, array(), 0, array());
}
public function events() {
$msgObj = new Models\Event($this);
return new NylasModelCollection($msgObj, $this, NULL, array(), 0, array());
}
public function getResources($namespace, $klass, $filter) {
$suffix = ($namespace) ? '/'.$klass->apiRoot.'/'.$namespace : '';
$url = $this->apiServer.$suffix.'/'.$klass->collectionName;
$url = $url.'?'.http_build_query($filter);
$data = $this->apiClient->get($url, $this->createHeaders())->json();
$mapped = array();
foreach ($data as $i) {
$mapped[] = clone $klass->_createObject($this, $namespace, $i);
}
return $mapped;
}
public function getResource($namespace, $klass, $id, $filters) {
$extra = '';
if(array_key_exists('extra', $filters)) {
$extra = $filters['extra'];
unset($filters['extra']);
}
$response = $this->getResourceRaw($namespace, $klass, $id, $filters);
return $klass->_createObject($this, $namespace, $response);
}
public function getResourceRaw($namespace, $klass, $id, $filters) {
$extra = '';
if(array_key_exists('extra', $filters)) {
$extra = $filters['extra'];
unset($filters['extra']);
}
$prefix = ($namespace) ? '/'.$klass->apiRoot.'/'.$namespace : '';
$postfix = ($extra) ? '/'.$extra : '';
$url = $this->apiServer.$prefix.'/'.$klass->collectionName.'/'.$id.$postfix;
$url = $url.'?'.http_build_query($filters);
$data = $this->apiClient->get($url, $this->createHeaders())->json();
return $data;
}
public function getResourceData($namespace, $klass, $id, $filters) {
$extra = '';
$customHeaders = array();
if(array_key_exists('extra', $filters)) {
$extra = $filters['extra'];
unset($filters['extra']);
}
if(array_key_exists('headers', $filters)) {
$customHeaders = $filters['headers'];
unset($filters['headers']);
}
$prefix = ($namespace) ? '/'.$klass->apiRoot.'/'.$namespace : '';
$postfix = ($extra) ? '/'.$extra : '';
$url = $this->apiServer.$prefix.'/'.$klass->collectionName.'/'.$id.$postfix;
$url = $url.'?'.http_build_query($filters);
$customHeaders = array_merge($this->createHeaders()['headers'], $customHeaders);
$headers = array('headers' => $customHeaders);
$data = $this->apiClient->get($url, $headers)->getBody();
return $data;
}
public function _createResource($namespace, $klass, $data) {
$prefix = ($namespace) ? '/'.$klass->apiRoot.'/'.$namespace : '';
$url = $this->apiServer.$prefix.'/'.$klass->collectionName;
$payload = $this->createHeaders();
if($klass->collectionName == 'files') {
$payload['headers']['Content-Type'] = 'multipart/form-data';
$payload['body'] = $data;
} else {
$payload['headers']['Content-Type'] = 'application/json';
$payload['json'] = $data;
}
$response = $this->apiClient->post($url, $payload)->json();
return $klass->_createObject($this, $namespace, $response);
}
public function _updateResource($namespace, $klass, $id, $data) {
$prefix = ($namespace) ? '/'.$klass->apiRoot.'/'.$namespace : '';
$url = $this->apiServer.$prefix.'/'.$klass->collectionName.'/'.$id;
if($klass->collectionName == 'files') {
$payload['headers']['Content-Type'] = 'multipart/form-data';
$payload['body'] = $data;
} else {
$payload = $this->createHeaders();
$payload['json'] = $data;
$response = $this->apiClient->put($url, $payload)->json();
return $klass->_createObject($this, $namespace, $response);
}
}
public function _deleteResource($namespace, $klass, $id) {
$prefix = ($namespace) ? '/'.$klass->apiRoot.'/'.$namespace : '';
$url = $this->apiServer.$prefix.'/'.$klass->collectionName.'/'.$id;
$payload = $this->createHeaders();
$response = $this->apiClient->delete($url, $payload)->json();
return $response;
}
private function generateId() {
// Generates unique UUID
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff)
);
}
}