49 lines
1019 B
Go
49 lines
1019 B
Go
package biliapi
|
||
|
||
import (
|
||
"io"
|
||
"net/http"
|
||
"time"
|
||
)
|
||
|
||
type Root struct {
|
||
Code int `json:"code"`
|
||
Message string `json:"message"`
|
||
TTL int `json:"ttl"`
|
||
Data interface{} `json:"data"`
|
||
}
|
||
|
||
type BaseService struct {
|
||
client *http.Client
|
||
url string
|
||
}
|
||
|
||
func (srv *BaseService) DoRequest() ([]byte, error) {
|
||
req, err := http.NewRequest("GET", srv.url, nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.46")
|
||
|
||
if srv.client == nil {
|
||
srv.client = &http.Client{
|
||
Timeout: time.Duration(5) * time.Second,
|
||
}
|
||
}
|
||
resp, err := srv.client.Do(req)
|
||
defer resp.Body.Close()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
body, err := io.ReadAll(resp.Body)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
//if resp.StatusCode != 200 {
|
||
// return body, NewAPIError(fmt.Sprintf("非正常返回值:StatusCode %d", resp.StatusCode))
|
||
//}
|
||
|
||
return body, nil
|
||
}
|