Skip to content

Commit

Permalink
add region_support
Browse files Browse the repository at this point in the history
  • Loading branch information
PTST committed May 30, 2024
1 parent 99f4b27 commit 0eba67f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
9 changes: 7 additions & 2 deletions LibreView/LibreView.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from typing import List
from typing import Dict, List
from uuid import UUID
from LibreView.models import Connection
from LibreView.utils import API


class LibreView:
client: API

connections_dict: Dict[UUID, Connection]

def __init__(self, username: str, password: str):
self.client = API(username, password)

def get_connections(self) -> List[Connection]:
return self.client.get_connections()
cons = self.client.get_connections()
self.connections_dict = {x.id: x for x in cons}
return cons
28 changes: 18 additions & 10 deletions LibreView/utils/API.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import Optional
import requests
from LibreView.models import User, Connection

Expand All @@ -18,14 +18,13 @@ def wrapper(*args):


class API:
username: str
password: str
base_url = "https://api.libreview.io"
client = requests.session()
product = "llu.android"
version = "4.7"

def __init__(self, username: str, password: str):
def __init__(self, username: str, password: str, region: Optional[str] = None):
self.base_url = "https://api.libreview.io"
if region:
self.base_url = f"https://api-{region}.libreview.io"
self.client = requests.session()
self.product = "llu.android"
self.version = "4.7"
self.username = username
self.password = password
self.client.headers["product"] = self.product
Expand All @@ -39,10 +38,16 @@ def authenticate(self):
"email": self.username,
"password": self.password,
},
verify=False
)
r.raise_for_status()
content = r.json()

if content and content.get("status") == 0 and content["data"].get("redirect", False):
region = content["data"]["region"]
self.base_url = f"https://api-{region}.libreview.io"
return self.authenticate()

# status 0 == login successfull
if content and content.get("status") == 0:
self.set_token(content["data"]["authTicket"]["token"])
Expand All @@ -68,6 +73,7 @@ def accept_terms(self, token):
headers={
"Authorization": f"Bearer {token}",
},
verify=False
)
r.raise_for_status()
content = r.json()
Expand All @@ -79,14 +85,16 @@ def accept_terms(self, token):
def get_user(self) -> User:
r = self.client.get(
f"{self.base_url}/user",
verify=False
)
r.raise_for_status()
return User.from_dict(r.json()["data"]["user"])

@reauth_on_fail
def get_connections(self) -> List[Connection]:
def get_connections(self) -> list[Connection]:
r = self.client.get(
f"{self.base_url}/llu/connections",
verify=False
)
r.raise_for_status()
return Connection.from_list(r.json()["data"])
3 changes: 3 additions & 0 deletions Tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ def test_connections():
assert isinstance(gm.factory_timestamp, datetime)
assert isinstance(gm.timestamp, datetime)
assert isinstance(gm.trend_message, str) or gm.trend_message is None

assert libre.connections_dict is not None
assert len(libre.connections_dict) > 0
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

setuptools.setup(
name="LibreView",
version="0.0.5",
version="0.1.2",
author="PTST",
author_email="patrick@steffensen.io",
description="API interface for LibreView / LibreLinkUp glucose readings",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/PTST/LibreView_Py",
packages=setuptools.find_packages(),
install_requires=["requests >= 2.31.0"],
install_requires=["requests >= 2.31.0", "dataclass-wizard >= 0.22.3"],
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
Expand Down

0 comments on commit 0eba67f

Please sign in to comment.