added the data service as an external component (class)

This commit is contained in:
Luis Aleixo 2023-07-11 13:36:29 +01:00
parent 9c33ac1df0
commit 38587c43ef

View 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)