diff --git a/composer.json b/composer.json index e0667c1..8e07918 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "roanokecollege/ethos", + "name": "roanoke_college/ethos", "description": "Roanoke College custom package for Ethos interactions", "type": "library", "license": "MIT", @@ -14,15 +14,17 @@ "email": "chssmith@roanoke.edu" } ], - "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" + ] + } } } diff --git a/config/ethos.php b/config/ethos.php new file mode 100644 index 0000000..0d1fa2d --- /dev/null +++ b/config/ethos.php @@ -0,0 +1,6 @@ + env("ETHOS_API_KEY"), + "proxy_url" => env("ETHOS_PROXY_URL"), + "api_header" => env("ETHOS_API_HEADER", "application/vnd.hedtech.integration.v6+json") + ]; diff --git a/src/EthosRequest.php b/src/EthosRequest.php new file mode 100644 index 0000000..9009ace --- /dev/null +++ b/src/EthosRequest.php @@ -0,0 +1,92 @@ +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(); + } +} diff --git a/src/EthosServiceProvider.php b/src/EthosServiceProvider.php new file mode 100644 index 0000000..e6d6d0a --- /dev/null +++ b/src/EthosServiceProvider.php @@ -0,0 +1,33 @@ +mergeConfigFrom( + __DIR__ . "/../config/ethos.php", "ethos" + ); + } + + /** + * Bootstrap services. + * + * @return void + */ + public function boot() + { + $this->publishes ([ + __DIR__ . "/../config/ethos.php" => config_path("ethos.php"), + ]); + } +}