From fd73fce37552371d92cf528f64049d8b375b5478 Mon Sep 17 00:00:00 2001 From: Kumi Date: Thu, 7 Apr 2022 13:27:49 +0200 Subject: [PATCH] Initial commit - Crew Portal API --- .gitignore | 4 +++ classes/adonis.py | 76 +++++++++++++++++++++++++++++++++++++++++++ classes/config.py | 18 ++++++++++ classes/crew.py | 20 ++++++++++++ classes/exceptions.py | 5 +++ const.py | 1 + requirements.txt | 0 settings.dist.ini | 4 +++ worker.py | 30 +++++++++++++++++ 9 files changed, 158 insertions(+) create mode 100644 .gitignore create mode 100644 classes/adonis.py create mode 100644 classes/config.py create mode 100644 classes/crew.py create mode 100644 classes/exceptions.py create mode 100644 const.py create mode 100644 requirements.txt create mode 100644 settings.dist.ini create mode 100644 worker.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1c8f647 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +venv/ +*.pyc +__pycache__/ +settings.ini \ No newline at end of file diff --git a/classes/adonis.py b/classes/adonis.py new file mode 100644 index 0000000..c9c70e3 --- /dev/null +++ b/classes/adonis.py @@ -0,0 +1,76 @@ +from urllib.request import urlopen, Request, urljoin + +import json + +from classes.exceptions import AdonisAuthenticationDeniedError, AdonisError +from classes.crew import Crew + +from const import USER_AGENT + + +class Adonis: + def __init__(self, crew_portal_base_url: str, login: str, password: str): + self.crew_portal_base_url = crew_portal_base_url + self.login = login + self.password = password + + @classmethod + def fromConfig(cls, config): + return cls(config.crew_portal_base_url, config.login, config.password) + + def requestCrewPortal(self, endpoint: str, payload: dict, add_token: bool = True, wrap_request: bool = True, extract_response: bool = True): + url = urljoin(self.crew_portal_base_url, endpoint) + + req = Request(url) + req.add_header("User-Agent", USER_AGENT) + req.add_header("Content-Type", "application/json") + req.add_header("Accept", "application/json") + + if add_token: + payload["Authentication_Token"] = self.getToken() + + if wrap_request: + payload = {"request": payload} + + req.data = json.dumps(payload).encode() + + res = json.loads(urlopen(req).read()) + + if extract_response: + res = res[endpoint + "Result"] + + if not res["Authentication_Approved"]: + raise AdonisAuthenticationDeniedError( + res["Authentication_ReasonDenied"]) + + if res["ErrorText"]: + raise AdonisError(res["ErrorText"]) + + return res + + def getTokenCrewPortal(self, lifetime: int = 60): + data = { + "credentials": { + "Login": self.login, + "Password": self.password, + "LifeTime": lifetime + } + } + + result = self.requestCrewPortal("GNL_API_AUTHENTICATION", data, False, False) + + return result["Authentication_Token"] + + def getCrewPersonalData(self, pin): + data = { + "Pin": pin + } + + result = self.requestCrewPortal("DG_PersonalInformationRead", data) + + return result + + def getCrew(self, pin): + crew = Crew(pin) + crew._personal_data = self.getCrewPersonalData(pin) + return crew \ No newline at end of file diff --git a/classes/config.py b/classes/config.py new file mode 100644 index 0000000..2924d60 --- /dev/null +++ b/classes/config.py @@ -0,0 +1,18 @@ +from configparser import ConfigParser + +class Config: + def __init__(self, path): + self.config = ConfigParser() + self.config.read(path) + + @property + def crew_portal_base_url(self): + return self.config["ADONIS"]["CrewPortalBaseURL"] + + @property + def login(self): + return self.config["ADONIS"]["Login"] + + @property + def password(self): + return self.config["ADONIS"]["Password"] \ No newline at end of file diff --git a/classes/crew.py b/classes/crew.py new file mode 100644 index 0000000..c892542 --- /dev/null +++ b/classes/crew.py @@ -0,0 +1,20 @@ +class Crew: + def __init__(self, pin): + self.pin = pin + self._personal_data = None + + @property + def firstName(self): + return self._personal_data["DetailsAll"]["Details"]["FirstName"] + + @property + def middleName(self): + return self._personal_data["DetailsAll"]["Details"]["MiddleName"] + + @property + def lastName(self): + return self._personal_data["DetailsAll"]["Details"]["LastName"] + + @property + def birthDate(self): + return self._personal_data["DetailsAll"]["Details"]["BirthDate"] \ No newline at end of file diff --git a/classes/exceptions.py b/classes/exceptions.py new file mode 100644 index 0000000..b792002 --- /dev/null +++ b/classes/exceptions.py @@ -0,0 +1,5 @@ +class AdonisError(Exception): + pass + +class AdonisAuthenticationDeniedError(AdonisError): + pass \ No newline at end of file diff --git a/const.py b/const.py new file mode 100644 index 0000000..a8fc4cb --- /dev/null +++ b/const.py @@ -0,0 +1 @@ +USER_AGENT = "PyAdonis / dev (https://kumi.systems)" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/settings.dist.ini b/settings.dist.ini new file mode 100644 index 0000000..b0b90e9 --- /dev/null +++ b/settings.dist.ini @@ -0,0 +1,4 @@ +[ADONIS] +CrewPortalBaseURL=http://adonis/AdonisWebServices/CrewPortalWebService.svc/ +Login=Adonis_API +Password= \ No newline at end of file diff --git a/worker.py b/worker.py new file mode 100644 index 0000000..756ed53 --- /dev/null +++ b/worker.py @@ -0,0 +1,30 @@ +from argparse import ArgumentParser + +from classes.config import Config +from classes.adonis import Adonis + + +if __name__ == "__main__": + parser = ArgumentParser() + parser.add_argument("pin", nargs="+") + args = parser.parse_args() + + config = Config("settings.ini") + api = Adonis.fromConfig(config) + + data = dict() + + for pin in args.pin: + crewdata = dict() + + crew = api.getCrew(pin) + + crewdata["firstName"] = crew.firstName + crewdata["middleName"] = crew.middleName + crewdata["lastName"] = crew.lastName + crewdata["dob"] = crew.birthDate + crewdata["password"] = crew.birthDate.replace("-","") + + data[pin] = crewdata + + print(data) \ No newline at end of file