Skip to content

Commit

Permalink
Added methods to get Person and PERC records
Browse files Browse the repository at this point in the history
  • Loading branch information
chssmith committed Oct 15, 2021
1 parent 9458cc0 commit 5f7dd8c
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 10 deletions.
22 changes: 12 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "roanokecollege/ethos",
"name": "roanoke_college/ethos",
"description": "Roanoke College custom package for Ethos interactions",
"type": "library",
"license": "MIT",
Expand All @@ -14,15 +14,17 @@
"email": "[email protected]"
}
],
"require": {},
"require": {
"guzzlehttp/guzzle"
},
"extra": {
"laravel": {
"providers": [
"RoanokeCollege\\Ethos\\ServiceProvider"
],
"aliases": [
"Ethos": "RoanokeCollege\\Ethos\\Facade"
]
}
"laravel": {
"providers": [
"RoanokeCollege\\Ethos\\EthosServiceProvider"
],
"aliases": [
"Ethos": "RoanokeCollege\\Ethos\\Facade"
]
}
}
}
6 changes: 6 additions & 0 deletions config/ethos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
return [
"api_key" => env("ETHOS_API_KEY"),
"proxy_url" => env("ETHOS_PROXY_URL"),
"api_header" => env("ETHOS_API_HEADER", "application/vnd.hedtech.integration.v6+json")
];
92 changes: 92 additions & 0 deletions src/EthosRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace RoanokeCollege\Ethos;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Collection;

class EthosRequest {

public function __construct () {
$this->api_url = config("ethos.proxy_url") . "api/";

try {
$auth_token_response = Http::withToken(config("ethos.api_key"))->post(config("ethos.proxy_url") . "auth");
$auth_token_response->throw();
$this->auth_token = $auth_token_response->body();
} catch (\Exception $e) {
$exception = new \Exception("Unable to retrieve authorization token. Please check your .env vars and try again");
throw $exception;
}
}

private function generateUrl (string $api_endpoint, string $guid = "") {
$url = $this->api_url . $api_endpoint;
if (!empty($guid)) {
$url .= "/" . $guid;
}

return $url;
}

/**
* Get Colleague record of a person by their RCID.
* This function gets you a Colleague record by specifying a persons name. This is most useful for getting a users GUID, but includes other personal data of the user.
*
* @param string $rcid The RCID of the user the record being requested.
* @return object A std_class object with all relevant colleague information for the requested user.
**/
public function getPersonRecordByRCID (string $rcid): object {
return Http::withToken($this->auth_token)->get($this->generateUrl("persons"), ["criteria" => "{'credentials':[{'type':'colleaguePersonId', 'value': '$rcid'}]}"])->object()[0];
}

/**
* Get a list of active PERCs for a student by GUID.
*
* @param string $guid The GUID of the person record we are trying to get the PERCs for.
* @return Collection A Laravel Collection of PERC entries for the specified student.
**/
public function getPersonHoldsByPersonGUID (string $guid): Collection {
return collect(Http::withToken($this->auth_token)->get($this->generateUrl("person-holds"), ["person" => $guid])->json());
}

/**
* Get a list of active PERCs for a student by RCID.
*
* @param string $rcid The RCID of the person record we are trying to get the PERCs for.
* @return Collection A Laravel collection of active PERC entries for the specified student.
**/
public function getPersonHoldsByRCID (string $rcid): Collection {
$person_record = $this->getPersonRecordByRCID ($rcid);
return $this->getPersonHoldsByPersonGUID ($person_record->id);
}

/**
* Get a specific PERC for a student by specifying the person-hold-type GUID
*
* @param string $rcid The RCID of the person record we are checking the PERCs for.
* @param string $guid The GUID of the person-hold-type entry of the PERC we are checking for.
* @return mixed Null if the student does not have the specified perc active. Otherwise, an array representing the PERC details.
**/
public function getPersonHoldByRCIDAndPercGUID (string $rcid, string $guid) {
return $this->getPersonHoldsByRCID($rcid)->where("type.detail.id", $guid)->first();
}

/**
* Update a PERC.
*
* @param array $person_hold an array of attributes to update a hold in colleague.
* MUST HAVE AN id ATTRIBUTE, which is the GUID of the PERC entry to be updated.
* @throws Exception If a PERC entry cannot be found for the specified ID.
**/
public function updatePersonHoldByEntry (array $person_hold) {
Http::withToken($this->auth_token)
->withHeaders([
"Accept" => config("ethos.api_header"),
"Content-Type" => config("ethos.api_header")
])
->withBody(json_encode($person_hold), "text/json")
->put($this->generateUrl("person-holds", $person_hold["id"]))
->throw();
}
}
33 changes: 33 additions & 0 deletions src/EthosServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace RoanokeCollege\Ethos;

use Illuminate\Support\ServiceProvider;


class EthosServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(
__DIR__ . "/../config/ethos.php", "ethos"
);
}

/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
$this->publishes ([
__DIR__ . "/../config/ethos.php" => config_path("ethos.php"),
]);
}
}

0 comments on commit 5f7dd8c

Please sign in to comment.