package inner import ( "encoding/json" "fmt" "io" "net/http" "net/url" "strings" "time" ) func main(clientID, databaseType string, supabaseMain map[string]interface{}) (map[string]interface{}, error) { // Extract Supabase configuration supabaseURL, ok := supabaseMain["url"].(string) if !ok { return nil, fmt.Errorf("missing or invalid supabase URL") } supabaseKey, ok := supabaseMain["key"].(string) if !ok { return nil, fmt.Errorf("missing or invalid supabase key") } // Trim whitespace from inputs clientID = strings.TrimSpace(clientID) databaseType = strings.TrimSpace(databaseType) // Build query URL queryURL := fmt.Sprintf("%s/rest/v1/credential_resolution_config?client_id=eq.%s&database_type=eq.%s&is_active=eq.true&order=priority.asc&limit=1", supabaseURL, url.QueryEscape(clientID), url.QueryEscape(databaseType)) // Create HTTP client client := &http.Client{ Timeout: 30 * time.Second, } // Create request req, err := http.NewRequest("GET", queryURL, nil) if err != nil { return buildErrorResponse(clientID, databaseType, fmt.Sprintf("failed to create request: %v", err)), nil } // Set headers req.Header.Set("apikey", supabaseKey) req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", supabaseKey)) req.Header.Set("Content-Type", "application/json") // Execute request resp, err := client.Do(req) if err != nil { return buildErrorResponse(clientID, databaseType, fmt.Sprintf("request failed: %v", err)), nil } defer resp.Body.Close() // Check response status if resp.StatusCode != http.StatusOK { body, _ := io.ReadAll(resp.Body) return buildErrorResponse(clientID, databaseType, fmt.Sprintf("Supabase API error: %d %s - %s", resp.StatusCode, resp.Status, string(body))), nil } // Parse response var results []map[string]interface{} if err := json.NewDecoder(resp.Body).Decode(&results); err != nil { return buildErrorResponse(clientID, databaseType, fmt.Sprintf("failed to parse response: %v", err)), nil } // Check if we found any results if len(results) == 0 { return buildErrorResponse(clientID, databaseType, fmt.Sprintf("No active credential configuration found for client: %s, database_type: %s", clientID, databaseType)), nil } // Return successful response config := results[0] return map[string]interface{}{ "success": true, "client_id": clientID, "database_type": databaseType, "credential_config": config, "resolved_at": time.Now().UTC().Format(time.RFC3339), "message": fmt.Sprintf("Found %s configuration for %s", config["vendor"], clientID), "api_method": "supabase_rest_go", }, nil } func buildErrorResponse(clientID, databaseType, message string) map[string]interface{} { return map[string]interface{}{ "success": false, "client_id": clientID, "database_type": databaseType, "error": map[string]interface{}{ "code": "CREDENTIAL_RESOLUTION_FAILED", "message": message, "category": "credentials", "recoverable": false, }, "resolved_at": time.Now().UTC().Format(time.RFC3339), "api_method": "supabase_rest_go", } }