Cdn_RackSpace_Api_Cdn.php
6.11 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
<?php
namespace W3TC;
class Cdn_RackSpace_Api_Cdn {
private $_access_token;
private $_access_region_descriptor;
private $_new_access_required = null;
public function __construct( $config = array() ) {
$this->_access_token = $config['access_token'];
$this->_access_region_descriptor = $config['access_region_descriptor'];
$this->_new_access_required = $config['new_access_required'];
}
public function services() {
$r = $this->_wp_remote_get( '/services' );
if ( !isset( $r['services'] ) )
return array();
return $r['services'];
}
public function service_get( $service ) {
$response = $this->_wp_remote_get( '/services/' . $service );
// expand links to links_by_rel
if ( isset( $response['links'] ) ) {
$by_rel = array();
foreach ( $response['links'] as $r ) {
$by_rel[ $r['rel'] ] = $r;
}
$response['links_by_rel'] = $by_rel;
}
return $response;
}
public function service_create( $data ) {
// required static
$data['flavor_id'] = 'cdn';
return $this->_wp_remote_post( '/services', json_encode( $data ),
array(
'Accept' => 'application/json',
'Content-type' => 'application/json'
) );
}
public function service_set( $service_id, $data ) {
return $this->_wp_remote_patch( '/services/' . $service_id,
json_encode( $data ),
array(
'Accept' => 'application/json',
'Content-type' => 'application/json'
) );
}
public function purge( $service_id, $url ) {
return $this->_wp_remote_delete( '/services/' . $service_id . '/assets?url=' .
urlencode( $url ) );
}
private function _wp_remote_get( $uri ) {
if ( !empty( $this->_access_region_descriptor['cdn.publicURL'] ) ) {
$url_base = $this->_access_region_descriptor['cdn.publicURL'];
$result = wp_remote_get( $url_base . $uri . '?format=json', array(
'headers' => 'X-Auth-Token: ' . $this->_access_token,
'sslcertificates' => dirname( __FILE__ ) . '/Cdn_RackSpace_Api_CaCert.pem'
) );
$r = self::_decode_response_json( $result );
if ( !$r['auth_required'] )
return $r['response_json'];
}
$new_object = call_user_func( $this->_new_access_required );
return $new_object->_wp_remote_get( $uri );
}
private function _wp_remote_patch( $uri, $body = array(), $headers = array() ) {
if ( !empty( $this->_access_region_descriptor['cdn.publicURL'] ) ) {
$url_base = $this->_access_region_descriptor['cdn.publicURL'];
$headers['X-Auth-Token'] = $this->_access_token;
$result = wp_remote_post( $url_base . $uri, array(
'headers' => $headers,
'body' => $body,
'sslcertificates' => dirname( __FILE__ ) . '/Cdn_RackSpace_Api_CaCert.pem',
'method' => 'PATCH'
) );
$r = self::_decode_response( $result );
if ( !$r['auth_required'] ) {
$location = explode( '/', $result['headers']['location'] );
return $location[ count( $location ) - 1 ];
}
}
$new_object = call_user_func( $this->_new_access_required );
return $new_object->_wp_remote_patch( $uri, $body );
}
private function _wp_remote_post( $uri, $body = array(), $headers = array() ) {
if ( !empty( $this->_access_region_descriptor['cdn.publicURL'] ) ) {
$url_base = $this->_access_region_descriptor['cdn.publicURL'];
$headers['X-Auth-Token'] = $this->_access_token;
$result = wp_remote_post( $url_base . $uri, array(
'headers' => $headers,
'body' => $body,
'sslcertificates' => dirname( __FILE__ ) . '/Cdn_RackSpace_Api_CaCert.pem'
) );
$r = self::_decode_response( $result );
if ( !$r['auth_required'] ) {
$location = explode( '/', $result['headers']['location'] );
return $location[ count( $location ) - 1 ];
}
}
$new_object = call_user_func( $this->_new_access_required );
return $new_object->_wp_remote_post( $uri, $body );
}
private function _wp_remote_delete( $uri, $headers = array() ) {
if ( !empty( $this->_access_region_descriptor['cdn.publicURL'] ) ) {
$url_base = $this->_access_region_descriptor['cdn.publicURL'];
$headers['X-Auth-Token'] = $this->_access_token;
$result = wp_remote_post( $url_base . $uri, array(
'headers' => $headers,
'sslcertificates' => dirname( __FILE__ ) . '/Cdn_RackSpace_Api_CaCert.pem',
'method' => 'DELETE'
) );
$r = self::_decode_response( $result );
if ( !$r['auth_required'] )
return;
}
$new_object = call_user_func( $this->_new_access_required );
return $new_object->_wp_remote_delete( $uri, $body );
}
static private function _decode_response_json( $result ) {
if ( is_wp_error( $result ) )
throw new \Exception( 'Failed to reach API endpoint' );
if ( empty( $result['body'] ) )
$response_json = array();
else {
$response_json = @json_decode( $result['body'], true );
if ( is_null( $response_json ) )
throw new \Exception(
'Failed to reach API endpoint, got unexpected response ' .
$result['body'] );
}
if ( $result['response']['code'] != '200' &&
$result['response']['code'] != '201' &&
$result['response']['code'] != '202' &&
$result['response']['code'] != '204' )
throw new \Exception( $result['body'] );
return array( 'response_json' => $response_json, 'auth_required' => false );
}
static private function _decode_response( $result ) {
if ( is_wp_error( $result ) )
throw new \Exception( 'Failed to reach API endpoint' );
if ( $result['response']['code'] != '200' &&
$result['response']['code'] != '201' &&
$result['response']['code'] != '202' &&
$result['response']['code'] != '204' ) {
// try to decode response
$response_json = @json_decode( $result['body'], true );
if ( is_null( $response_json ) ||
!isset( $response_json['message'] ) )
throw new \Exception(
'Failed to reach API endpoint, got unexpected response ' .
$result['response']['message'] );
else {
$errors = array();
if ( is_string( $response_json['message'] ) ) {
$errors[] = $response_json['message'];
} elseif ( isset( $response_json['message']['errors'] ) ) {
foreach ( $response_json['message']['errors'] as $error )
$errors[] = $error['message'];
}
throw new \Exception( implode( ';', $errors ) );
}
}
return array( 'auth_required' => false );
}
}