File size: 5,722 Bytes
f119b25
 
 
d1583b8
 
08a6cc3
d1583b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108dc63
3448865
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e513bd
 
 
 
 
 
 
 
 
3448865
 
1e513bd
 
 
 
 
 
 
 
3448865
 
1e513bd
 
 
 
 
 
 
 
 
 
08a6cc3
 
 
d1583b8
 
3448865
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1583b8
3448865
d1583b8
3448865
 
 
 
f119b25
00ccbad
db4bc9c
00ccbad
db4bc9c
 
 
 
 
f119b25
db4bc9c
f119b25
db4bc9c
 
 
 
 
 
 
 
 
 
f119b25
db4bc9c
f119b25
db4bc9c
 
 
f119b25
 
db4bc9c
 
 
 
 
 
 
 
 
 
f119b25
db4bc9c
 
f119b25
db4bc9c
 
 
 
 
 
 
f119b25
 
db4bc9c
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import requests
import json


def parse_key_value_string(key_value_string):
    """Parse a key-value string into a dictionary (meant for API arguments).

    Parameters:
    - key_value_string: String with key-value pairs, one per line, separated by ':'

    Returns:
    - Dictionary of parsed key-value pairs
    """
    result = {}

    if not key_value_string:
        return result

    lines = key_value_string.strip().split("\n")
    for line in lines:
        if ":" in line:
            key, value = line.split(":", 1)
            key = key.strip()
            value = value.strip()

            if key:  # Only add non-empty keys
                # Try to parse numeric values
                if value.isdigit():
                    result[key] = int(value)
                elif value.lower() == "true":
                    result[key] = True
                elif value.lower() == "false":
                    result[key] = False
                else:
                    result[key] = value

    return result


def call_api(
    method="GET",
    base_url=None,
    endpoint=None,
    param_keys_values=None,
    header_keys_values=None,
    additional_params=None,
):
    """Make an API call to fetch data with dynamic headers and parameters.

    Parameters:
    - method: HTTP method to use (GET, POST, PUT, DELETE)
    - base_url: The base URL of the API
    - endpoint: The specific API endpoint
    - param_keys_values: Parameter key-value pairs, one per line
    - header_keys_values: Header key-value pairs, one per line
    - additional_params: Optional JSON string for complex parameters

    Examples:

    1. Simple GET request to a search API:
        method: "GET"
        base_url: "https://v2.xivapi.com/api"
        endpoint: "search"
        param_keys_values:
            query: Name~"popoto"
            sheets: Item
            fields: Name,Description
            language: en
            limit: 1

    2. GitHub API request with headers:
        method: "GET"
        base_url: "https://api.github.com"
        endpoint: "repos/microsoft/TypeScript/issues"
        param_keys_values:
            state: open
            per_page: 5
        header_keys_values:
            Accept: application/vnd.github.v3+json

    3. POST request with JSON body. the "messages" parameter is complexly structured, so it requires special handling."
        method: "POST"
        base_url: "https://api.anthropic.com"
        endpoint: "v1/messages"
        param_keys_values:
            model: claude-opus-4-20250514
            max_tokens: 1024
        header_keys_values:
            x-api-key: your_api_key_here
            content-type: application/json
        additional_params: {"messages": [{"role": "user", "content": "Hello there!"}]}
    """  
    
    # Build params and headers dictionaries from key-value pairs
    params = parse_key_value_string(param_keys_values)
    headers = parse_key_value_string(header_keys_values)

    # Handle additional parameters
    if additional_params and additional_params.strip():
        try:
            # Parse additional JSON parameters
            extra_params = json.loads(additional_params)
            if isinstance(extra_params, dict):
                params.update(extra_params)
            else:
                return "Error: Additional parameters must be a valid JSON object"
        except json.JSONDecodeError as e:
            return f"Error parsing additional parameters: {str(e)}"

    try:
        client = APIClient(base_url)
        result = client.make_request(
            endpoint=endpoint,
            params=params,
            headers=headers,
            method=method,
        )
        return result

    except Exception as e:
        return f"Error making API call: {str(e)}"


class APIClient:
    def __init__(self, base_url):
        """
        Initialize the API client with a base URL

        Parameters:
        - base_url: The base URL of the API
        """
        self.base_url = base_url.rstrip("/")

    def make_request(self, endpoint="", params=None, headers=None, method="GET"):
        """
        Make an HTTP request to the API endpoint.

        Parameters:
        - endpoint: API endpoint (without leading slash)
        - params: Dictionary of parameters to include in the request
        - headers: Dictionary of headers to include in the request
        - method: HTTP method (GET, POST, PUT, DELETE)

        Returns:
        - String representation of the API response
        """
        url = f"{self.base_url}/{endpoint.lstrip('/')}" if endpoint else self.base_url

        # Initialize headers dictionary if None
        if headers is None:
            headers = {}

        try:
            if method.upper() == "GET":
                response = requests.get(url, params=params, headers=headers)
            elif method.upper() == "POST":
                response = requests.post(url, json=params, headers=headers)
            elif method.upper() == "PUT":
                response = requests.put(url, json=params, headers=headers)
            elif method.upper() == "DELETE":
                response = requests.delete(url, json=params, headers=headers)
            else:
                return f"Unsupported method: {method}"

            # Check if the response is successful
            response.raise_for_status()

            # Try to parse JSON response
            try:
                result = response.json()
                return json.dumps(result, indent=2)
            except ValueError:
                # Return raw text if not JSON
                return response.text

        except requests.exceptions.RequestException as e:
            return f"Request error: {str(e)}"