forked from BoldGrid/w3-total-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cdn_Highwinds_Api.php
218 lines (150 loc) · 5.1 KB
/
Cdn_Highwinds_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
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
<?php
namespace W3TC;
class Cdn_Highwinds_Api {
static private $root_uri = 'https://striketracker3.highwinds.com';
private $account_hash;
private $api_token;
static public function users_me( $api_token ) {
$result = wp_remote_get( self::$root_uri . '/api/v1/users/me', array(
'headers' => 'Authorization: Bearer ' . $api_token
) );
$r = self::_decode_response( $result );
if ( !$r['auth_required'] )
return $r['response_json'];
throw new \Exception( 'Authentication failed' );
}
public function __construct( $account_hash, $api_token ) {
$this->account_hash = $account_hash;
$this->api_token = $api_token;
}
public function analytics_transfer( $host, $granularity, $platforms,
$start_date, $end_date ) {
return $this->_wp_remote_get(
self::$root_uri . '/api/v1/accounts/' . $this->account_hash .
'/analytics/transfer?startDate=' . urlencode( $start_date ) .
'&endDate=' . urlencode( $end_date ) .
'&granularity=' . urlencode( $granularity ) .
'&platforms=' . urlencode( $platforms ) );
}
public function configure_scopes( $host ) {
return $this->_wp_remote_get(
self::$root_uri . '/api/v1/accounts/' . $this->account_hash .
'/hosts/' . $host . '/configuration/scopes' );
}
public function configure_scope_get( $host, $scope_id ) {
return $this->_wp_remote_get(
self::$root_uri . '/api/v1/accounts/' . $this->account_hash .
'/hosts/' . $host . '/configuration/' . $scope_id );
}
public function configure_scope_set( $host, $scope_id, $configuration ) {
return $this->_wp_remote_put(
self::$root_uri . '/api/v1/accounts/' . $this->account_hash .
'/hosts/' . $host . '/configuration/' . $scope_id,
json_encode( $configuration )
);
}
public function host_add( $host ) {
return $this->_wp_remote_post(
self::$root_uri . '/api/v1/accounts/' . $this->account_hash . '/hosts',
json_encode( $host )
);
}
public function hosts() {
return $this->_wp_remote_get(
self::$root_uri . '/api/v1/accounts/' . $this->account_hash . '/hosts' );
}
public function origin_add( $origin ) {
return $this->_wp_remote_post(
self::$root_uri . '/api/v1/accounts/' . $this->account_hash . '/origins',
json_encode( $origin )
);
}
public function origins() {
return $this->_wp_remote_get(
self::$root_uri . '/api/v1/accounts/' . $this->account_hash . '/origins' );
}
/**
* $recursive - true/false
*/
public function purge( $urls, $recursive ) {
$list = array();
foreach ( $urls as $url ) {
$list[] = array(
'url' => $url,
'recursive' => $recursive );
}
$response = $this->_wp_remote_post(
self::$root_uri . '/api/v1/accounts/' . $this->account_hash . '/purge',
json_encode( array( 'list' => $list ) )
);
}
public function services() {
return $this->_wp_remote_get(
self::$root_uri . '/api/v1/accounts/' . $this->account_hash . '/services'
);
}
private function _wp_remote_get( $url, $body = array() ) {
$result = wp_remote_get( $url, array(
'headers' => 'Authorization: Bearer ' . $this->api_token,
'body' => $body
) );
$r = self::_decode_response( $result );
if ( !$r['auth_required'] )
return $r['response_json'];
throw new \Exception( 'Authentication failed' );
}
private function _wp_remote_post( $url, $body ) {
$headers = array(
'Authorization' => 'Bearer ' . $this->api_token
);
if ( !is_array( $body ) )
$headers['Content-Type'] = 'application/json';
$result = wp_remote_post( $url, array(
'headers' => $headers,
'body' => $body
) );
$r = self::_decode_response( $result );
if ( !$r['auth_required'] )
return $r['response_json'];
throw new \Exception( 'Authentication failed' );
}
private function _wp_remote_put( $url, $body ) {
$headers = array(
'Authorization' => 'Bearer ' . $this->api_token
);
if ( !is_array( $body ) )
$headers['Content-Type'] = 'application/json';
$result = wp_remote_post( $url, array(
'headers' => $headers,
'body' => $body,
'method' => 'PUT'
) );
$r = self::_decode_response( $result );
if ( !$r['auth_required'] )
return $r['response_json'];
throw new \Exception( 'Authentication failed' );
}
static private function _decode_response( $result ) {
if ( is_wp_error( $result ) )
throw new \Exception( 'Failed to reach API endpoint' );
$response_json = @json_decode( $result['body'], true );
if ( is_null( $response_json ) ) {
if ( $result['response']['code'] == '200' && empty( $result['body'] ) )
return array(
'response_json' => array(),
'auth_required' => false
);
throw new \Exception(
'Failed to reach API endpoint, got unexpected response ' .
$result['body'] );
}
if ( isset( $response_json['error'] ) ) {
if ( isset( $response_json['code'] ) && $response_json['code'] == '203' )
return array( 'response_json' => $response_json, 'auth_required' => true );
throw new \Exception( $response_json['error'] );
}
if ( $result['response']['code'] != '200' && $result['response']['code'] != '201' )
throw new \Exception( $result['body'] );
return array( 'response_json' => $response_json, 'auth_required' => false );
}
}