Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for libraries using DDFCMS as their info endpoint #6

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[TYPECHECK]
ignored-modules=voluptuous,homeassistant,LibreView
disable=missing-docstring,too-few-public-methods,missing-module-docstring,broad-exception-caught
disable=missing-docstring,too-few-public-methods,missing-module-docstring,broad-exception-caught,line-too-long

[SIMILARITIES]
min-similarity-lines = 25
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

# DanskeBiblioteker-HomeAssistant
Install the integration either manually or via HACS by adding this repo as a custom repo
Add the G4S integration by following the config flow in settings/integrations in your Home Assistant instance
Login to your G4S smart alarm account with you username and password
Add the Danske Biblioteker integration by following the config flow in settings/integrations in your Home Assistant instance
Login to your library account with you username and password

## Spported libraries:
* Albertslund
Expand Down
52 changes: 44 additions & 8 deletions custom_components/danish_libraries/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,46 @@ async def get_info(self, identifier: str, original_object, output_type: type):
"query": INFO_GRAPG_QL_QUERY,
"variables": {"faust": identifier},
}
url = f"{INFO_BASE_URL}/fbcms-vis/graphql"
info_response = await self.session.post(
url, headers=headers, json=body, follow_redirects=True
tasks = []
url1 = f"{INFO_BASE_URL}/fbcms-vis/graphql"
url2 = f"{INFO_BASE_URL}/DDFCMS-VIS/graphql"

tasks.append(
asyncio.get_event_loop().create_task(
self.session.post(
url1, headers=headers, json=body, follow_redirects=True
)
)
)
info_response.raise_for_status()
info = info_response.json()
image_url = await self.get_image_cover(
info["data"]["manifestation"]["pid"], "pid"
tasks.append(
asyncio.get_event_loop().create_task(
self.session.post(
url2, headers=headers, json=body, follow_redirects=True
)
)
)
pid = None
info = None
results = await self.unpack_results(tasks)
for res in results:
res.raise_for_status()
info = Library.get_nested_value(res.json(), ["data", "manifestation"])
if info is None:
continue
pid = info.get("pid")
if pid is not None:
break

if pid is None:
LOGGER.error(
"Could not extract PID from object, maybe this municipality uses a new url. MUNICIPALITY=%s",
self.municipality,
)

image_url = await self.get_image_cover(pid, "pid")
return output_type(
original_object,
info["data"]["manifestation"],
info,
image_url,
)

Expand Down Expand Up @@ -314,3 +342,11 @@ async def unpack_results(self, tasks):
if ex := x.exception():
LOGGER.exception(ex)
return [x.result() for x in done]

@staticmethod
def get_nested_value(d: dict[str, any], keys: list[str]) -> any:
next_key = keys.pop(0)
value = d[next_key]
if len(keys) == 0 or value is None:
return value
return Library.get_nested_value(value, keys)
12 changes: 6 additions & 6 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
async def test_auth():
user = os.getenv("LIBRARY_USER_ID")
pin = os.getenv("LIBRARY_PIN")
lib = Library("Aalborg", user, pin)
lib = Library(os.getenv("MUNICIPALITY"), user, pin)
await lib.authenticate()
assert lib.user_bearer_token != None


async def test_loans():
user = os.getenv("LIBRARY_USER_ID")
pin = os.getenv("LIBRARY_PIN")
lib = Library("Aalborg", user, pin)
lib = Library(os.getenv("MUNICIPALITY"), user, pin)
loans = await lib.get_loans()
assert loans != None
assert len(loans) > 0
Expand All @@ -43,7 +43,7 @@ async def test_loans():
async def test_reservations():
user = os.getenv("LIBRARY_USER_ID")
pin = os.getenv("LIBRARY_PIN")
lib = Library("Aalborg", user, pin)
lib = Library(os.getenv("MUNICIPALITY"), user, pin)
reservations = await lib.get_reservations()
assert reservations != None
assert len(reservations) > 0
Expand All @@ -53,7 +53,7 @@ async def test_reservations():
async def test_profile():
user = os.getenv("LIBRARY_USER_ID")
pin = os.getenv("LIBRARY_PIN")
lib = Library("Aalborg", user, pin)
lib = Library(os.getenv("MUNICIPALITY"), user, pin)
profile = await lib.get_profile_info()
assert profile != None
assert isinstance(profile, ProfileInfo)
Expand All @@ -63,7 +63,7 @@ async def test_profile():
async def test_ereolen_loans():
user = os.getenv("LIBRARY_USER_ID")
pin = os.getenv("LIBRARY_PIN")
lib = Library("Aalborg", user, pin)
lib = Library(os.getenv("MUNICIPALITY"), user, pin)
loans = await lib.get_ereolen_loans()
assert loans != None
assert len(loans) > 0
Expand All @@ -73,7 +73,7 @@ async def test_ereolen_loans():
async def test_ereolen_reservations():
user = os.getenv("LIBRARY_USER_ID")
pin = os.getenv("LIBRARY_PIN")
lib = Library("Aalborg", user, pin)
lib = Library(os.getenv("MUNICIPALITY"), user, pin)
reservations = await lib.get_ereolen_reservations()
assert reservations != None
assert len(reservations) > 0
Expand Down
Loading