Some changes

This commit is contained in:
Kumi 2022-06-22 11:20:34 +02:00
parent 67c06af2a8
commit e7bfb3b379
Signed by: kumi
GPG key ID: 5D1CE6AF1805ECA2
3 changed files with 32 additions and 10 deletions

View file

@ -11,18 +11,24 @@ from const import USER_AGENT
from PIL.Image import Image from PIL.Image import Image
CREW_PORTAL = 0
INTEGRATION = 1
class Adonis: class Adonis:
def __init__(self, crew_portal_base_url: str, login: str, password: str): def __init__(self, crew_portal_base_url: str, integration_base_url: str, login: str, password: str):
self.crew_portal_base_url = crew_portal_base_url self.crew_portal_base_url = crew_portal_base_url
self.integration_base_url = integration_base_url
self.login = login self.login = login
self.password = password self.password = password
@classmethod @classmethod
def fromConfig(cls, config): def fromConfig(cls, config):
return cls(config.crew_portal_base_url, config.login, config.password) return cls(config.crew_portal_base_url, config.integration_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): def request(self, endpoint: str, payload: dict, add_token: bool = True, wrap_request: bool = True, extract_response: bool = True, api: int = CREW_PORTAL):
url = urljoin(self.crew_portal_base_url, endpoint) base_url = self.crew_portal_base_url if api == CREW_PORTAL else self.integration_base_url
url = urljoin(base_url, endpoint)
req = Request(url) req = Request(url)
req.add_header("User-Agent", USER_AGENT) req.add_header("User-Agent", USER_AGENT)
@ -30,7 +36,7 @@ class Adonis:
req.add_header("Accept", "application/json") req.add_header("Accept", "application/json")
if add_token: if add_token:
payload["Authentication_Token"] = self.getTokenCrewPortal() payload["Authentication_Token"] = self.getToken(api=api)
if wrap_request: if wrap_request:
payload = {"request": payload} payload = {"request": payload}
@ -51,7 +57,14 @@ class Adonis:
return res return res
def getTokenCrewPortal(self, lifetime: int = 60):
def requestIntegration(self, endpoint: str, payload: dict, add_token: bool = True, wrap_request: bool = True, extract_response: bool = True):
return self.request(endpoint, payload, add_token, wrap_request, extract_response, INTEGRATION)
def requestCrewPortal(self, endpoint: str, payload: dict, add_token: bool = True, wrap_request: bool = True, extract_response: bool = True):
return self.request(endpoint, payload, add_token, wrap_request, extract_response, CREW_PORTAL)
def getToken(self, lifetime: int = 60, api: int = CREW_PORTAL):
data = { data = {
"credentials": { "credentials": {
"Login": self.login, "Login": self.login,
@ -60,7 +73,8 @@ class Adonis:
} }
} }
result = self.requestCrewPortal("GNL_API_AUTHENTICATION", data, False, False) method = self.requestCrewPortal if api == CREW_PORTAL else self.requestIntegration
result = method("GNL_API_AUTHENTICATION", data, False, False)
return result["Authentication_Token"] return result["Authentication_Token"]
@ -94,4 +108,7 @@ class Adonis:
if profile_picture: if profile_picture:
crew._profile_picture = self.getCrewProfilePicture(pin) crew._profile_picture = self.getCrewProfilePicture(pin)
return crew return crew
def getRequirements():
pass

View file

@ -9,10 +9,14 @@ class Config:
def crew_portal_base_url(self): def crew_portal_base_url(self):
return self.config["ADONIS"]["CrewPortalBaseURL"] return self.config["ADONIS"]["CrewPortalBaseURL"]
@property
def integration_base_url(self):
return self.config["ADONIS"]["IntegrationBaseURL"]
@property @property
def login(self): def login(self):
return self.config["ADONIS"]["Login"] return self.config["ADONIS"]["Login"]
@property @property
def password(self): def password(self):
return self.config["ADONIS"]["Password"] return self.config["ADONIS"]["Password"]

View file

@ -1,4 +1,5 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from json import dumps
from classes.config import Config from classes.config import Config
from classes.adonis import Adonis from classes.adonis import Adonis
@ -28,4 +29,4 @@ if __name__ == "__main__":
data[pin] = crewdata data[pin] = crewdata
print(data) print(dumps(data))