Created
September 24, 2020 20:02
-
-
Save nikola-wd/94540fe97f0512b158878fdbd22e198f to your computer and use it in GitHub Desktop.
[“Async” Promise.all PHP Guzzle version] Guzzle version of Promise.all #PHP, #async, #Guzzle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ping_app_fetch_counts(WP_REST_Request $request) | |
{ | |
if (is_request_not_from_the_same_domain()) { | |
wp_send_json_error( "Not authorized", 401 ); | |
return false; | |
} | |
$ping_env_id = get_option('wrwps_ping_env_id'); | |
$base_endpoint = 'https://api.pingone.com/v1/environments/' . $ping_env_id . '/'; | |
$token = get_jwt_token_string(); | |
$headers = [ | |
"content-type" => "application/json", | |
"authorization" => "Bearer " . $token | |
]; | |
$client = new Client([ | |
'base_uri' => $base_endpoint, | |
'headers' => $headers | |
]); | |
try { | |
$promises = [ | |
'USERS_ALL' => $client->getAsync('users?limit=1'), | |
'PATIENTS_USERS' => $client->getAsync('users?limit=1&filter=%20patientStatus%20eq%20%22FALSE%22%20'), | |
'USERS_PENDING' => $client->getAsync('users?limit=1&filter=%20patientStatus%20eq%20%22PENDING%22%20'), | |
'USERS_APPROVED' => $client->getAsync('users?limit=1&filter=%20patientStatus%20eq%20%22APPROVED%22%20') | |
]; | |
$responses = Promise\unwrap($promises); | |
$responses = Promise\settle($promises)->wait(); | |
function map_responses($response) | |
{ | |
$response['value']->getBody()->rewind(); | |
$mod_response = $response['value']->getBody()->getContents(); | |
$data = json_decode($mod_response, true); | |
return $data['count']; | |
} | |
$promises_values = array_map('map_responses', $responses); | |
wp_send_json($promises_values); | |
} catch (RequestException $e) { | |
echo Psr7\str($e->getRequest()); | |
if ($e->hasResponse()) { | |
echo Psr7\str($e->getResponse()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment