Skip to content

Commit

Permalink
- Added example 13;
Browse files Browse the repository at this point in the history
- Added faster fetching of members and units from DB table instead of Profile data.
  • Loading branch information
macrini committed Oct 23, 2020
1 parent bc927d0 commit d176bd6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 9 deletions.
12 changes: 7 additions & 5 deletions queries/example1/example1.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@
throw new Exception($response['error']);
}

$unitType = 'Faculty';
$units = $client->queryUnits($unitType, 'en');
$unitName = $units[1]['name'] ?? false;
$units = $client->queryUnits(['sortBy' => 'memberCount']);
$unit = array_pop($units); // last unit

if (!$unitName) {
throw new Exception("Cannot find a '$unitType' unit name");
if (!$unit) {
throw new Exception("Cannot find a unit");
}

$unitId = $unit['contentId'];
$unitName = $unit['unitName'];

// Get authorized API client
$filter = ['unit' => $unitName, 'title' => 'Professor'];

Expand Down
39 changes: 39 additions & 0 deletions queries/example13.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* Request a list of members from the DB table Members.
* By not specifying "resources", the API interprets that the desired
* response is simply a list of content-types items.
*/

require_once __DIR__ . '/../src/UniwebClient.php';

use Proximify\Uniweb\API\UniwebClient;

$client = new UniwebClient(UniwebClient::loadCredentials());

$units = $client->queryUnits(['sortBy' => 'memberCount']);
$unit = array_pop($units); // last unit

if (!$unit) {
throw new Exception("Cannot find a unit");
}

$unitId = $unit['contentId'];


// If "recourses" is not given, the query will fetch from a DB table
// of the requested content type. That is the fastest type of query.
// Profile resources are comparatively slower.
$request = [
'action' => 'read',
'contentType' => 'members',
'filter' => [
'units' => [$unitId]
]
];

$response = $client->sendRequest($request);

$unitName = $unit['unitName'];
$client->printResponse($response, "List of members in unit '$unitName':");
37 changes: 33 additions & 4 deletions src/UniwebClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,40 @@ public function getAccessToken()
/**
* Query units from the server.
*
* @param array $options Includes: 'lang', 'filter', and 'sortBy'.
* @return array
*/
public function queryUnits(array $options = []): array
{
$request = [
'contentType' => 'units',
'lang' => $options['lang'] ?? 'en',
'filter' => $options['filter'] ?? null
];

$units = $this->read($request);

$sortBy = $options['sortBy'] ?? false;

if ($sortBy) {
usort($units, function ($u1, $u2) use ($sortBy) {
$a = $u1[$sortBy];
$b = $u2[$sortBy];
return ($a == $b) ? 0 : ($a < $b ? -1 : 1);
});
}

return $units;
}

/**
* Query unit profiles from the server.
*
* @param string|null $unitType Filter response by type.
* @param string|null $lang Localize to selected language code ('en, 'fr).
* @return array
*/
public function queryUnits(string $unitType = null, string $lang = null): array
public function queryUnitProfiles(string $unitType = null, string $lang = null): array
{
$request = [
'contentType' => 'units',
Expand Down Expand Up @@ -483,9 +512,9 @@ public static function assertValidRequest($request)
self::throwError('Invalid request parameters');
}

if (empty($request['resources'])) {
self::throwError('Empty "resources" property in request');
}
// if (empty($request['resources'])) {
// self::throwError('Empty "resources" property in request');
// }
}

/**
Expand Down
5 changes: 5 additions & 0 deletions www/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ <h2>Example API queries</h2>
</td>
</tr>

<tr>
<td class="tg-2fdn"><a href="?example13">Get the members of a unit</a></td>
<td class="tg-2fdn">Choose one 'University' unit and request all members in it.</td>
</tr>

<tr>
<td class="tg-2fdn"><a href="?example3">Fetch CV data</a></td>
<td class="tg-2fdn">Request information about sections, fields
Expand Down

0 comments on commit d176bd6

Please sign in to comment.