Skip to content

Commit

Permalink
added refreshing token
Browse files Browse the repository at this point in the history
  • Loading branch information
aabduvak committed Jul 3, 2024
1 parent 89692ff commit cfabfa0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
17 changes: 0 additions & 17 deletions api/management/commands/send_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,6 @@ def prepare_message(customer: User, payment: Payment, debt) -> str:
amount=payment.amount,
currency=currency_name,
)

if debt:
if len(debt["customers"]) == 1:
template = MessageTemplate.objects.get(name="DebtOneCurrency")
if template.is_active:
message += " " + template.message_content.format(
debt=f'{debt["customers"][0]["amount"]} {debt["customers"][0]["currency"]}'
)

elif len(debt["customers"]) == 2:
template = MessageTemplate.objects.get(name="DebtTwoCurrencies")
if template.is_active:
message += " " + template.message_content.format(
firstCurrency=f'{debt["customers"][0]["amount"]} {debt["customers"][0]["currency"]}',
secondCurrency=f'{debt["customers"][1]["amount"]} {debt["customers"][1]["currency"]}',
)

return message


Expand Down
16 changes: 14 additions & 2 deletions api/utils/eskiz.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,19 @@ def _create_token() -> str:
if response.status_code == 200:
return response.json()["data"]["token"]

send_telegram_message(response.text)
send_telegram_message(f"❌ Error while creating token:\n{response.text}")
raise NotImplementedError(response.text)


def _refresh_token(token: str) -> str:
url = f"https://{ESKIZ_URL}/auth/refresh"
headers = {"Authorization": f"Bearer {token}"}

response = requests.patch(url=url, headers=headers)
if response.status_code == 200:
return response.json()["data"]["token"]

send_telegram_message(f"❌ Error while refreshing token:\n{response.text}")
raise NotImplementedError(response.text)


Expand All @@ -34,7 +46,7 @@ def get_token() -> str:
)

if eskiz.expires_at <= now:
token = _create_token()
token = _refresh_token(eskiz.token)
eskiz.refresh_token(token)

return eskiz.token
Expand Down

0 comments on commit cfabfa0

Please sign in to comment.