Create api_integration.py
Browse files- api_integration.py +39 -0
api_integration.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
class APIConnector:
|
| 5 |
+
def __init__(self):
|
| 6 |
+
self.base_url = "https://api.example.com" # Replace with actual API base URL
|
| 7 |
+
self.api_key = "your_api_key_here" # Replace with actual API key
|
| 8 |
+
|
| 9 |
+
def fetch_data(self, endpoint, params=None):
|
| 10 |
+
url = f"{self.base_url}/{endpoint}"
|
| 11 |
+
headers = {
|
| 12 |
+
"Authorization": f"Bearer {self.api_key}",
|
| 13 |
+
"Content-Type": "application/json"
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
response = requests.get(url, headers=headers, params=params)
|
| 18 |
+
response.raise_for_status()
|
| 19 |
+
data = response.json()
|
| 20 |
+
return pd.DataFrame(data)
|
| 21 |
+
except requests.exceptions.RequestException as e:
|
| 22 |
+
print(f"Error fetching data from API: {e}")
|
| 23 |
+
return None
|
| 24 |
+
|
| 25 |
+
def post_data(self, endpoint, data):
|
| 26 |
+
url = f"{self.base_url}/{endpoint}"
|
| 27 |
+
headers = {
|
| 28 |
+
"Authorization": f"Bearer {self.api_key}",
|
| 29 |
+
"Content-Type": "application/json"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
response = requests.post(url, headers=headers, json=data)
|
| 34 |
+
response.raise_for_status()
|
| 35 |
+
return response.json()
|
| 36 |
+
except requests.exceptions.RequestException as e:
|
| 37 |
+
print(f"Error posting data to API: {e}")
|
| 38 |
+
return None
|
| 39 |
+
Last edited 8 minutes ago
|