Making WordPress.org

Changeset 6800


Ignore:
Timestamp:
02/28/2018 12:22:39 AM (8 years ago)
Author:
coreymckrill
Message:

WordCamp Utilities: Add internal method for redundant remote get requests

This replaces wcorg_redundant_remote_get with a method that doesn't have
other WordCamp-specific dependencies so that the Meetup client can more easily
be used in other environments.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/utilities/class-meetup-client.php

    r6596 r6800  
    5959            $request_url = $this->sign_request_url( $request_url );
    6060
    61             $response = wcorg_redundant_remote_get( $request_url );
     61            $response = $this->tenacious_remote_get( $request_url );
    6262
    6363            if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
     
    115115        $request_url = $this->sign_request_url( $request_url );
    116116
    117         $response = wcorg_redundant_remote_get( $request_url );
     117        $response = $this->tenacious_remote_get( $request_url );
    118118
    119119        if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
     
    139139
    140140        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;
    141198    }
    142199
     
    213270     * Extract error information from an API response and add it to our error handler.
    214271     *
    215      * @param array $response
     272     * @param array|\WP_Error $response
    216273     *
    217274     * @return void
    218275     */
    219276    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 );
    221293
    222294        if ( isset( $data['errors'] ) ) {
     
    226298        } elseif ( isset( $data['code'] ) && isset( $data['details'] ) ) {
    227299            $this->error->add( $data['code'], $data['details'] );
    228         } else {
     300        } elseif ( $response_code ) {
    229301            $this->error->add(
    230302                'http_response_code',
    231                 wp_remote_retrieve_response_code( $response ) . ': ' . print_r( $data, true )
     303                sprintf( 'HTTP Status: %d', absint( $response_code ) )
    232304            );
     305        } else {
     306            $this->error->add( 'unknown_error', 'There was an unknown error.' );
    233307        }
    234308    }
     
    291365     *
    292366     * @param string $group_slug The slug/urlname of a group.
    293      * @param array $args        Optional. Additional request parameters.
     367     * @param array  $args       Optional. Additional request parameters.
    294368     *                           See https://www.meetup.com/meetup_api/docs/:urlname/events/
    295369     *
Note: See TracChangeset for help on using the changeset viewer.