UNPKG

dadacat-lambda-pipeline

Version:
119 lines (99 loc) 3.72 kB
/** * Client for ImageB2Uploader via API Gateway * @module B2UploadClient */ import fetch from 'cross-fetch'; /** * Client for interacting with ImageB2Uploader via API Gateway */ export class B2UploadClient { /** * Initialize with API Gateway base URL * @param {string} [baseUrl] - The API Gateway base URL */ constructor(baseUrl) { this.baseUrl = baseUrl; this.timeout = 60000; // 60 second timeout for uploads } /** * Upload image to B2 bucket * @param {Object} requestData - Upload request data * @param {string} requestData.job_id - Job ID from image generation * @param {string} requestData.run_id - Run ID from image generation * @param {string} requestData.image_url - URL of the generated image * @param {string} [requestData.bucket] - Optional bucket override * @param {string} [requestData.folder] - Optional folder override * @param {Object} [requestData.metadata] - Additional metadata * @returns {Promise<Object>} Response with upload results */ async uploadImage(requestData) { if (!this.baseUrl) { throw new Error('ImageB2Uploader API Gateway base URL not configured'); } const uploadUrl = `${this.baseUrl}/upload-b2`; console.log('Calling ImageB2Uploader via API Gateway'); console.debug('Request data:', requestData); // Validate required fields const requiredFields = ['job_id', 'run_id', 'image_url']; for (const field of requiredFields) { if (!(field in requestData)) { throw new Error(`Missing required field: ${field}`); } } try { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), this.timeout); const response = await fetch(uploadUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' }, body: JSON.stringify(requestData), signal: controller.signal }); clearTimeout(timeoutId); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const result = await response.json(); if (result.success) { const b2Url = result.data?.b2_url; console.log(`Image uploaded successfully: ${b2Url}`); } else { console.error(`Upload failed: ${JSON.stringify(result)}`); } return result; } catch (error) { if (error.name === 'AbortError') { console.error('ImageB2Uploader request timed out'); return { success: false, error: 'Request timed out' }; } console.error(`ImageB2Uploader request failed: ${error.message}`); return { success: false, error: error.message }; } } /** * Test if the lambda function is accessible * @returns {Promise<boolean>} True if accessible, false otherwise */ async testConnection() { try { // Test with minimal request (will fail validation but should reach the lambda) // We expect this to throw a validation error, which means the lambda is accessible await this.uploadImage({ test: 'connection' }); // If we get here without an error, something unexpected happened return false; } catch (error) { // Check if it's a validation error (expected) vs connection error (unexpected) const errorMsg = error.message || ''; const success = errorMsg.includes('Missing required field'); if (success) { console.log('✅ ImageB2Uploader connection test passed (validation error expected)'); } else { console.error(`Connection test failed: ${error.message}`); } return success; } } }