added the data service as an external component (class)
This commit is contained in:
parent
9c33ac1df0
commit
38587c43ef
1 changed files with 65 additions and 0 deletions
65
caimira/apps/calculator/data_service.py
Normal file
65
caimira/apps/calculator/data_service.py
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
import dataclasses
|
||||
import json
|
||||
import logging
|
||||
|
||||
from tornado.web import RequestHandler
|
||||
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class DataService(RequestHandler):
|
||||
'''
|
||||
Responsible for establishing a connection to a
|
||||
database through a REST API by handling authentication
|
||||
and fetching data. It utilizes the Tornado web framework
|
||||
for asynchronous HTTP requests.
|
||||
'''
|
||||
# Credentials used for authentication
|
||||
credentials: dict
|
||||
|
||||
# Host URL for the CAiMIRA Data Service API
|
||||
host: str = 'https://caimira-data-api.app.cern.ch'
|
||||
|
||||
async def login(self):
|
||||
client_email = self.credentials["data_service_client_email"]
|
||||
client_password = self.credentials['data_service_client_password']
|
||||
|
||||
if (client_email == None or client_password == None):
|
||||
# If the credentials are not defined, an error is thrown.
|
||||
return self.send_error(500)
|
||||
|
||||
http_client = AsyncHTTPClient()
|
||||
headers = {'Content-type': 'application/json'}
|
||||
json_body = { "email": f"{client_email}", "password": f"{client_password}"}
|
||||
|
||||
try:
|
||||
response = await http_client.fetch(HTTPRequest(
|
||||
url=self.host + '/login',
|
||||
method='POST',
|
||||
headers=headers,
|
||||
body=json.dumps(json_body),
|
||||
),
|
||||
raise_error=True)
|
||||
except Exception as err:
|
||||
LOG.error("Something went wrong: %s" % err)
|
||||
self.send_error(500)
|
||||
|
||||
return json.loads(response.body)['access_token']
|
||||
|
||||
async def fetch(self, access_token: str):
|
||||
http_client = AsyncHTTPClient()
|
||||
headers = {'Authorization': f'Bearer {access_token}'}
|
||||
|
||||
try:
|
||||
response = await http_client.fetch(HTTPRequest(
|
||||
url=self.host + '/data',
|
||||
method='GET',
|
||||
headers=headers,
|
||||
),
|
||||
raise_error=True)
|
||||
except Exception as e:
|
||||
print("Something went wrong: %s" % e)
|
||||
|
||||
return json.loads(response.body)
|
||||
Loading…
Reference in a new issue