-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathrequestHandler.go
86 lines (73 loc) · 1.96 KB
/
requestHandler.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
81
82
83
84
85
86
package deepseek
import (
"context"
"fmt"
"net/http"
"os"
"time"
"github.com/joho/godotenv"
)
// HandleTimeout gets the timeout duration from the DEEPSEEK_TIMEOUT environment variable.
//
// (xgfone): Do we need to export the function?
func HandleTimeout() (time.Duration, error) {
return handleTimeout()
}
func handleTimeout() (time.Duration, error) {
if err := godotenv.Load(); err != nil {
_ = err
}
timeoutStr := os.Getenv("DEEPSEEK_TIMEOUT")
if timeoutStr == "" {
return 5 * time.Minute, nil
}
duration, err := time.ParseDuration(timeoutStr)
if err != nil {
return 0, fmt.Errorf("invalid timeout duration %q: %w", timeoutStr, err)
}
return duration, nil
}
func getTimeoutContext(ctx context.Context, timeout time.Duration) (
context.Context,
context.CancelFunc,
error,
) {
if timeout <= 0 {
// Try to get timeout from environment variable
var err error
timeout, err = handleTimeout()
if err != nil {
return nil, nil, fmt.Errorf("error getting timeout from environment: %w", err)
}
}
var cancel context.CancelFunc
if timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, timeout)
} else {
cancel = func() {}
}
return ctx, cancel, nil
}
// HandleSendChatCompletionRequest sends a request to the DeepSeek API and returns the response.
//
// (xgfone): Do we need to export this function?
func HandleSendChatCompletionRequest(c Client, req *http.Request) (*http.Response, error) {
return c.handleRequest(req)
}
// HandleNormalRequest sends a request to the DeepSeek API and returns the response.
//
// (xgfone): Do we need to export this function?
func HandleNormalRequest(c Client, req *http.Request) (*http.Response, error) {
return c.handleRequest(req)
}
func (c *Client) handleRequest(req *http.Request) (*http.Response, error) {
client := c.HTTPClient
if client == nil {
client = http.DefaultClient
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %w", err)
}
return resp, nil
}