feat(hook): Add hook module to help decouple some specific logic (#784)

This commit is contained in:
Shui
2025-11-08 20:02:30 -05:00
committed by GitHub
parent 52b0bfe1d2
commit 631fb62d21
12 changed files with 466 additions and 35 deletions

View File

@@ -6,6 +6,7 @@ import (
"io"
"log"
"net/http"
"nofx/hook"
"strconv"
"time"
)
@@ -19,10 +20,18 @@ type APIClient struct {
}
func NewAPIClient() *APIClient {
client := &http.Client{
Timeout: 30 * time.Second,
}
hookRes := hook.HookExec[hook.SetHttpClientResult](hook.SET_HTTP_CLIENT, client)
if hookRes != nil && hookRes.Error() == nil {
log.Printf("使用Hook设置的HTTP客户端")
client = hookRes.GetResult()
}
return &APIClient{
client: &http.Client{
Timeout: 30 * time.Second,
},
client: client,
}
}
@@ -74,6 +83,7 @@ func (c *APIClient) GetKlines(symbol, interval string, limit int) ([]Kline, erro
var klineResponses []KlineResponse
err = json.Unmarshal(body, &klineResponses)
if err != nil {
log.Printf("获取K线数据失败,响应内容: %s", string(body))
return nil, err
}

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"io/ioutil"
"math"
"net/http"
"strconv"
"strings"
"sync"
@@ -315,7 +314,8 @@ func calculateLongerTermData(klines []Kline) *LongerTermData {
func getOpenInterestData(symbol string) (*OIData, error) {
url := fmt.Sprintf("https://fapi.binance.com/fapi/v1/openInterest?symbol=%s", symbol)
resp, err := http.Get(url)
apiClient := NewAPIClient()
resp, err := apiClient.client.Get(url)
if err != nil {
return nil, err
}
@@ -359,7 +359,8 @@ func getFundingRate(symbol string) (float64, error) {
// 缓存过期或不存在,调用 API
url := fmt.Sprintf("https://fapi.binance.com/fapi/v1/premiumIndex?symbol=%s", symbol)
resp, err := http.Get(url)
apiClient := NewAPIClient()
resp, err := apiClient.client.Get(url)
if err != nil {
return 0, err
}