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

Retry strategy #25

Merged
merged 2 commits into from
Nov 27, 2023
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
14 changes: 14 additions & 0 deletions custom_components/alpha_innotec/api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import base64
import logging

import requests
from backports.pbkdf2 import pbkdf2_hmac
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from requests.adapters import HTTPAdapter
from urllib3 import Retry

_LOGGER = logging.getLogger(__name__)

Expand All @@ -22,6 +25,17 @@ def __init__(self, hostname: str, username: str, password: str) -> None:
self.last_request_signature: str | None = None
self.udid: str = "homeassistant"

self.session = requests.Session()
self.session.mount(
prefix='http://',
adapter=HTTPAdapter(
max_retries=Retry(
total=5,
backoff_factor=3
)
)
)

@staticmethod
def string_to_charcodes(data: str) -> str:
a = ""
Expand Down
6 changes: 3 additions & 3 deletions custom_components/alpha_innotec/controller_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def call(self, endpoint: str, data: dict = None) -> dict:

_LOGGER.debug("[%s] - body: %s", endpoint, urlencoded_body)

response = requests.post("http://{hostname}/{endpoint}".format(hostname=self.api_host, endpoint=endpoint),
response = self.session.post("http://{hostname}/{endpoint}".format(hostname=self.api_host, endpoint=endpoint),
data=urlencoded_body,
headers={'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
)
Expand All @@ -64,15 +64,15 @@ def call(self, endpoint: str, data: dict = None) -> dict:
return json_response

def login(self):
response = requests.post("http://" + self.api_host + "/api/user/token/challenge", data={
response = self.session.post("http://" + self.api_host + "/api/user/token/challenge", data={
"udid": self.udid
})

_LOGGER.debug('[api/user/token/challenge] - response body: %s', response.json())

device_token = response.json()['devicetoken']

response = requests.post("http://" + self.api_host + "/api/user/token/response", data={
response = self.session.post("http://" + self.api_host + "/api/user/token/response", data={
"login": self.username,
"token": device_token,
"udid": self.udid,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/alpha_innotec/gateway_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def call(self, endpoint: str, data: dict = None) -> dict:

_LOGGER.debug("[%s] - body: %s", endpoint, urlencoded_body)

response = requests.post("http://{hostname}/{endpoint}".format(hostname=self.api_host, endpoint=endpoint),
response = self.session.post("http://{hostname}/{endpoint}".format(hostname=self.api_host, endpoint=endpoint),
data=urlencoded_body,
headers={'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
)
Expand Down