Skip to content

Commit aa97879

Browse files
authored
Merge pull request #220 from bcc-code/copilot/check-group-retrieval-limit
Paginate group fetching and reduce batch size to 500
2 parents 873b6e6 + 40f4edb commit aa97879

1 file changed

Lines changed: 38 additions & 20 deletions

File tree

plugins/bcc-login/includes/class-bcc-coreapi-client.php

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,28 @@ function fetch_groups($group_uids) {
107107

108108
$qry = json_encode($qry);
109109

110-
$response = wp_remote_get( str_replace("https://", "https://core.", $this->_settings->coreapi_base_url) . "/groups?limit=1000&fields=uid,name,tags&filter=$qry", array(
111-
"headers" => array(
112-
"Authorization" => "Bearer ".$token
113-
)
114-
) );
110+
$limit = 500;
111+
$offset = 0;
112+
$all_data = [];
115113

116-
if ( is_wp_error( $response ) ) {
117-
wp_die( $response->get_error_message() );
118-
}
114+
do {
115+
$response = wp_remote_get( str_replace("https://", "https://core.", $this->_settings->coreapi_base_url) . "/groups?limit=$limit&offset=$offset&fields=uid,name,tags&filter=$qry", array(
116+
"headers" => array(
117+
"Authorization" => "Bearer ".$token
118+
)
119+
) );
119120

120-
$body = json_decode($response['body']);
121+
if ( is_wp_error( $response ) ) {
122+
wp_die( $response->get_error_message() );
123+
}
124+
125+
$body = json_decode($response['body']);
126+
$data = $body->data;
127+
$all_data = array_merge($all_data, $data);
128+
$offset += $limit;
129+
} while (count($data) === $limit);
121130

122-
return $body->data;
131+
return $all_data;
123132
}
124133

125134
function fetch_groups_by_tags($tags) {
@@ -150,19 +159,28 @@ function fetch_groups_by_tag($tag) {
150159

151160
$qry = json_encode($qry);
152161

153-
$response = wp_remote_get( str_replace("https://", "https://core.", $this->_settings->coreapi_base_url) . "/groups?limit=1000&fields=uid,name,tags&filter=$qry", array(
154-
"headers" => array(
155-
"Authorization" => "Bearer ".$token
156-
)
157-
) );
162+
$limit = 500;
163+
$offset = 0;
164+
$all_data = [];
158165

159-
if ( is_wp_error( $response ) ) {
160-
wp_die( $response->get_error_message() );
161-
}
166+
do {
167+
$response = wp_remote_get( str_replace("https://", "https://core.", $this->_settings->coreapi_base_url) . "/groups?limit=$limit&offset=$offset&fields=uid,name,tags&filter=$qry", array(
168+
"headers" => array(
169+
"Authorization" => "Bearer ".$token
170+
)
171+
) );
162172

163-
$body = json_decode($response['body']);
173+
if ( is_wp_error( $response ) ) {
174+
wp_die( $response->get_error_message() );
175+
}
176+
177+
$body = json_decode($response['body']);
178+
$data = $body->data;
179+
$all_data = array_merge($all_data, $data);
180+
$offset += $limit;
181+
} while (count($data) === $limit);
164182

165-
return $body->data;
183+
return $all_data;
166184
}
167185

168186
function get_groups_for_user($user_uid) {

0 commit comments

Comments
 (0)