-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspace_api.php
94 lines (79 loc) · 2.5 KB
/
space_api.php
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
<?php
/* Copyright (c) 2014 Michael Wendland
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* Michael Wendland <[email protected]>
*/
// callAPI returns either the response or an error.
function callAPI($url) {
// FIXME set timeout, useragent, etc.
$api_response = wp_remote_get( $url );
$rsp_code = wp_remote_retrieve_response_code( $api_response );
$rsp_msg = wp_remote_retrieve_response_message( $api_response );
$rsp_body = wp_remote_retrieve_body( $api_response );
// If request wasn't successfull return error.
if( $rsp_code != 200) {
return new WP_ERROR(
'api_call_failed',
"Failed calling ".$url,
$rsp_msg );
}
return New APIResponse( $rsp_body );
}
// APIResponse holds the decoded data and provides api-version independend
// methods to get the fields needed in this plugin.
class APIResponse {
private $rsp;
function __construct($data) {
$this->rsp = json_decode( $data );
}
function getSpaceStatus() {
if( $this->getAPIVersion() < 13 ) {
return $this->rsp->open;
}
else {
return $this->rsp->state->open;
}
}
function getAPIVersion() {
return $this->rsp->api * 100;
}
function getIconOpen() {
if( $this->getAPIVersion() < 13 ) {
return $this->rsp->icon->open;
}
else {
return $this->rsp->state->icon->open;
}
}
function getIconClosed() {
if( $this->getAPIVersion() < 13 ) {
return $this->rsp->icon->closed;
}
else {
return $this->rsp->state->icon->closed;
}
}
function getLastChange() {
if( $this->getAPIVersion() < 13 ) {
return $this->rsp->lastchange;
}
else {
return $this->rsp->state->lastchange;
}
}
}
?>