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

Fix/add retry mechanism to billing service request handling #12006

Merged
Merged
Changes from 1 commit
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
Next Next commit
fix: add retry mechanism to billing service request handling
Signed-off-by: -LAN- <laipz8200@outlook.com>
  • Loading branch information
laipz8200 committed Dec 23, 2024
commit 7cc1b59f4a18edb576e216863488adf9d063542c
7 changes: 6 additions & 1 deletion api/services/billing_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import requests
from tenacity import retry, stop_before_delay, wait_fixed

from extensions.ext_database import db
from models.account import TenantAccountJoin, TenantAccountRole
Expand Down Expand Up @@ -38,12 +39,16 @@ def get_invoices(cls, prefilled_email: str = "", tenant_id: str = ""):
params = {"prefilled_email": prefilled_email, "tenant_id": tenant_id}
return cls._send_request("GET", "/invoices", params=params)

@retry(wait=wait_fixed(2), stop=stop_before_delay(10), reraise=True)
@classmethod
def _send_request(cls, method, endpoint, json=None, params=None):
headers = {"Content-Type": "application/json", "Billing-Api-Secret-Key": cls.secret_key}

url = f"{cls.base_url}{endpoint}"
response = requests.request(method, url, json=json, params=params, headers=headers)
try:
response = requests.request(method, url, json=json, params=params, headers=headers)
except requests.exceptions.RequestException as e:
raise ValueError(f"Failed to connect to billing service: {e}")

return response.json()

Expand Down