Skip to content

Commit

Permalink
refactor: enhance _send_request method with type hints and error hand…
Browse files Browse the repository at this point in the history
…ling for GET requests

Signed-off-by: -LAN- <laipz8200@outlook.com>
  • Loading branch information
laipz8200 committed Jan 7, 2025
1 parent 5139f41 commit d4049fd
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions api/services/billing_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from typing import Optional
from typing import Literal, Optional

import httpx
from tenacity import retry, retry_if_exception_type, stop_before_delay, wait_fixed
Expand All @@ -17,7 +17,6 @@ def get_info(cls, tenant_id: str):
params = {"tenant_id": tenant_id}

billing_info = cls._send_request("GET", "/subscription/info", params=params)

return billing_info

@classmethod
Expand Down Expand Up @@ -47,12 +46,13 @@ def get_invoices(cls, prefilled_email: str = "", tenant_id: str = ""):
retry=retry_if_exception_type(httpx.RequestError),
reraise=True,
)
def _send_request(cls, method, endpoint, json=None, params=None):
def _send_request(cls, method: Literal["GET", "POST", "DELETE"], endpoint: str, json=None, params=None):
headers = {"Content-Type": "application/json", "Billing-Api-Secret-Key": cls.secret_key}

url = f"{cls.base_url}{endpoint}"
response = httpx.request(method, url, json=json, params=params, headers=headers)

if method == "GET" and response.status_code != httpx.codes.OK:
raise ValueError("Unable to retrieve billing information. Please try again later or contact support.")
return response.json()

@staticmethod
Expand Down

0 comments on commit d4049fd

Please sign in to comment.