BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr
Fix the issue of inaccurate testing of apiKey
hwliange9372e712c1c
diff --git a/core/internal/service/askai/supplier.go b/core/internal/service/askai/supplier.goindex ca45fbd3..fd418c61 100644--- a/core/internal/service/askai/supplier.go+++ b/core/internal/service/askai/supplier.go@@ -6,8 +6,11 @@ import ("encoding/json""errors""fmt"+ "net/http"+ "net/url""os""sort"+ "time""github.com/gogf/gf/v2/frame/g")@@ -390,21 +393,83 @@ func SetSupplierConfig(supplierName string, baseUrl string, apiKey string) errorreturn SaveSupplierConfig(supplierName, *supplierConfig)}-// Test whether the model supplier baseURL and apiKey are available-// Test whether the baseURL can be accessed normally-// Test whether the apiKey is correct-func Testing(applierName string, baseUrl string, apiKey string) error {- if applierName == "" || baseUrl == "" || apiKey == "" {- return errors.New("applier name, base URL, and API key are required")+// Testing validates the supplier's configuration by checking the base URL and API key.+// It performs the following checks:+// 1. Validates the base URL format.+// 2. Tests the accessibility of the base URL by sending a HEAD request.+// 3. Validates the API key by sending a GET request to the models endpoint.+// If any of these checks fail, it returns an error indicating the issue.+func Testing(supplierName, baseUrl, apiKey string) error {+ if supplierName == "" || baseUrl == "" || apiKey == "" {+ return errors.New("supplier name, base URL, and API key are required")}- // fmt.Printf("Testing supplier: %s\n", applierName)- // fmt.Printf("Base URL: %s\n", baseUrl)- // fmt.Printf("API Key: %s\n", apiKey)- // Here you would implement the actual testing logic, such as making a request to the supplier+ // Validate base URL format+ parsedUrl, err := url.ParseRequestURI(baseUrl)+ if err != nil {+ return fmt.Errorf("invalid base URL format: %w", err)+ }+ if parsedUrl.Scheme != "http" && parsedUrl.Scheme != "https" {+ return errors.New("base URL must use http or https protocol")+ }+ // Ensure the base URL ends with a slash+ if err := testBaseURLAccessibility(baseUrl); err != nil {+ return fmt.Errorf("base URL accessibility test failed: %w", err)+ }+ // Validate API key by making a request to the models endpoint+ if err := testAPIKeyValidity(baseUrl, apiKey); err != nil {+ return fmt.Errorf("API key validation failed: %w", err)+ }++ return nil+}++// testBaseURLAccessibility checks if the base URL is accessible by sending a HEAD request.+// It returns an error if the request fails or if the response status is not OK (200).+func testBaseURLAccessibility(baseUrl string) error {+ client := &http.Client{Timeout: 3 * time.Second}+ resp, err := client.Head(baseUrl)+ if err != nil {+ return err+ }+ defer resp.Body.Close()return nil}+// testAPIKeyValidity checks if the provided API key is valid by making a GET request to the models endpoint.+// It constructs the API URL using the base URL and sends a request with the API key in the Authorization header.+// It returns an error if the request fails, if the response status is unauthorized (401),+// or if the response status is not OK (200) or not found (404).+func testAPIKeyValidity(baseUrl, apiKey string) error {+ apiUrl := baseUrl + "/models"+ fmt.Println("Testing API key validity at:", apiUrl)+ client := &http.Client{Timeout: 15 * time.Second}+ req, err := http.NewRequest("GET", apiUrl, nil)+ if err != nil {+ return err+ }+ req.Header.Set("Authorization", "Bearer "+apiKey)+ req.Header.Set("Content-Type", "application/json")++ resp, err := client.Do(req)+ if err != nil {+ return err+ }+ defer resp.Body.Close()++ switch resp.StatusCode {+ case http.StatusOK:+ return nil // API key is successfully validated+ case http.StatusUnauthorized:+ return errors.New("invalid API key")+ case http.StatusNotFound:+ return nil+ default:+ return fmt.Errorf("unexpected API response: %d %s",+ resp.StatusCode, http.StatusText(resp.StatusCode))+ }+}+// GetSupplierConfig retrieves the configuration for a specific supplier by its name.// It reads the configuration file and returns a Supplier struct containing the supplier's details.// If the configuration file does not exist or an error occurs, it returns an error.