import logging import requests class LibreViewSession: def __init__(self): self.authenticated = False self.token = "" self.connections = [] def useToken(self, token): url = "https://api.libreview.io/user" self.token = token headers = { "version": "4.7", "product": "llu.ios", "Accept": "application/json", "Authorization": "Bearer " + self.token } response = requests.get(url, headers=headers) if response.status_code != 200: logging.debug(f"got status code {response.status_code} when attempting to test token") self.authenticated = False else: self.authenticated = True def authenticate(self, email, password): url = "https://api.libreview.io/llu/auth/login" if self.authenticated: return headers = { "version": "4.7", "product": "llu.ios", "Content-Type": "application/json", "Accept": "application/json" } payload = { "email": email, "password": password, } response = requests.post(url, json=payload, headers=headers) if response.status_code == 200: json = response.json() if 'status' in json: if json['status'] == 0 and 'data' in json: data = json['data'] auth_ticket = data['authTicket'] self.token = auth_ticket['token'] self.authenticated = True else: self.authenticated = False logging.debug(f"failed to authenticate with libreview, got status {json['status']}") return def getConnections(self): url = "https://api.libreview.io/llu/connections" headers = { "version": "4.7", "product": "llu.ios", "Accept": "application/json", "Authorization": "Bearer " + self.token } response = requests.get(url, headers=headers) if response.status_code != 200: logging.debug(f"got http response code {response.status_code} from libreview when attempting to get connections") return [] json = response.json() if 'data' in json: self.connections = json['data'] else: print(response.content) self.connections = [] def getGraph(self, patientId): """ getGraph gets the LibreView graph for the {patientID} """ url = f"https://api.libreview.io/llu/connections/{patientId}/graph" headers = { "version": "4.7", "product": "llu.ios", "Accept": "application/json", "Authorization": "Bearer " + self.token } response = requests.get(url, headers=headers) json = response.json() if 'data' in json: return json['data'] else: return None