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
CREW_PORTAL = 0
INTEGRATION = 1
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.integration_base_url = integration_base_url
self.login = login
self.password = password
@classmethod
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):
url = urljoin(self.crew_portal_base_url, endpoint)
def request(self, endpoint: str, payload: dict, add_token: bool = True, wrap_request: bool = True, extract_response: bool = True, api: int = CREW_PORTAL):
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.add_header("User-Agent", USER_AGENT)
@ -30,7 +36,7 @@ class Adonis:
req.add_header("Accept", "application/json")
if add_token:
payload["Authentication_Token"] = self.getTokenCrewPortal()
payload["Authentication_Token"] = self.getToken(api=api)
if wrap_request:
payload = {"request": payload}
@ -51,7 +57,14 @@ class Adonis:
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 = {
"credentials": {
"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"]
@ -95,3 +109,6 @@ class Adonis:
crew._profile_picture = self.getCrewProfilePicture(pin)
return crew
def getRequirements():
pass

View file

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

View file

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