-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock.go
80 lines (67 loc) · 1.67 KB
/
mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package tavily
import (
"encoding/json"
"fmt"
"math/rand"
"net/http"
"time"
)
type SearchHandler struct{}
func (h *SearchHandler) search(data *TavilyRequest) (TavilyResponse, error) {
resp := TavilyResponse{
Query: data.Query,
}
if data.IncludeAnswer {
resp.Answer = "A valid answer"
}
for i := uint32(0); i < data.MaxResults; i++ {
t := TavilyResult{
Title: "A nice title",
URL: "https://github.com/lwileczek/tavily",
Content: "Some modified page content",
RawContent: nil,
Score: rand.Float64(),
}
if data.IncludeRawContent {
raw := "<div>Some modified page content</div>"
t.RawContent = &raw
}
}
return resp, nil
}
func (h *SearchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, "Only POST requests are allowed")
return
}
defer r.Body.Close()
var request TavilyRequest
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&request)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Invalid JSON request: %v", err)
return
}
if request.ApiKey == "" {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "Invalid API key")
return
}
startTime := time.Now()
response, err := h.search(&request)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Error processing search: %v", err)
return
}
response.ResponseTime = time.Since(startTime).Seconds()
w.Header().Set("Content-Type", "application/json")
encoder := json.NewEncoder(w)
err = encoder.Encode(response)
if err != nil {
fmt.Printf("Error encoding response: %v", err)
return
}
}