From 7bc537b401436edff868777f36c45661183c9115 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Fri, 6 Jan 2023 00:07:34 +0100 Subject: ... --- overlays/worktime/default.nix | 2 +- overlays/worktime/worktime.py | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) (limited to 'overlays') diff --git a/overlays/worktime/default.nix b/overlays/worktime/default.nix index 20c0b90f..699e00c0 100644 --- a/overlays/worktime/default.nix +++ b/overlays/worktime/default.nix @@ -5,7 +5,7 @@ phases = [ "buildPhase" "checkPhase" "installPhase" ]; - python = prev.python39.withPackages (ps: with ps; [pyxdg python-dateutil uritools requests configparser tabulate]); + python = prev.python39.withPackages (ps: with ps; [pyxdg python-dateutil uritools requests configparser tabulate backoff]); buildInputs = [ python ]; buildPhase = '' diff --git a/overlays/worktime/worktime.py b/overlays/worktime/worktime.py index 166bea1c..46197b6e 100755 --- a/overlays/worktime/worktime.py +++ b/overlays/worktime/worktime.py @@ -30,6 +30,9 @@ from tabulate import tabulate from itertools import groupby from functools import cache +import backoff + + class TogglAPISection(Enum): TOGGL = '/api/v8' REPORTS = '/reports/api/v2' @@ -62,7 +65,17 @@ class TogglAPI(object): return uri def _query(self, url, method): + response = self._raw_query(url, method) + response.raise_for_status() + return response + @backoff.on_predicate( + backoff.expo, + factor=0.1, max_value=2, + predicate=lambda r: r.status_code == 429, + max_time=10, + ) + def _raw_query(self, url, method): headers = {'content-type': 'application/json'} response = None @@ -73,8 +86,6 @@ class TogglAPI(object): else: raise ValueError(f"Undefined HTTP method “{method}”") - response.raise_for_status() - return response def get_billable_hours(self, start_date, end_date=datetime.now(timezone.utc), rounding=False): -- cgit v1.2.3