Upload 2 files
Browse files- internal/utils/req_client.go +44 -0
- internal/utils/string.go +21 -0
internal/utils/req_client.go
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package utils
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"crypto/tls"
|
| 5 |
+
"fmt"
|
| 6 |
+
"github.com/go-resty/resty/v2"
|
| 7 |
+
"time"
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
var (
|
| 11 |
+
RestySSEClient = resty.New().
|
| 12 |
+
SetTimeout(3 * time.Minute).
|
| 13 |
+
SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}).
|
| 14 |
+
SetDoNotParseResponse(true). // 告诉 Resty,不要自动读取/解析 Body,让我们自己来处理流
|
| 15 |
+
SetHeaders(map[string]string{
|
| 16 |
+
"Content-Type": "application/json",
|
| 17 |
+
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
|
| 18 |
+
"x-client-locale": "zh_CN", // 可以不传,但默认会返回英文回答
|
| 19 |
+
}).
|
| 20 |
+
OnAfterResponse(func(c *resty.Client, resp *resty.Response) error {
|
| 21 |
+
// 如果不是 200,尝试把 body 打印出来
|
| 22 |
+
if resp.StatusCode() != 200 {
|
| 23 |
+
return fmt.Errorf("monica API error: status %d, body: %s",
|
| 24 |
+
resp.StatusCode(), resp.String())
|
| 25 |
+
}
|
| 26 |
+
return nil
|
| 27 |
+
})
|
| 28 |
+
|
| 29 |
+
RestyDefaultClient = resty.New().
|
| 30 |
+
SetTimeout(time.Second * 30).
|
| 31 |
+
SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}).
|
| 32 |
+
SetHeaders(map[string]string{
|
| 33 |
+
"Content-Type": "application/json",
|
| 34 |
+
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
|
| 35 |
+
}).
|
| 36 |
+
OnAfterResponse(func(c *resty.Client, resp *resty.Response) error {
|
| 37 |
+
// 如果不是 200,尝试把 body 打印出来
|
| 38 |
+
if resp.StatusCode() != 200 {
|
| 39 |
+
return fmt.Errorf("monica API error: status %d, body: %s",
|
| 40 |
+
resp.StatusCode(), resp.String())
|
| 41 |
+
}
|
| 42 |
+
return nil
|
| 43 |
+
})
|
| 44 |
+
)
|
internal/utils/string.go
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package utils
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"math/rand"
|
| 5 |
+
"time"
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
var randSource = rand.New(rand.NewSource(time.Now().UnixNano()))
|
| 9 |
+
|
| 10 |
+
// RandStringUsingMathRand 生成指定长度的随机字符串
|
| 11 |
+
func RandStringUsingMathRand(n int) string {
|
| 12 |
+
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
| 13 |
+
|
| 14 |
+
// 创建一个长度为 n 的切片,用来存放随机字符
|
| 15 |
+
result := make([]rune, n)
|
| 16 |
+
for i := 0; i < n; i++ {
|
| 17 |
+
result[i] = letters[randSource.Intn(len(letters))]
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
return string(result)
|
| 21 |
+
}
|