hackathon / api_integration.py
Ashar086's picture
Update api_integration.py
a33a593 verified
import requests
import pandas as pd
class APIConnector:
def __init__(self):
self.base_url = "https://api.aimlapi.com/v1" # Replace with actual API base URL
self.api_key = "c496d9094ba54ddb9d66eeeb35a6196f" # Replace with actual API key
def fetch_data(self, endpoint, params=None):
url = f"{self.base_url}/{endpoint}"
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
try:
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
data = response.json()
return pd.DataFrame(data)
except requests.exceptions.RequestException as e:
print(f"Error fetching data from API: {e}")
return None
def post_data(self, endpoint, data):
url = f"{self.base_url}/{endpoint}"
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"Error posting data to API: {e}")
return None