Skip to content

Commit

Permalink
Hl 1070 token refresh (#2786)
Browse files Browse the repository at this point in the history
* feat: run refresh_ahjo_token_hourly

* fix: error handling and crash of refresh_token
  • Loading branch information
rikuke authored Feb 7, 2024
1 parent a99de19 commit ccf953a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
Empty file.
13 changes: 13 additions & 0 deletions backend/benefit/applications/jobs/hourly/hourly_ahjo_job.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.core.management import call_command
from django_extensions.management.jobs import HourlyJob

"""
A hourly job to refresh the Ahjo access token via the refresh token saved in the Django database.
"""


class Job(HourlyJob):
help = "Hourly Ahjo refresh jobs are executed here."

def execute(self):
call_command("refresh_ahjo_token")
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def handle(self, *args, **options):
return
try:
token = ahjo_connector.refresh_token()
self.stdout.write(
f"Ahjo token refreshed, token valid until {token.expires_in}"
)
except Exception as e:
self.stdout.write(f"Failed to refresh Ahjo token: {e}")
self.stdout.write(f"Ahjo token refreshed, token valid until {token.expires_in}")
return
9 changes: 6 additions & 3 deletions backend/benefit/applications/services/ahjo_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ def refresh_token(self) -> AhjoToken:

def do_token_request(self, payload: Dict[str, str]) -> AhjoToken:
# Make the POST request
response = requests.post(
self.token_url, headers=self.headers, data=payload, timeout=self.timeout
)
try:
response = requests.post(
self.token_url, headers=self.headers, data=payload, timeout=self.timeout
)
except requests.exceptions.RequestException as e:
raise Exception(f"Failed to get or refresh token from Ahjo: {e}")

# Check if the request was successful
if response.status_code == 200:
Expand Down

0 comments on commit ccf953a

Please sign in to comment.