-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathApiRequest.class.php
264 lines (229 loc) · 9.99 KB
/
ApiRequest.class.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
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
<?php
// Make sure our response object is available
require_once( 'ApiResponse.class.php' );
require_once( 'ApiExceptions.class.php' );
/*
* This line is more for testing/use outside of WordPress. It can be safely
* removed if you do not need it
*/
//require_once(ABSPATH.'wp-load.php');
class ApiRequest {
private $mConfig = array();
/**
* Default constructor
*
* @throws InvalidArgumentException
* @throws InvalidParametersException
*/
public function __construct( $Config ) {
// Ensure the passed in config parameter is an array
if( !is_array( $Config ) )
throw new InvalidArgumentException( 'ApiRequest object requires an array of configuration data as a parameter' );
$required_config = array( 'api_entry' );
$default_config = array(
'response_type' => 'json',
'cache_enabled' => false,
'cache_lifetime' => 60, // In seconds. Default is 1 minute
'cache_prefix' => 'api_request_',
'check_for_update' => true, // Checks HTTP HEAD to see if there are updates since last cache
'force_caching' => false // If true the cache parameters in HEAD will be overridden by these config settings
);
$Config = wp_parse_args( $Config, $default_config );
if( !$this->verifyParameters( $required_config, $Config ) )
throw new InvalidParametersException( $required_config );
$this->setConfig( $Config );
}
/**
* Performs the GET query against the specified endpoint
*
* @todo Add conditional code to fetch header data first
* @throws UnkownErrorException
* @throws BadRequestException
* @throws UnauthorizedRequestException
* @throws InternalServerErrorException
* @param String $Url - Endpoint with URL paramters (for now)
* @return ApiResponse
*/
public function get( $Url ) {
/*
* Several things to check before we go fetch the data
*
* - Is caching enabled?
* --- Yes? Check to see if the transient is still valid
* ------ Yes? Set it as the response
* --------- Is check_for_update? (HTTP HEAD)
* ------------ Boolean: fetch_url = false
* ------------ Yes? Check HTTP HEAD
* --------------- If timestamp newer than transient
* ------------------ fetch_url = true
* --------- If fetch_url
* ------------ Go fetch!, Set transient
* ------ No? Go fetch!, Set transient
* --- No? Go fetch!
* - Return response
*
* To make this easier to read some of the conditionals may be broken
* apart in a not as efficient manner as possible
*/
if( $this->getConfig( 'cache_enabled' ) ) {
$transient_name = $this->transientName( $Url );
// Grab the cached data
$transient = get_transient( $transient_name );
if( !$transient ) {
// Cached data is no longer valid refresh it
$response = $this->performGet( $Url );
if( $response->isCachable() || $this->getConfig( 'force_caching' ))
set_transient( $transient_name, $response, $this->getConfig( 'cache_lifetime' ));
} else {
// Cached data is valid do we need to check the HTTP HEAD data?
if( $this->getConfig( 'check_for_update' )) {
// Need to check HTTP HEAD to see if there are any updates
// or if the expires heading has been exceeded
$head = $this->performHead( $Url );
if( $head->isExpired() ) {
// Cache is expired. Grab the data again.
$response = $this->performGet( $Url );
if( $response->isCachable() || $this->getConfig( 'force_caching' )) {
// Response is cachable (HTTP HEAD) or force_caching option is true
set_transient( $transient_name, $response, $this->getConfig( 'cache_lifetime' ));
}
}
} else {
$response = $transient;
}
}
} else {
// Caching is not enabled. Always perform the GET
$response = $this->performGet( $Url );
}
return $response;
}
private function transientName( $Name ) {
return $this->getConfig( 'cache_prefix' ) . md5($Name);
}
// Helper function
private function performGet( $Url ) {
$res = wp_remote_get( $Url );
//echo '<pre>';var_dump( wp_remote_retrieve_headers($res));
if( is_wp_error( $res ) ) {
// ( var_dump( ($res->errors) ));
if( count( $res->errors ) == 1 ) {
// If there was only 1 error let's see if I can deal with it
$error = array_keys( $res->errors );
$error = $error[0];
switch( $error ) {
case 'http_request_failed':
throw new HttpRequestFailedException( $Url );
default:
// WordPress had some error that I do not know about
throw new UnknownErrorException( $res );
}
echo '1 error';
} else {
// WordPress had some error that I do not know about
throw new UnknownErrorException( $res );
}
die();
} else {
$response = new ApiResponse();
$response->setHttpCode( wp_remote_retrieve_response_code( $res ) );
$response->setHeaders( wp_remote_retrieve_headers($res) );
$response->setResponse( wp_remote_retrieve_body( $res ) );
if( $response->getHttpCode() == '400' ) {
// 400 Bad request when there was a problem with the request
throw new BadRequestException($Url, $response);
} else if ( $response->getHttpCode() == '401' ) {
// 401 Unauthorized when you don't provide a valid key
throw new UnauthorizedRequestException();
} else if ( $response->getHttpCode() == '500' ) {
// 500 Internal Server Error
throw new InternalServerErrorException();
}
return $response;
}
}
private function performHead( $Url ) {
$res = wp_remote_head( $Url );
//echo '<pre>';var_dump( wp_remote_retrieve_headers($res));
if( is_wp_error( $res ) ) {
// WordPress had some error that I do not know about
throw new UnknownErrorException( $res );
} else {
$response = new ApiResponse();
$response->setHttpCode( wp_remote_retrieve_response_code( $res ) );
$response->setHeaders( wp_remote_retrieve_headers($res) );
// $response->setResponse( wp_remote_retrieve_body( $res ) );
if( $response->getHttpCode() == '400' ) {
// 400 Bad request when there was a problem with the request
throw new BadRequestException($Url, $response);
} else if ( $response->getHttpCode() == '401' ) {
// 401 Unauthorized when you don't provide a valid key
throw new UnauthorizedRequestException();
} else if ( $response->getHttpCode() == '500' ) {
// 500 Internal Server Error
throw new InternalServerErrorException();
}
return $response;
}
}
/**
*
* @throws InvalidParametersException
* @param <type> $Endpoint
* @param <type> $Parameters
* @param <type> $RequiredParameters - Optional - Some endpoints do not have parameters
* @return <type>
*/
public function buildUrl( $Endpoint, $Parameters = null, $RequiredParameters = null ) {
if(is_array($RequiredParameters) && !$this->verifyParameters( $RequiredParameters, $Parameters )) {
throw new InvalidParametersException( $RequiredParameters );
}
$url = trailingslashit( $this->getConfig( 'api_entry' ) ) . $Endpoint;
if( !is_null( $Parameters ) )
$url = add_query_arg( $Parameters, $url );
return $url;
}
/**
* Checks the input parameters against a list of required parameters to
* ensure at least one of the required parameters exists.
*
* NOTE: The Meetup API contains a list of parameters that are required for
* each endpoint with a default condition of "any of"
*
* @param Array $RequiredList - Names of required parameters
* @param Array $Parameters - List of provided paramters
* @return Boolean
*/
public function verifyParameters($RequiredList, $Parameters) {
if( !is_array( $Parameters ) ) return false;
$Parameters = array_keys($Parameters);
/*
* Check to see if any of the required list is in the parameters array
* Since the Meetup API requires "any of" if a required key is found in
* parameters the verification will pass
*/
foreach($RequiredList AS $r) {
if(in_array($r, $Parameters)) {
return true;
}
}
return false;
}
public function getConfig( $Var ) {
return ( isset( $this->mConfig[$Var] ) )? $this->mConfig[$Var] : null;
}
/**
*Recursive function
* @param type $Var
* @param type $Value
*/
public function setConfig( $Var, $Value = null ) {
if(is_array( $Var ) ) {
foreach( $Var AS $k => $v ) {
$this->setConfig( $k, $v );
}
} else {
$this->mConfig[$Var] = $Value;
}
}
} // end class