-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventable.php
More file actions
251 lines (205 loc) · 8.44 KB
/
eventable.php
File metadata and controls
251 lines (205 loc) · 8.44 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
namespace Eventable;
define(EVENTABLE_API_URL, 'https://api.eventable.com/v1');
class Eventable
{
function __construct($token) {
$this->client = new EventableClient($token);
}
public function create_event(array $event_data) {
$endpoint = '/events/';
$response = $this->client->post_request($endpoint, $event_data);
if ($response['status'] == 400) {
throw new EventableSDKException($response['content']);
}
$event = json_decode($response["content"]);
return $event;
}
public function update_event(array $event_data) {
$endpoint = Null;
if (isset($event_data['id'])) {
$endpoint = '/events/' . $event_data['id'] . '/';
}
elseif (isset($event_data['external_id'])) {
$get_endpoint = '/events/?external_id=' . $event_data['external_id'];
$get_response = $this->client->get_request($get_endpoint);
$content = json_decode($get_response["content"]);
if ($content->total < 1) {
throw new EventableSDKException("no event with the given external_id exists");
}
$event = $content->results[0];
$endpoint = '/events/' . $event->id . '/';
}
else {
throw new EventableSDKException('need an event id or external event id');
}
$response = $this->client->put_request($endpoint, $event_data);
if ($response['status'] != 200) {
throw new EventableSDKException($response['content']);
}
$event = json_decode($response["content"]);
return $event;
}
public function delete_event(array $event_data) {
$endpoint = Null;
if (isset($event_data['id'])) {
$endpoint = '/events/' . $event_data['id'];
}
elseif (isset($event_data['external_id'])){
$get_endpoint = '/events/?external_id=' . $event_data['external_id'];
$get_response = $this->client->get_request($get_endpoint);
$content = json_decode($get_response["content"]);
if ($content->total < 1) {
throw new EventableSDKException("no event with the given external_id exists");
}
$event = json_decode($get_response->content);
$endpoint = '/events/' . $event['id'] . '/';
}
else {
throw new EventableSDKException('need an event id or external event id');
}
$response = $this->client->delete_request($endpoint, $event_data);
if ($response['status'] != 204) {
throw new EventableSDKException($response['content']);
}
return true;
}
public function get_or_create_subscriber_by_alias($alias) {
$endpoint = '/subscribers/?alias=' . $alias;
$response = $this->client->get_request($endpoint);
$content = json_decode($response["content"]);
if ($content->total < 1) {
$post_endpoint = '/subscribers/';
$post_data = array('alias' => $alias);
$response = $this->client->post_request($post_endpoint, $post_data);
$content = json_decode($response["content"]);
if ($response['status'] != 201) {
throw new EventableSDKException($response['content']);
}
$subscriber = $content->results[0];
}
elseif ($content->total >= 1){
$subscriber = $content->results[0];
}
else {
throw new EventableSDKException('there was an error creating/updating your subscriber');
}
return $subscriber;
}
public function add_event_to_subscriber_by_alias(array $event_data, $alias) {
$subscriber = $this->get_or_create_subscriber_by_alias($alias);
$endpoint = '/subscribers/' . $subscriber->id . '/custom_events/';
if (isset($event_data['id'])) {
$event_id = $event_data['id'];
}
elseif (isset($event_data['external_id'])){
$get_endpoint = '/events/?external_id=' . $event_data['external_id'];
$response = $this->client->get_request($get_endpoint);
$content = json_decode($response["content"]);
if ($content->total < 1) {
throw new EventableSDKException("no event with the given external_id exists");
}
$event = $content->results[0];
$event_id = $event->id;
}
else {
throw new EventableSDKException('need an event id or external event id');
}
$response = $this->client->post_request($endpoint, array('id' => $event_id));
if ($response["status"] == 400) {
$content = json_decode($response['content']);
if ($content->detail == "Event already exists.") {
return true; # event was already added, but it's still good to return true
}
else{
throw new EventableSDKException($content);
}
}
return ($response["status"] == 201); # return true if successfully added
}
public function remove_event_from_subscriber_by_alias(array $event_data, $alias){
$subscriber = $this->get_or_create_subscriber_by_alias($alias);
if (isset($event_data['id'])) {
$event_id = $event_data['id'];
}
elseif (isset($event_data['external_id'])){
$get_endpoint = '/events/?external_id=' . $event_data['external_id'];
$response = $this->client->get_request($get_endpoint);
$content = json_decode($response["content"]);
if ($content->total < 1) {
throw new EventableSDKException("no event with the given external_id exists");
}
$event = $content->results[0];
$event_id = $event->id;
}
else {
throw new EventableSDKException('need an event id or external event id');
}
$endpoint = '/subscribers/' . $subscriber->id . '/custom_events/' . $event_id . '/';
$response = $this->client->delete_request($endpoint);
if ($response['status'] != 204){
throw new EventableSDKException($response['content']);
}
return true;
}
}
class EventableClient
{
function __construct($token) {
$this->token = $token;
}
private function create_request($url, $method) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $this->token
));
return $ch;
}
public function get_request($endpoint, array $params=array()) {
$url = EVENTABLE_API_URL . $endpoint;
$ch = $this->create_request($url, "GET");
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array('content' => $response, 'status' => $httpCode);
}
public function post_request($endpoint, array $data) {
$url = EVENTABLE_API_URL . $endpoint;
$ch = $this->create_request($url, "POST");
$body = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array('content' => $response, 'status' => $httpCode);
}
public function put_request($endpoint, array $data) {
$url = EVENTABLE_API_URL . $endpoint;
$ch = $this->create_request($url, "PUT");
$body = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array('content' => $response, 'status' => $httpCode);
}
public function delete_request($endpoint) {
$url = EVENTABLE_API_URL . $endpoint;
$ch = $this->create_request($url, "DELETE");
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array('content' => $response, 'status' => $httpCode);
}
}
class EventableSDKException extends \Exception
{
public function __construct($message, $code = 0, \Exception $previous = null) {
parent::__construct($message, $code, $previous);
}
}
?>