-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathWheniwork.php
More file actions
290 lines (252 loc) · 8.28 KB
/
Wheniwork.php
File metadata and controls
290 lines (252 loc) · 8.28 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
* Client library for the When I Work scheduling and attendance platform.
*
* Uses curl to request JSON responses from the When I Work API.
* This probably has more comments than code.
*
* Contributors:
* Daniel Olfelt <daniel@thisclicks.com>
*
* @author Daniel Olfelt <daniel@thisclicks.com>
* @author Conner McCall <conner@wheniwork.com>
* @version 0.2
*/
class Wheniwork
{
/**
* Library Version
*/
const VERSION = '0.2';
/**
* HTTP Methods
*/
const METHOD_GET = 'get';
const METHOD_POST = 'post';
const METHOD_PUT = 'put';
const METHOD_PATCH = 'patch';
const METHOD_DELETE = 'delete';
private $api_token;
private $api_endpoint = 'https://api.wheniwork.com/2';
private $api_headers = [];
private $verify_ssl = false;
private $user_agent = 'WhenIWork-PHP';
/**
* Create a new instance
*
* @param string $api_token The user WhenIWork API token
* @param array $options Allows you to set the `headers` and the `endpoint`
*/
function __construct($api_token = null, $options = [])
{
$this->api_token = $api_token;
$this->user_agent = $this->user_agent . '/' . static::VERSION;
if (!empty($options['endpoint'])) {
$this->setEndpoint($options['endpoint']);
}
if (!empty($options['headers'])) {
$this->setHeaders($options['headers'], true);
}
if (!empty($options['user_agent'])) {
$this->setUserAgent($options['user_agent'], true);
}
}
/**
* Set the user token for all requests
*
* @param string $api_token The user WhenIWork API token
* @return Wheniwork
*/
public function setToken($api_token)
{
$this->api_token = $api_token;
return $this;
}
/**
* Set the user agent for all requests
*
* @param string $user_agent The user agent sent with each request
* @return Wheniwork
*/
public function setUserAgent($user_agent)
{
$this->user_agent = $user_agent;
return $this;
}
/**
* Get the user token to save for future requests
*
* @return string The user WhenIWork API token
*/
public function getToken()
{
return $this->api_token;
}
/**
* Set the endpoint for all requests
*
* @param string $endpoint The WhenIWork API endpoint to use
* @return Wheniwork
*/
public function setEndpoint($endpoint)
{
$this->api_endpoint = $endpoint;
return $this;
}
/**
* Get the endpoint to use for future requests
*
* @return string The WhenIWork API endpoint
*/
public function getEndpoint()
{
return $this->api_endpoint;
}
/**
* Set the headers for all requests
*
* @param array $headers Global headers for all future requests
* @param bool $reset
* @return $this
*/
public function setHeaders(array $headers, $reset = false)
{
if ($reset === true) {
$this->api_headers = $headers;
} else {
$this->api_headers += $headers;
}
return $this;
}
/**
* Get the host to use for future requests
*
* @return array Global headers array
*/
public function getHeaders()
{
return $this->api_headers;
}
/**
* Get an object or list.
*
* @param string $method The API method to call, e.g. '/users/'
* @param array $params An array of arguments to pass to the method.
* @param array $headers Array of custom headers to be passed
* @return array Object of json decoded API response.
*/
public function get($method, $params = [], $headers = [])
{
return $this->makeRequest($method, self::METHOD_GET, $params, $headers);
}
/**
* Post to an endpoint.
*
* @param string $method The API method to call, e.g. '/shifts/publish/'
* @param array $params An array of data used to create the object.
* @param array $headers Array of custom headers to be passed
* @return array Object of json decoded API response.
*/
public function post($method, $params = [], $headers = [])
{
return $this->makeRequest($method, self::METHOD_POST, $params, $headers);
}
/**
* Create an object. Helper method for post.
*
* @param string $method The API method to call, e.g. '/users/'
* @param array $params An array of data used to create the object.
* @param array $headers Array of custom headers to be passed
* @return array Object of json decoded API response.
*/
public function create($method, $params = [], $headers = [])
{
return $this->post($method, $params, $headers);
}
/**
* Update an object. Must include the ID.
*
* @param string $method The API method to call, e.g. '/users/1'
* @param array $params An array of data to update the object. Only changed fields needed.
* @param array $headers Array of custom headers to be passed
* @return array Object of json decoded API response.
*/
public function update($method, $params = [], $headers = [])
{
return $this->makeRequest($method, self::METHOD_PUT, $params, $headers);
}
/**
* Delete an object. Must include the ID.
*
* @param string $method The API method to call, e.g. '/users/1'
* @param array $params An array of arguments to pass to the method.
* @param array $headers Array of custom headers to be passed
* @return array Object of json decoded API response.
*/
public function delete($method, $params = [], $headers = [])
{
return $this->makeRequest($method, self::METHOD_DELETE, $params, $headers);
}
/**
* Performs the underlying HTTP request. Exciting stuff happening here. Not really.
*
* @param string $method The API method to be called
* @param string $request The type of request
* @param array $params Assoc array of parameters to be passed
* @param array $headers Assoc array of custom headers to be passed
* @return array Assoc array of decoded result
*/
private function makeRequest($method, $request, $params = [], $headers = [])
{
$url = $this->getEndpoint() . '/' . $method;
if ($params && ($request == self::METHOD_GET || $request == self::METHOD_DELETE)) {
$url .= '?' . http_build_query($params);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
$headers += $this->getHeaders();
$headers['Content-Type'] = 'application/json';
if ($this->api_token) {
$headers['W-Token'] = $this->api_token;
}
$headers_data = [];
foreach ($headers as $k => $v) {
$headers_data[] = $k . ': ' . $v;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($request));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
if (in_array($request, [self::METHOD_POST, self::METHOD_PUT, self::METHOD_PATCH])) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
}
$result = curl_exec($ch);
curl_close($ch);
return $result ? json_decode($result) : false;
}
/**
* Login helper using developer key and credentials to get back a login response
*
* @param $key Developer API key
* @param $email Email of the user logging in
* @param $password Password of the user
* @return
*/
public static function login($key, $email, $password)
{
$params = [
"username" => $email,
"password" => $password,
];
$headers = [
'W-Key' => $key
];
$login = new static();
$response = $login->makeRequest("login", self::METHOD_POST, $params, $headers);
return $response;
}
}