|
import requests |
|
import pandas as pd |
|
|
|
class APIConnector: |
|
def __init__(self): |
|
self.base_url = "https://api.example.com" |
|
self.api_key = "your_api_key_here" |
|
|
|
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 |