(endpoint, payload, key, onRequestId, maxAttempts = 60)
| 40 | } |
| 41 | |
| 42 | async function submitAndPoll(endpoint, payload, key, onRequestId, maxAttempts = 60) { |
| 43 | const url = `${BASE_URL}/api/v1/${endpoint}`; |
| 44 | const response = await fetch(url, { |
| 45 | method: 'POST', |
| 46 | headers: { 'Content-Type': 'application/json', 'x-api-key': key }, |
| 47 | body: JSON.stringify(payload) |
| 48 | }); |
| 49 | if (!response.ok) { |
| 50 | const errText = await response.text(); |
| 51 | notifyAuthRequired(response.status, errText); |
| 52 | throw new Error(`API Request Failed: ${response.status} ${response.statusText} - ${errText.slice(0, 100)}`); |
| 53 | } |
| 54 | const submitData = await response.json(); |
| 55 | const requestId = submitData.request_id || submitData.id; |
| 56 | if (!requestId) return submitData; |
| 57 | if (onRequestId) onRequestId(requestId); |
| 58 | const result = await pollForResult(requestId, key, maxAttempts); |
| 59 | const outputUrl = result.outputs?.[0] || result.url || result.output?.url; |
| 60 | return { ...result, url: outputUrl }; |
| 61 | } |
| 62 | |
| 63 | export async function generateImage(apiKey, params) { |
| 64 | const modelInfo = getModelById(params.model); |
no test coverage detected