52 lines
961 B
Go
52 lines
961 B
Go
package service
|
|
|
|
import (
|
|
"ai-search/service/logger"
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/imroc/req/v3"
|
|
)
|
|
|
|
type SearchClient interface {
|
|
Search(c context.Context, query string, nums int) (SearchResp, error)
|
|
}
|
|
|
|
type searchClient struct {
|
|
URL string
|
|
}
|
|
|
|
type SearchResp struct {
|
|
Results []SearchResult `json:"results"`
|
|
}
|
|
|
|
type SearchResult struct {
|
|
Body string `json:"body"`
|
|
Href string `json:"href"`
|
|
Title string `json:"title"`
|
|
}
|
|
|
|
func NewSearchClient() SearchClient {
|
|
return &searchClient{
|
|
URL: GetSettings().RPCEndpoints.SearchURL,
|
|
}
|
|
}
|
|
|
|
func (s *searchClient) Search(c context.Context, query string, nums int) (SearchResp, error) {
|
|
resp := SearchResp{}
|
|
_, err := req.C().R().
|
|
SetFormData(map[string]string{
|
|
"q": query,
|
|
"max_results": fmt.Sprintf("%d", nums),
|
|
}).
|
|
SetContentType("application/x-www-form-urlencoded").
|
|
SetSuccessResult(&resp).
|
|
Post(s.URL)
|
|
|
|
if err != nil {
|
|
logger.Logger(c).Error(err)
|
|
}
|
|
|
|
return resp, err
|
|
}
|