Skip to content

Commit

Permalink
Use yarl for URL building
Browse files Browse the repository at this point in the history
Signed-off-by: tr4nt0r <[email protected]>
  • Loading branch information
tr4nt0r committed Jan 7, 2025
1 parent d5ab27d commit da3069b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
37 changes: 21 additions & 16 deletions bring_api/bring.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import cast

import aiohttp
from yarl import URL

from .const import (
API_BASE_URL,
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
aiohttp~=3.11
aiohttp~=3.11
yarl~=1.18.3

0 comments on commit da3069b

Please sign in to comment.