Skip to content

Commit

Permalink
hacs ready
Browse files Browse the repository at this point in the history
  • Loading branch information
PTST committed Nov 3, 2024
1 parent 2932402 commit e69bed6
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 49 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file

version: 2
updates:
- package-ecosystem: "pip"
directory: "/custom_components/danish_libraries/"
schedule:
interval: "weekly"
22 changes: 22 additions & 0 deletions .github/workflows/HACS.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: HACS validation

on:
push:
branches:
- main
pull_request:
branches:
- main
schedule:
- cron: "0 0 * * *"
workflow_dispatch:

jobs:
validate-hacs:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v3"
- name: HACS validation
uses: "hacs/action@main"
with:
category: "integration"
19 changes: 19 additions & 0 deletions .github/workflows/Hassfest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Hassfest validation

on:
push:
branches:
- main
pull_request:
branches:
- main
schedule:
- cron: "0 0 * * *"
workflow_dispatch:

jobs:
hassfest:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v3"
- uses: home-assistant/actions/hassfest@master
18 changes: 18 additions & 0 deletions .github/workflows/IssueAssignment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Issue assignment

on:
issues:
types: [opened]

jobs:
auto-assign:
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- name: "Auto-assign issue"
uses: pozil/auto-assign-issue@v1
with:
assignees: ptst
allowSelfAssign: true
abortIfPreviousAssignees: true
48 changes: 48 additions & 0 deletions .github/workflows/linting.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Linting

on:
push:
branches:
- main
pull_request:
branches:
- main
schedule:
- cron: "0 0 * * *"
workflow_dispatch:


jobs:
black-formatted:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: psf/black@stable

py-linted:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Install pylint
run: pip3 install pylint
- name: Run pylint
run: pylint custom_components/danish_libraries/

imports-sorted:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Install isort
run: pip3 install isort
- name: Run isort
run: isort . --diff --check --profile black


116 changes: 67 additions & 49 deletions custom_components/danish_libraries/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ async def wrapper(*args):
await library.authenticate()
return await func(*args)
if e.response.status_code >= 500:
LOGGER.exception(e)
LOGGER.error("Unknown error, retrying in 30sec")
LOGGER.debug(e, exc_info=True)
LOGGER.debug("Unknown error, retrying in 30sec")
await asyncio.sleep(30)
return await func(*args)
raise e
except httpx.ConnectError as e:
LOGGER.exception(e)
LOGGER.error("Connect error, retrying in 30sec")
LOGGER.debug(e)
LOGGER.debug("Connect error, retrying in 30sec", exc_info=True)
await asyncio.sleep(30)
return await func(*args)
except Exception as e:
LOGGER.exception(e)
LOGGER.error("Unknown error, retrying in 30sec")
LOGGER.debug(e)
LOGGER.debug("Unknown error, retrying in 30sec", exc_info=True)
await asyncio.sleep(30)
return await func(*args)

Expand Down Expand Up @@ -76,50 +76,68 @@ def library_bearer_token(self):
return f"Bearer {self.library_token}"

async def authenticate(self):
LOGGER.debug("Authenticating")
self.session = (
create_async_httpx_client()
if not self.hass
else create_async_httpx_client(self.hass)
)
self.session.cookies.clear()
r = await self.session.get(
self.municipality.url, follow_redirects=True, timeout=None
)
r.raise_for_status()
login_page_request = await self.session.get(
f"{self.municipality.url}/login?current-path=/user/me/dashboard",
follow_redirects=True,
timeout=None,
)
login_page_request.raise_for_status()
login_page_text = login_page_request.text
login_path = re.search(r"action=\"(.*?)\"", login_page_text).group(1)
common_login_url = f"{COMMON_LOGIN_BASE_URL}{login_path}"
payload = {
"agency": self.municipality.branch_id,
"libraryName": self.municipality.name,
"loginBibDkUserId": self.user_id,
"pincode": self.pin,
}
r = await self.session.post(
common_login_url,
headers=COMMON_LOGIN_HEADERS,
data=payload,
follow_redirects=True,
timeout=None,
)
r.raise_for_status()
token_response = await self.session.get(
f"{self.municipality.url}/dpl-react/user-tokens",
follow_redirects=False,
timeout=None,
)
token_response.raise_for_status()
token_text = token_response.text
try:
LOGGER.debug("Authenticating")
self.session = (
create_async_httpx_client()
if not self.hass
else create_async_httpx_client(self.hass)
)
self.session.cookies.clear()
r = await self.session.get(
self.municipality.url, follow_redirects=True, timeout=None
)
r.raise_for_status()
login_page_request = await self.session.get(
f"{self.municipality.url}/login?current-path=/user/me/dashboard",
follow_redirects=True,
timeout=None,
)
login_page_request.raise_for_status()
login_page_text = login_page_request.text
login_path = re.search(r"action=\"(.*?)\"", login_page_text).group(1)
common_login_url = f"{COMMON_LOGIN_BASE_URL}{login_path}"
payload = {
"agency": self.municipality.branch_id,
"libraryName": self.municipality.name,
"loginBibDkUserId": self.user_id,
"pincode": self.pin,
}
r = await self.session.post(
common_login_url,
headers=COMMON_LOGIN_HEADERS,
data=payload,
follow_redirects=True,
timeout=None,
)
r.raise_for_status()
token_response = await self.session.get(
f"{self.municipality.url}/dpl-react/user-tokens",
follow_redirects=False,
timeout=None,
)
token_response.raise_for_status()
token_text = token_response.text

self.user_token = re.search(r"\"user\",\s*\"(.*?)\"", token_text).group(1)
self.library_token = re.search(r"\"library\",\s*\"(.*?)\"", token_text).group(1)
self.user_token = re.search(r"\"user\",\s*\"(.*?)\"", token_text).group(1)
self.library_token = re.search(r"\"library\",\s*\"(.*?)\"", token_text).group(1)
except httpx.HTTPStatusError as e:
if e.response.status_code >= 500:
LOGGER.debug(e, exc_info=True)
LOGGER.debug("Unknown error, retrying in 30sec")
await asyncio.sleep(30)
return await self.authenticate()
raise e
except httpx.ConnectError as e:
LOGGER.debug(e)
LOGGER.debug("Connect error, retrying in 30sec", exc_info=True)
await asyncio.sleep(30)
return await self.authenticate()
except Exception as e:
LOGGER.debug(e)
LOGGER.debug("Unknown error, retrying in 30sec", exc_info=True)
await asyncio.sleep(30)
return await self.authenticate()

@reauth_on_fail
async def get_profile_info(self) -> ProfileInfo:
Expand Down
5 changes: 5 additions & 0 deletions hacs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Danske Biblioteker",
"render_readme": true,
"hide_default_branch": true
}

0 comments on commit e69bed6

Please sign in to comment.