hackathon / api_integration.py
Ashar086's picture
Create api_integration.py
009c20f verified
raw
history blame
1.36 kB
import requests
import pandas as pd
class APIConnector:
def __init__(self):
self.base_url = "https://api.example.com" # Replace with actual API base URL
self.api_key = "your_api_key_here" # 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
Last edited 8 minutes ago