|
| 1 | +"""Centralized client for making requests to Fireworks API with consistent headers.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from typing import Any, Dict, Optional |
| 5 | + |
| 6 | +import requests |
| 7 | + |
| 8 | +from .common_utils import get_user_agent |
| 9 | + |
| 10 | + |
| 11 | +class FireworksAPIClient: |
| 12 | + """Client for making authenticated requests to Fireworks API with proper headers. |
| 13 | + |
| 14 | + This client automatically includes: |
| 15 | + - Authorization header (Bearer token) |
| 16 | + - User-Agent header for tracking eval-protocol CLI usage |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, api_key: Optional[str] = None, api_base: Optional[str] = None): |
| 20 | + """Initialize the Fireworks API client. |
| 21 | + |
| 22 | + Args: |
| 23 | + api_key: Fireworks API key. If None, will be read from environment. |
| 24 | + api_base: API base URL. If None, defaults to https://api.fireworks.ai |
| 25 | + """ |
| 26 | + self.api_key = api_key |
| 27 | + self.api_base = api_base or os.environ.get("FIREWORKS_API_BASE", "https://api.fireworks.ai") |
| 28 | + self._session = requests.Session() |
| 29 | + |
| 30 | + def _get_headers(self, content_type: Optional[str] = "application/json", |
| 31 | + additional_headers: Optional[Dict[str, str]] = None) -> Dict[str, str]: |
| 32 | + """Build headers for API requests. |
| 33 | + |
| 34 | + Args: |
| 35 | + content_type: Content-Type header value. If None, Content-Type won't be set. |
| 36 | + additional_headers: Additional headers to merge in. |
| 37 | + |
| 38 | + Returns: |
| 39 | + Dictionary of headers including authorization and user-agent. |
| 40 | + """ |
| 41 | + headers = { |
| 42 | + "User-Agent": get_user_agent(), |
| 43 | + } |
| 44 | + |
| 45 | + if self.api_key: |
| 46 | + headers["Authorization"] = f"Bearer {self.api_key}" |
| 47 | + |
| 48 | + if content_type: |
| 49 | + headers["Content-Type"] = content_type |
| 50 | + |
| 51 | + if additional_headers: |
| 52 | + headers.update(additional_headers) |
| 53 | + |
| 54 | + return headers |
| 55 | + |
| 56 | + def get(self, path: str, params: Optional[Dict[str, Any]] = None, |
| 57 | + timeout: int = 30, **kwargs) -> requests.Response: |
| 58 | + """Make a GET request to the Fireworks API. |
| 59 | + |
| 60 | + Args: |
| 61 | + path: API path (relative to api_base) |
| 62 | + params: Query parameters |
| 63 | + timeout: Request timeout in seconds |
| 64 | + **kwargs: Additional arguments passed to requests.get |
| 65 | + |
| 66 | + Returns: |
| 67 | + Response object |
| 68 | + """ |
| 69 | + url = f"{self.api_base.rstrip('/')}/{path.lstrip('/')}" |
| 70 | + headers = self._get_headers(content_type=None) |
| 71 | + if "headers" in kwargs: |
| 72 | + headers.update(kwargs.pop("headers")) |
| 73 | + return self._session.get(url, params=params, headers=headers, timeout=timeout, **kwargs) |
| 74 | + |
| 75 | + def post(self, path: str, json: Optional[Dict[str, Any]] = None, |
| 76 | + data: Optional[Any] = None, files: Optional[Dict[str, Any]] = None, |
| 77 | + timeout: int = 60, **kwargs) -> requests.Response: |
| 78 | + """Make a POST request to the Fireworks API. |
| 79 | + |
| 80 | + Args: |
| 81 | + path: API path (relative to api_base) |
| 82 | + json: JSON payload |
| 83 | + data: Form data payload |
| 84 | + files: Files to upload |
| 85 | + timeout: Request timeout in seconds |
| 86 | + **kwargs: Additional arguments passed to requests.post |
| 87 | + |
| 88 | + Returns: |
| 89 | + Response object |
| 90 | + """ |
| 91 | + url = f"{self.api_base.rstrip('/')}/{path.lstrip('/')}" |
| 92 | + |
| 93 | + # For file uploads, don't set Content-Type (let requests handle multipart/form-data) |
| 94 | + content_type = None if files else "application/json" |
| 95 | + headers = self._get_headers(content_type=content_type) |
| 96 | + |
| 97 | + if "headers" in kwargs: |
| 98 | + headers.update(kwargs.pop("headers")) |
| 99 | + |
| 100 | + return self._session.post(url, json=json, data=data, files=files, |
| 101 | + headers=headers, timeout=timeout, **kwargs) |
| 102 | + |
| 103 | + def put(self, path: str, json: Optional[Dict[str, Any]] = None, |
| 104 | + timeout: int = 60, **kwargs) -> requests.Response: |
| 105 | + """Make a PUT request to the Fireworks API.""" |
| 106 | + url = f"{self.api_base.rstrip('/')}/{path.lstrip('/')}" |
| 107 | + headers = self._get_headers() |
| 108 | + if "headers" in kwargs: |
| 109 | + headers.update(kwargs.pop("headers")) |
| 110 | + return self._session.put(url, json=json, headers=headers, timeout=timeout, **kwargs) |
| 111 | + |
| 112 | + def patch(self, path: str, json: Optional[Dict[str, Any]] = None, |
| 113 | + timeout: int = 60, **kwargs) -> requests.Response: |
| 114 | + """Make a PATCH request to the Fireworks API.""" |
| 115 | + url = f"{self.api_base.rstrip('/')}/{path.lstrip('/')}" |
| 116 | + headers = self._get_headers() |
| 117 | + if "headers" in kwargs: |
| 118 | + headers.update(kwargs.pop("headers")) |
| 119 | + return self._session.patch(url, json=json, headers=headers, timeout=timeout, **kwargs) |
| 120 | + |
| 121 | + def delete(self, path: str, timeout: int = 30, **kwargs) -> requests.Response: |
| 122 | + """Make a DELETE request to the Fireworks API.""" |
| 123 | + url = f"{self.api_base.rstrip('/')}/{path.lstrip('/')}" |
| 124 | + headers = self._get_headers(content_type=None) |
| 125 | + if "headers" in kwargs: |
| 126 | + headers.update(kwargs.pop("headers")) |
| 127 | + return self._session.delete(url, headers=headers, timeout=timeout, **kwargs) |
0 commit comments