From da3069b362049cf6b1ddc308db0377f7ea2fa226 Mon Sep 17 00:00:00 2001 From: tr4nt0r <4445816+tr4nt0r@users.noreply.github.com> Date: Mon, 6 Jan 2025 18:05:49 +0100 Subject: [PATCH] Use yarl for URL building Signed-off-by: tr4nt0r <4445816+tr4nt0r@users.noreply.github.com> --- bring_api/bring.py | 37 +++++++++++++++++++++---------------- requirements.txt | 3 ++- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/bring_api/bring.py b/bring_api/bring.py index 072afcc..e5218b9 100644 --- a/bring_api/bring.py +++ b/bring_api/bring.py @@ -11,6 +11,7 @@ from typing import cast import aiohttp +from yarl import URL from .const import ( API_BASE_URL, @@ -67,7 +68,7 @@ def __init__( self.__translations: dict[str, dict[str, str]] = {} self.uuid = "" - self.url = API_BASE_URL + self.url = URL(API_BASE_URL) self.headers = DEFAULT_HEADERS.copy() @@ -106,7 +107,7 @@ async def login(self) -> BringAuthResponse: user_data = {"email": self.mail, "password": self.password} try: - url = f"{self.url}v2/bringauth" + url = self.url / "v2/bringauth" async with self._session.post(url, data=user_data) as r: _LOGGER.debug( "Response from %s [%s]: %s", @@ -223,7 +224,7 @@ async def load_lists(self) -> BringListResponse: """ try: - url = f"{self.url}bringusers/{self.uuid}/lists" + url = self.url / "bringusers" / self.uuid / "lists" async with self._session.get(url, headers=self.headers) as r: _LOGGER.debug( "Response from %s [%s]: %s", url, r.status, await r.text() @@ -301,7 +302,7 @@ async def get_list(self, list_uuid: str) -> BringItemsResponse: """ try: - url = f"{self.url}v2/bringlists/{list_uuid}" + url = self.url / "v2/bringlists" / list_uuid async with self._session.get(url, headers=self.headers) as r: _LOGGER.debug( "Response from %s [%s]: %s", url, r.status, await r.text() @@ -400,7 +401,7 @@ async def get_all_item_details( """ try: - url = f"{self.url}bringlists/{list_uuid}/details" + url = self.url / "bringlists" / list_uuid / "details" async with self._session.get(url, headers=self.headers) as r: _LOGGER.debug( "Response from %s [%s]: %s", url, r.status, await r.text() @@ -730,7 +731,7 @@ async def notify( json_data["arguments"] = [item_name] try: - url = f"{self.url}v2/bringnotifications/lists/{list_uuid}" + url = self.url / "v2/bringnotifications/lists" / list_uuid async with self._session.post( url, headers=self.headers, json=json_data ) as r: @@ -808,11 +809,9 @@ async def does_user_exist(self, mail: str | None = None) -> bool: if not mail: raise ValueError("Argument mail missing.") - params = {"email": mail} - try: - url = f"{self.url}bringusers" - async with self._session.get(url, headers=self.headers, params=params) as r: + url = self.url / "bringusers" % {"email": mail} + async with self._session.get(url, headers=self.headers) as r: _LOGGER.debug( "Response from %s [%s]: %s", url, r.status, await r.text() ) @@ -907,7 +906,7 @@ async def __load_article_translations(self) -> dict[str, dict[str, str]]: ) try: - url = f"{LOCALES_BASE_URL}articles.{locale}.json" + url = URL(LOCALES_BASE_URL) / f"articles.{locale}.json" async with self._session.get(url) as r: _LOGGER.debug("Response from %s [%s]", url, r.status) r.raise_for_status() @@ -1061,7 +1060,7 @@ async def get_all_user_settings(self) -> BringUserSettingsResponse: """ try: - url = f"{self.url}bringusersettings/{self.uuid}" + url = self.url / "bringusersettings" / self.uuid async with self._session.get(url, headers=self.headers) as r: _LOGGER.debug( "Response from %s [%s]: %s", url, r.status, await r.text() @@ -1234,7 +1233,7 @@ async def get_user_account(self) -> BringSyncCurrentUserResponse: """ try: - url = f"{self.url}v2/bringusers/{self.uuid}" + url = self.url / "v2/bringusers" / self.uuid async with self._session.get(url, headers=self.headers) as r: _LOGGER.debug( "Response from %s [%s]: %s", url, r.status, await r.text() @@ -1357,7 +1356,7 @@ async def batch_update_list( } try: - url = f"{self.url}v2/bringlists/{list_uuid}/items" + url = self.url / "v2/bringlists" / list_uuid / "items" async with self._session.put( url, headers=self.headers, json=json_data ) as r: @@ -1431,7 +1430,7 @@ async def retrieve_new_access_token( user_data = {"grant_type": "refresh_token", "refresh_token": refresh_token} try: - url = f"{self.url}v2/bringauth/token" + url = self.url / "v2/bringauth/token" async with self._session.post( url, headers=self.headers, data=user_data ) as r: @@ -1526,7 +1525,13 @@ async def set_list_article_language( if language not in BRING_SUPPORTED_LOCALES: raise ValueError(f"Language {language} not supported.") - url = f"{self.url}bringusersettings/{self.uuid}/{list_uuid}/listArticleLanguage" + url = ( + self.url + / "bringusersettings" + / self.uuid + / list_uuid + / "listArticleLanguage" + ) data = {"value": language} try: diff --git a/requirements.txt b/requirements.txt index 519f14d..717ca2a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -aiohttp~=3.11 \ No newline at end of file +aiohttp~=3.11 +yarl~=1.18.3 \ No newline at end of file