File size: 1,350 Bytes
009c20f
 
 
 
 
a33a593
 
009c20f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a33a593
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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