Add health check endpoint and refactor integration response handling
- Introduced a new public health check endpoint at `api/ping/` to verify API responsiveness. - Refactored integration response handling to utilize a unified success and error response format across various methods in `IntegrationSettingsViewSet`, improving consistency and clarity in API responses. - Updated URL patterns to include the new ping endpoint and adjusted imports accordingly.
This commit is contained in:
@@ -138,6 +138,8 @@ export default function ImageGenerationCard({
|
||||
console.log('[ImageGenerationCard] Making request to image generation endpoint');
|
||||
console.log('[ImageGenerationCard] Request body:', requestBody);
|
||||
|
||||
// fetchAPI extracts data from unified format {success: true, data: {...}}
|
||||
// So data is the extracted response payload
|
||||
const data = await fetchAPI('/v1/system/settings/integrations/image_generation/generate/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(requestBody),
|
||||
@@ -145,8 +147,10 @@ export default function ImageGenerationCard({
|
||||
|
||||
console.log('[ImageGenerationCard] Response data:', data);
|
||||
|
||||
if (!data.success) {
|
||||
throw new Error(data.error || 'Failed to generate image');
|
||||
// fetchAPI extracts data from unified format, so data is the response payload
|
||||
// If fetchAPI didn't throw, the request was successful
|
||||
if (!data || typeof data !== 'object') {
|
||||
throw new Error('Invalid response format');
|
||||
}
|
||||
|
||||
const imageData = {
|
||||
|
||||
@@ -81,38 +81,30 @@ export default function ValidationCard({
|
||||
};
|
||||
}
|
||||
|
||||
// Test endpoint returns Response({success: True, ...}) directly (not unified format)
|
||||
// So fetchAPI may or may not extract it - handle both cases
|
||||
// Test endpoint now returns unified format {success: true, data: {...}}
|
||||
// fetchAPI extracts the data field, so data is the inner object
|
||||
const data = await fetchAPI(`/v1/system/settings/integrations/${integrationId}/test/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
// Check if data has success field (direct Response format) or is extracted data
|
||||
if (data && typeof data === 'object' && ('success' in data ? data.success : true)) {
|
||||
// If data has success field, use it; otherwise assume success (extracted data)
|
||||
const isSuccess = data.success !== false;
|
||||
if (isSuccess) {
|
||||
setTestResult({
|
||||
success: true,
|
||||
message: data.message || 'API connection successful!',
|
||||
model_used: data.model_used || data.model,
|
||||
response: data.response,
|
||||
tokens_used: data.tokens_used,
|
||||
total_tokens: data.total_tokens,
|
||||
cost: data.cost,
|
||||
full_response: data.full_response || {
|
||||
image_url: data.image_url,
|
||||
provider: data.provider,
|
||||
size: data.size,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
setTestResult({
|
||||
success: false,
|
||||
message: data.error || data.message || 'API connection failed',
|
||||
});
|
||||
}
|
||||
// fetchAPI extracts data from unified format, so data is the response payload
|
||||
if (data && typeof data === 'object') {
|
||||
// Success response - data contains message, model_used, response, etc.
|
||||
setTestResult({
|
||||
success: true,
|
||||
message: data.message || 'API connection successful!',
|
||||
model_used: data.model_used || data.model,
|
||||
response: data.response,
|
||||
tokens_used: data.tokens_used,
|
||||
total_tokens: data.total_tokens,
|
||||
cost: data.cost,
|
||||
full_response: data.full_response || {
|
||||
image_url: data.image_url,
|
||||
provider: data.provider,
|
||||
size: data.size,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
setTestResult({
|
||||
success: false,
|
||||
|
||||
@@ -334,8 +334,7 @@ export default function Integration() {
|
||||
|
||||
try {
|
||||
// fetchAPI extracts data from unified format {success: true, data: {...}}
|
||||
// But test endpoint may return {success: true, ...} directly (not wrapped)
|
||||
// So data could be either the extracted data object or the full response
|
||||
// So data is the extracted response payload
|
||||
const data = await fetchAPI(`/v1/system/settings/integrations/${selectedIntegration}/test/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
@@ -344,48 +343,23 @@ export default function Integration() {
|
||||
}),
|
||||
});
|
||||
|
||||
// Handle both unified format (extracted) and direct format
|
||||
// If data has success field, it's the direct response (not extracted)
|
||||
// If data doesn't have success but has other fields, it's extracted data (successful)
|
||||
// fetchAPI extracts data from unified format, so data is the response payload
|
||||
if (data && typeof data === 'object') {
|
||||
if (data.success === true || data.success === false) {
|
||||
// Direct response format (not extracted by fetchAPI)
|
||||
if (data.success) {
|
||||
toast.success(data.message || 'API connection test successful!');
|
||||
if (data.response) {
|
||||
toast.info(`Response: ${data.response}`);
|
||||
}
|
||||
if (data.tokens_used) {
|
||||
toast.info(`Tokens used: ${data.tokens_used}`);
|
||||
}
|
||||
|
||||
// Update validation status to success
|
||||
if (selectedIntegration) {
|
||||
setValidationStatuses(prev => ({
|
||||
...prev,
|
||||
[selectedIntegration]: 'success',
|
||||
}));
|
||||
}
|
||||
} else {
|
||||
throw new Error(data.error || data.message || 'Connection test failed');
|
||||
}
|
||||
} else {
|
||||
// Extracted data format (successful response)
|
||||
toast.success('API connection test successful!');
|
||||
if (data.response) {
|
||||
toast.info(`Response: ${data.response}`);
|
||||
}
|
||||
if (data.tokens_used) {
|
||||
toast.info(`Tokens used: ${data.tokens_used}`);
|
||||
}
|
||||
|
||||
// Update validation status to success
|
||||
if (selectedIntegration) {
|
||||
setValidationStatuses(prev => ({
|
||||
...prev,
|
||||
[selectedIntegration]: 'success',
|
||||
}));
|
||||
}
|
||||
// Success response - data contains message, response, tokens_used, etc.
|
||||
toast.success(data.message || 'API connection test successful!');
|
||||
if (data.response) {
|
||||
toast.info(`Response: ${data.response}`);
|
||||
}
|
||||
if (data.tokens_used) {
|
||||
toast.info(`Tokens used: ${data.tokens_used}`);
|
||||
}
|
||||
|
||||
// Update validation status to success
|
||||
if (selectedIntegration) {
|
||||
setValidationStatuses(prev => ({
|
||||
...prev,
|
||||
[selectedIntegration]: 'success',
|
||||
}));
|
||||
}
|
||||
} else {
|
||||
throw new Error('Invalid response format');
|
||||
|
||||
Reference in New Issue
Block a user