support If-None-Match

This commit is contained in:
yuzuki999 2023-06-25 07:49:51 +08:00
parent 163cd2a6dc
commit 9af5d1a6e7
3 changed files with 7 additions and 3 deletions

View File

@ -68,11 +68,14 @@ type NodeInfo struct {
func (c *Client) GetNodeInfo() (node *NodeInfo, err error) { func (c *Client) GetNodeInfo() (node *NodeInfo, err error) {
const path = "/api/v1/server/UniProxy/config" const path = "/api/v1/server/UniProxy/config"
r, err := c.client.R().Get(path) r, err := c.client.
R().
SetHeader("If-None-Match", c.etag).
Get(path)
if err = c.checkResponse(r, path, err); err != nil { if err = c.checkResponse(r, path, err); err != nil {
return return
} }
if c.etag == r.Header().Get("ETag") { // node info not changed if r.StatusCode() == 304 {
return nil, nil return nil, nil
} }
// parse common params // parse common params

View File

@ -23,6 +23,7 @@ func init() {
func TestClient_GetNodeInfo(t *testing.T) { func TestClient_GetNodeInfo(t *testing.T) {
log.Println(client.GetNodeInfo()) log.Println(client.GetNodeInfo())
log.Println(client.GetNodeInfo())
} }
func TestClient_ReportUserTraffic(t *testing.T) { func TestClient_ReportUserTraffic(t *testing.T) {

View File

@ -18,7 +18,7 @@ func (c *Client) checkResponse(res *resty.Response, path string, err error) erro
if err != nil { if err != nil {
return fmt.Errorf("request %s failed: %s", c.assembleURL(path), err) return fmt.Errorf("request %s failed: %s", c.assembleURL(path), err)
} }
if res.StatusCode() != 200 { if res.StatusCode() >= 400 {
body := res.Body() body := res.Body()
return fmt.Errorf("request %s failed: %s", c.assembleURL(path), string(body)) return fmt.Errorf("request %s failed: %s", c.assembleURL(path), string(body))
} }