ISM2023w / request_api.py
debayan's picture
Upload folder using huggingface_hub
a7539f2
raw
history blame
2.03 kB
# Generic code to make api requests using python requests library: frame
import requests
from urllib.parse import urljoin, urlencode
# Define the base URL
#BASE_URL = 'http://127.0.0.1:5000/'
import httpx
BASE_URL = 'https://d9c5-134-28-45-21.ngrok-free.app/'
async def make_get_request(endpoint, params):
async with httpx.AsyncClient() as client:
try:
response = await client.get(urljoin(BASE_URL, endpoint), params=params)
print(response.url)
response.raise_for_status() # Check if the response is successful
except httpx.HTTPError as e:
print(f"Request failed: {e}")
return None
print(response)
return response
async def make_post_request(endpoint, params, data=None):
if data is None:
return None
async with httpx.AsyncClient() as client:
try:
response = await client.post(urljoin(BASE_URL, endpoint), params=params, data=data)
response.raise_for_status() # Check if the response is successful
except httpx.HTTPError as e:
print(f"Request failed: {e}")
return None
return response
"""
def make_get_request(endpoint,params):
try:
# Make a GET request to the endpoint
response = requests.get(
url = f'{BASE_URL}{endpoint}',
params=params,
verify=True
)
print(response.url)
# Check if the response is successful
except requests.RequestException as e:
print(f"Request failed: {e}")
return None
return response
# Make a POST request to the endpoint
def make_post_request(endpoint,params,data=None):
if data is None:
return
# Make a POST request to the endpoint
response = requests.post(
url=BASE_URL + endpoint,
params=params,
data=data,
verify=True
)
# Check if the response is successful
# Return the error
return response
"""