Create api_integration.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839import requestsimport 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 NoneLast edited 8 minutes agoapi_integration.pyAdded+39