From 47efd1c8fa84f71f549c2a7881f87403b15036c1 Mon Sep 17 00:00:00 2001 From: Simon Costea Date: Tue, 5 Mar 2024 16:44:36 +0200 Subject: [PATCH] Retry logic for the fetch groups request --- .../includes/class-bcc-coreapi-client.php | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/plugins/bcc-login/includes/class-bcc-coreapi-client.php b/plugins/bcc-login/includes/class-bcc-coreapi-client.php index c9651dc..320e501 100644 --- a/plugins/bcc-login/includes/class-bcc-coreapi-client.php +++ b/plugins/bcc-login/includes/class-bcc-coreapi-client.php @@ -43,10 +43,7 @@ function get_translated_site_groups() { return $site_groups; } - function fetch_groups($group_uids) - { - $token = $this->get_coreapi_token(); - + function fetch_groups($group_uids) { $qry = array( "uid" => array( "_in" => $group_uids, @@ -55,21 +52,39 @@ function fetch_groups($group_uids) $qry = json_encode($qry); - $response = wp_remote_get( str_replace("https://", "https://core.", $this->_settings->coreapi_base_url) . "/groups?fields=uid,name&filter=$qry", array( - "headers" => array( - "Authorization" => "Bearer ".$token - ) - ) ); + $body = $this->send_get_request( + str_replace("https://", "https://core.", $this->_settings->coreapi_base_url) . "/groups?fields=uid,name&filter=$qry" + ); + + return $body->data; + } + + function send_get_request($link) { + $response = null; + $body = null; + $retries = 3; + $token = $this->get_coreapi_token(); + + while ( (is_wp_error($response) || !$body || $body && !is_array($body->data) ) && $retries-- > 0) { + $response = wp_remote_get( $link, array( + "timeout" => 30, + "headers" => array( + "Authorization" => "Bearer " . $token + ) + ) ); + + if ( is_wp_error( $response ) ) + continue; + + $body = json_decode($response['body']); + } if ( is_wp_error( $response ) ) { wp_die( $response->get_error_message() ); } - $body = json_decode($response['body']); - - return $body->data; + return $body; } - function get_groups_for_user($user_uid) { $cache_key = 'coreapi_user_groups_'.$user_uid; @@ -112,7 +127,6 @@ function fetch_groups_for_user($user_uid) { wp_die("cannot fetch groups for user: " . print_r($response['body'], true)); } - $body = json_decode($response['body']); return $body->data->groupUids;