Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache the Api response #380

Merged
merged 5 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions includes/api/class-bc-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ protected function get_account_id() {
global $bc_accounts;

return $bc_accounts->get_account_id();

}

/**
Expand Down Expand Up @@ -90,17 +89,17 @@ public function get_last_error() {
}

return end( array_values( $this->errors ) );

}

/**
* Tries to get cached item first, then falls back to API call.
*
* @param string $url the url to call
* @param array $args the arguments to pass to the API call
* @param bool $cache_fail_response whether or not to cache the api fail response
* @return array|false|mixed|WP_Error
*/
private function cached_get( $url, $args ) {
private function cached_get( $url, $args, $cache_fail_response = false ) {

global $bc_accounts;

Expand All @@ -113,15 +112,30 @@ private function cached_get( $url, $args ) {
$account_id = $bc_accounts->get_account_id();
$transient_key = BC_Utility::generate_transient_key( '_brightcove_req_', $account_id . BC_Utility::get_hash_for_object( $url ) );
$request = BC_Utility::get_cache_item( $transient_key );
if ( false === $request ) {
$force_refresh = ! empty( $_GET['bc_refresh'] ) && '1' === $_GET['bc_refresh'] && ! empty( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'], 'bc_refresh' );

if ( false === $request || $force_refresh ) {
if ( function_exists( 'vip_safe_wp_remote_get' ) ) {
$request = vip_safe_wp_remote_get( $url, '', 3, 3, 20, $args );
} else {
$request = wp_remote_get( $url, $args );
}
$successful_response_codes = array( 200, 201, 202, 204 );

if ( in_array( wp_remote_retrieve_response_code( $request ), $successful_response_codes, true ) ) {
/**
* Filter whether to cache the api fail response.
*
* @since 2.8.5
* @hook brightcove_cache_api_fail_response
* @param {bool} $cache_fail_response Whether to cache the response.
* @param {string} $url The URL of the request.
* @param {array} $args The arguments of the request.
*
* @return bool Whether to cache the fail response.
*/
$cache_fail_response = apply_filters( 'brightcove_cache_api_fail_response', $cache_fail_response, $url, $args );

if ( in_array( wp_remote_retrieve_response_code( $request ), $successful_response_codes, true ) || $cache_fail_response ) {
BC_Utility::set_cache_item( $transient_key, '', $request, $cache_time_in_seconds );
}
}
Expand All @@ -139,10 +153,11 @@ private function cached_get( $url, $args ) {
* @param string $method the http method to use
* @param array $data array of further data to send to the server
* @param boolean $force_new_token whether or not to force obtaining a new oAuth token
* @param boolean $cache_fail_response whether or not to cache the api fail response
*
* @return mixed the return data from the call of false if a failure occurred
*/
protected function send_request( $url, $method = 'GET', $data = array(), $force_new_token = false ) {
protected function send_request( $url, $method = 'GET', $data = array(), $force_new_token = false, $cache_fail_response = false ) {

$method = strtoupper( sanitize_text_field( $method ) );

Expand Down Expand Up @@ -206,7 +221,7 @@ protected function send_request( $url, $method = 'GET', $data = array(), $force_
switch ( $method ) {

case 'GET':
$request = $this->cached_get( $url, $args );
$request = $this->cached_get( $url, $args, $cache_fail_response );

break;

Expand All @@ -225,7 +240,6 @@ protected function send_request( $url, $method = 'GET', $data = array(), $force_

$request = wp_remote_request( $url, $args );
break;

}

if ( 401 === wp_remote_retrieve_response_code( $request ) ) {
Expand Down Expand Up @@ -266,11 +280,9 @@ protected function send_request( $url, $method = 'GET', $data = array(), $force_
if ( isset( $body[0] ) && isset( $body[0]['error_code'] ) ) {

$message = $body[0]['error_code'];

} elseif ( isset( $body['message'] ) ) {

$message = $body['message'];

}

$this->errors[] = array(
Expand All @@ -285,18 +297,17 @@ protected function send_request( $url, $method = 'GET', $data = array(), $force_
if ( 204 === wp_remote_retrieve_response_code( $request ) ) {

return true;

}

if ( is_wp_error( $request ) ) {
$this->errors[] = array(
'url' => $url,
'error' => $request,
);
$this->errors[] = array(
'url' => $url,
'error' => $request,
);

BC_Logging::log( sprintf( 'BC API ERROR: %s', $request->get_error_message() ) );
BC_Logging::log( sprintf( 'BC API ERROR: %s', $request->get_error_message() ) );

return false;
return false;
}

if ( $transient_key && $body && ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) ) {
Expand All @@ -305,7 +316,6 @@ protected function send_request( $url, $method = 'GET', $data = array(), $force_
}

return $body;

}

/**
Expand Down Expand Up @@ -340,6 +350,5 @@ protected function get_authorization_header( $force_new_token = false ) {
}

return 'Bearer ' . $token;

}
}
2 changes: 1 addition & 1 deletion includes/api/class-bc-player-management-api2.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function get_all_players() {
$bc_accounts->set_current_account_by_id( $account_id );

$url = esc_url_raw( self::BASE_URL . $account_id . '/players/' );
$account_players = $this->send_request( $url );
$account_players = $this->send_request( $url, 'GET', [], false, true );

if ( ! $account_players || is_wp_error( $account_players ) ) {
return [];
Expand Down
14 changes: 11 additions & 3 deletions includes/class-bc-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,20 @@ public static function bc_admin_notices() {
}

if ( count( $bc_accounts->get_sanitized_all_accounts() ) > 0 && empty( $players ) && is_array( $players ) ) {
$force_refresh_url = add_query_arg(
[
'bc_refresh' => 1,
'nonce' => wp_create_nonce( 'bc_refresh' ),
],
admin_url( 'admin.php?page=brightcove-sources' )
);

$notices[] = array(
'message' => sprintf(
'%s <a href="%s"><strong>%s</strong></a>',
esc_html__( 'It looks like one or more of your accounts API authentication changed recently. Please update your settings ', 'brightcove' ),
// translators: %1$s: Update settings URL, %2$s: Force refresh URL.
esc_html__( 'It looks like one or more of your accounts API authentication has changed recently. Please update your settings <a href="%1$s"><strong>here</strong></a>. Or click <a href="%2$s"><strong>here</strong></a> to try again.', 'brightcove' ),
esc_url( admin_url( 'admin.php?page=brightcove-sources' ) ),
esc_html__( 'here', 'brightcove' )
esc_url( $force_refresh_url )
),
'type' => 'error',
);
Expand Down
8 changes: 1 addition & 7 deletions includes/class-bc-utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -745,13 +745,7 @@ public static function set_cache_item( $key, $type, $value, $expiration = 600 )
$type = sanitize_text_field( $type );
$expiration = absint( $expiration );

$transient_value = get_transient( $key );

if ( false === $transient_value ) {
return set_transient( $key, $value, $expiration );
}

return true; // already cached
return set_transient( $key, $value, $expiration );
}

/**
Expand Down
Loading