Skip to content

Commit

Permalink
kasserver: Respect flood delay
Browse files Browse the repository at this point in the history
Every API call contains a field 'KasFloodDelay' in the result.
Wait this amount of seconds before attempting the next request.

Fix #9.
Supersede #10.
  • Loading branch information
fetzerch committed May 25, 2023
1 parent 0c79c17 commit 38439b3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
10 changes: 4 additions & 6 deletions kasserver/__init__.py
Expand Up @@ -41,14 +41,13 @@
class KasServer:
"""Manage domains hosted on All-Inkl.com through the KAS server API"""

FLOOD_TIMEOUT = 1

def __init__(self):
wsdl_file = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "KasApi.wsdl"
)
self._client = zeep.Client(wsdl_file)
self._get_credentials()
self._flood_timeout = 0

def _get_credentials(self):
self._username = os.environ.get("KASSERVER_USER", None)
Expand Down Expand Up @@ -76,15 +75,14 @@ def _request(self, request, params):
}

def _send_request(request):
time.sleep(self._flood_timeout)
try:
result = self._client.service.KasApi(json.dumps(request))
time.sleep(KasServer.FLOOD_TIMEOUT)
self._flood_timeout = result[1]["value"]["item"][0]["value"]
return result
except zeep.exceptions.Fault as exc:
if exc.message == "flood_protection":
timeout = (
math.ceil(float(exc.detail.text)) + KasServer.FLOOD_TIMEOUT
)
timeout = math.ceil(float(exc.detail.text))
LOGGER.warning("Hit flood protection, retrying in %ds", timeout)
time.sleep(timeout)
return _send_request(request)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_kasserver.py
Expand Up @@ -90,7 +90,7 @@ class TestKasServer:
"key": "Response",
"value": {
"item": [
{},
{"key": "KasFloodDelay", "value": 0},
{},
{
"key": "ReturnInfo",
Expand Down

0 comments on commit 38439b3

Please sign in to comment.