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

Retry logic for the fetch groups request #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
42 changes: 28 additions & 14 deletions plugins/bcc-login/includes/class-bcc-coreapi-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Loading