forked from Kamva-Academy/Kamva-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor proxies http request utils
- Loading branch information
1 parent
c17c6b3
commit 2ac062d
Showing
4 changed files
with
107 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from django.conf import settings | ||
|
||
from proxies.http_request_utils import post | ||
|
||
SHAD_LOGIN_USERNAME = settings.SHAD_LOGIN_USERNAME | ||
SHAD_LOGIN_PASSWORD = settings.SHAD_LOGIN_PASSWORD | ||
|
||
|
||
SHAD_API_URL = 'https://shadapi.noyanet.com/api/v1' | ||
|
||
|
||
def login_to_Shad(landing_id=284): | ||
|
||
url = f'{SHAD_API_URL}/account/login/' | ||
|
||
payload = { | ||
"landingId": landing_id, | ||
"username": SHAD_LOGIN_USERNAME, | ||
"password": SHAD_LOGIN_PASSWORD, | ||
} | ||
|
||
return post(url, payload) | ||
|
||
|
||
def get_user_data(token, user_uuid): | ||
url = f'{SHAD_API_URL}/ShadEvent?UserHashId={user_uuid}' | ||
|
||
headers = { | ||
"Authorization": f"Bearer {token}" | ||
} | ||
|
||
payload = {} | ||
return post(url, payload, headers=headers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import requests | ||
from requests.adapters import HTTPAdapter | ||
from urllib3.util.retry import Retry | ||
from rest_framework import status | ||
from rest_framework.response import Response | ||
|
||
|
||
# Configure retry strategy | ||
def get_retry_session(retries=5, backoff_factor=0.3, | ||
status_forcelist=(500, 502, 503, 504)): | ||
""" | ||
Create a requests session with retry capabilities. | ||
Args: | ||
retries (int): Number of total retries to allow. | ||
backoff_factor (float): A backoff factor to apply between attempts. | ||
status_forcelist (tuple): HTTP status codes to retry on. | ||
Returns: | ||
requests.Session: A configured session with retry capabilities | ||
""" | ||
retry_strategy = Retry( | ||
total=retries, | ||
status_forcelist=status_forcelist, | ||
allowed_methods=["GET", "POST"], # Only retry these methods | ||
backoff_factor=backoff_factor | ||
) | ||
adapter = HTTPAdapter(max_retries=retry_strategy) | ||
session = requests.Session() | ||
session.mount("http://", adapter) | ||
session.mount("https://", adapter) | ||
return session | ||
|
||
|
||
def get(url, params): | ||
try: | ||
session = get_retry_session() | ||
response = session.get( | ||
url, | ||
params=params, | ||
timeout=10 | ||
) | ||
response.raise_for_status() | ||
return response.json() | ||
except requests.RequestException as e: | ||
return Response( | ||
{"error": f"Failed to process GET request after retries: {str(e)}"}, | ||
status=status.HTTP_500_INTERNAL_SERVER_ERROR | ||
) | ||
|
||
|
||
def post(url, payload, headers=None): | ||
try: | ||
session = get_retry_session() | ||
response = session.post( | ||
url, | ||
json=payload, | ||
headers=headers, | ||
timeout=10 | ||
) | ||
response.raise_for_status() | ||
return response.json() | ||
except requests.RequestException as e: | ||
return Response( | ||
{"error": f"Failed to process POST request after retries: {str(e)}"}, | ||
status=status.HTTP_500_INTERNAL_SERVER_ERROR | ||
) |