Changeset 6800
- Timestamp:
- 02/28/2018 12:22:39 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/utilities/class-meetup-client.php
r6596 r6800 59 59 $request_url = $this->sign_request_url( $request_url ); 60 60 61 $response = wcorg_redundant_remote_get( $request_url );61 $response = $this->tenacious_remote_get( $request_url ); 62 62 63 63 if ( 200 === wp_remote_retrieve_response_code( $response ) ) { … … 115 115 $request_url = $this->sign_request_url( $request_url ); 116 116 117 $response = wcorg_redundant_remote_get( $request_url );117 $response = $this->tenacious_remote_get( $request_url ); 118 118 119 119 if ( 200 === wp_remote_retrieve_response_code( $response ) ) { … … 139 139 140 140 return $count; 141 } 142 143 /** 144 * Wrapper for `wp_remote_get` to retry requests that fail temporarily for various reasons. 145 * 146 * Based on `wcorg_redundant_remote_get`. 147 * 148 * @param string $url 149 * @param array $args 150 * 151 * @return array|\WP_Error 152 */ 153 protected function tenacious_remote_get( $url, $args = array() ) { 154 $attempt_count = 0; 155 $max_attempts = 3; 156 157 // Response codes that should break the loop. See https://www.meetup.com/meetup_api/docs/#errors 158 // TODO are there others? 159 $breaking_codes = array( 160 200, // Ok. 161 400, // Bad request. 162 401, // Unauthorized (invalid key). 163 429, // Too many requests (rate-limited). 164 ); 165 166 while ( $attempt_count < $max_attempts ) { 167 $response = wp_remote_get( $url, $args ); 168 $response_code = wp_remote_retrieve_response_code( $response ); 169 170 if ( in_array( $response_code, $breaking_codes, true ) || is_wp_error( $response ) ) { 171 break; 172 } 173 174 $attempt_count ++; 175 176 /** 177 * Action: Fires when tenacious_remote_get fails a request attempt. 178 * 179 * Note that the request parameter includes the request URL that contains a query string for the API key. 180 * This should be redacted before outputting anywhere public. 181 * 182 * @param array $response 183 * @param array $request 184 * @param int $attempt_count 185 * @param int $max_attempts 186 */ 187 do_action( 'meetup_client_tenacious_remote_get_attempt', $response, compact( 'url', 'args' ), $attempt_count, $max_attempts ); 188 189 if ( $attempt_count < $max_attempts ) { 190 $retry_after = wp_remote_retrieve_header( $response, 'retry-after' ) ?: 5; 191 $wait = min( $retry_after * $attempt_count, 30 ); 192 193 sleep( $wait ); 194 } 195 } 196 197 return $response; 141 198 } 142 199 … … 213 270 * Extract error information from an API response and add it to our error handler. 214 271 * 215 * @param array $response272 * @param array|\WP_Error $response 216 273 * 217 274 * @return void 218 275 */ 219 276 protected function handle_error_response( $response ) { 220 $data = json_decode( wp_remote_retrieve_body( $response ), true ); 277 if ( is_wp_error( $response ) ) { 278 $codes = $response->get_error_codes(); 279 280 foreach ( $codes as $code ) { 281 $messages = $response->get_error_messages( $code ); 282 283 foreach ( $messages as $message ) { 284 $this->error->add( $code, $message ); 285 } 286 } 287 288 return; 289 } 290 291 $response_code = wp_remote_retrieve_response_code( $response ); 292 $data = json_decode( wp_remote_retrieve_body( $response ), true ); 221 293 222 294 if ( isset( $data['errors'] ) ) { … … 226 298 } elseif ( isset( $data['code'] ) && isset( $data['details'] ) ) { 227 299 $this->error->add( $data['code'], $data['details'] ); 228 } else {300 } elseif ( $response_code ) { 229 301 $this->error->add( 230 302 'http_response_code', 231 wp_remote_retrieve_response_code( $response ) . ': ' . print_r( $data, true)303 sprintf( 'HTTP Status: %d', absint( $response_code ) ) 232 304 ); 305 } else { 306 $this->error->add( 'unknown_error', 'There was an unknown error.' ); 233 307 } 234 308 } … … 291 365 * 292 366 * @param string $group_slug The slug/urlname of a group. 293 * @param array $argsOptional. Additional request parameters.367 * @param array $args Optional. Additional request parameters. 294 368 * See https://www.meetup.com/meetup_api/docs/:urlname/events/ 295 369 *
Note: See TracChangeset
for help on using the changeset viewer.