Files

57 lines
1.0 KiB
Go

package core
import (
"errors"
"fmt"
"io"
"net/http"
"time"
"github.com/tidwall/gjson"
"github.com/v03413/bepusdt/app"
"github.com/v03413/bepusdt/app/conf"
"github.com/v03413/bepusdt/app/log"
)
type Api struct {
api string
}
func New() Api {
return Api{
api: conf.Api,
}
}
func (Api) get(url string) ([]byte, error) {
client := &http.Client{Timeout: 5 * time.Second}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("创建请求失败: %w", err)
}
req.Header.Set("User-Agent", fmt.Sprintf("BEpusdt/%s", app.Version))
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("请求失败: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("读取响应失败: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New(gjson.GetBytes(body, "error").String())
}
return body, nil
}
func (Api) error(err error) {
log.Warn("Api Error:", err.Error())
}