Sim API client for executing workflows programmatically. Args: api_key: Your Sim API key base_url: Base URL for the Sim API (defaults to https://sim.ai)
| 83 | |
| 84 | |
| 85 | class SimStudioClient: |
| 86 | """ |
| 87 | Sim API client for executing workflows programmatically. |
| 88 | |
| 89 | Args: |
| 90 | api_key: Your Sim API key |
| 91 | base_url: Base URL for the Sim API (defaults to https://sim.ai) |
| 92 | """ |
| 93 | |
| 94 | def __init__(self, api_key: str, base_url: str = "https://sim.ai"): |
| 95 | self.api_key = api_key |
| 96 | self.base_url = base_url.rstrip('/') |
| 97 | self._session = requests.Session() |
| 98 | self._session.headers.update({ |
| 99 | 'X-API-Key': self.api_key, |
| 100 | 'Content-Type': 'application/json', |
| 101 | }) |
| 102 | self._rate_limit_info: Optional[RateLimitInfo] = None |
| 103 | |
| 104 | def _convert_files_to_base64(self, value: Any) -> Any: |
| 105 | """ |
| 106 | Convert file objects in input to API format (base64). |
| 107 | Recursively processes nested dicts and lists. |
| 108 | """ |
| 109 | import base64 |
| 110 | |
| 111 | # Check if this is a file-like object |
| 112 | if hasattr(value, 'read') and callable(value.read): |
| 113 | # Save current position if seekable |
| 114 | initial_pos = value.tell() if hasattr(value, 'tell') else None |
| 115 | |
| 116 | # Read file bytes |
| 117 | file_bytes = value.read() |
| 118 | |
| 119 | # Restore position if seekable |
| 120 | if initial_pos is not None and hasattr(value, 'seek'): |
| 121 | value.seek(initial_pos) |
| 122 | |
| 123 | # Encode to base64 |
| 124 | base64_data = base64.b64encode(file_bytes).decode('utf-8') |
| 125 | |
| 126 | # Get file metadata |
| 127 | filename = getattr(value, 'name', 'file') |
| 128 | if isinstance(filename, str): |
| 129 | filename = os.path.basename(filename) |
| 130 | |
| 131 | content_type = getattr(value, 'content_type', 'application/octet-stream') |
| 132 | |
| 133 | return { |
| 134 | 'type': 'file', |
| 135 | 'data': f'data:{content_type};base64,{base64_data}', |
| 136 | 'name': filename, |
| 137 | 'mime': content_type |
| 138 | } |
| 139 | |
| 140 | # Recursively process lists |
| 141 | if isinstance(value, list): |
| 142 | return [self._convert_files_to_base64(item) for item in value] |
no outgoing calls