diff --git a/define.go b/define.go index 825be93..7537fa8 100644 --- a/define.go +++ b/define.go @@ -1,12 +1,16 @@ package nano -var ( +const ( // StandardAPI 正式环境接口域名 StandardAPI = `https://api.sgroup.qq.com` // SandboxAPI 沙箱环境接口域名 SandboxAPI = `https://sandbox.api.sgroup.qq.com` ) +var ( + OpenAPI = StandardAPI // OpenAPI 实际使用的 API, 可自行配置 +) + // CodeMessageBase 各种消息都有的 code + message 基类 type CodeMessageBase struct { C int `json:"code"` diff --git a/http.go b/http.go index da08977..7f10810 100644 --- a/http.go +++ b/http.go @@ -17,7 +17,7 @@ import ( ) func newHTTPEndpointRequestWithAuth(method, contenttype, ep string, auth string, body io.Reader) (req *http.Request, err error) { - req, err = http.NewRequest(method, StandardAPI+ep, body) + req, err = http.NewRequest(method, OpenAPI+ep, body) if err != nil { return } diff --git a/http_test.go b/http_test.go index e91b9f5..18e9a23 100644 --- a/http_test.go +++ b/http_test.go @@ -4,7 +4,7 @@ import "testing" func TestWriteHTTPQueryIfNotNil(t *testing.T) { expstr := "https://api.sgroup.qq.com/testapi?b=1&d=0.5" - str := WriteHTTPQueryIfNotNil(StandardAPI+"/testapi", "a", 0, "b", 1, "c", "", "d", 0.5) + str := WriteHTTPQueryIfNotNil(OpenAPI+"/testapi", "a", 0, "b", 1, "c", "", "d", 0.5) if str != expstr { t.Fatal("expected", expstr, "but got", str) } diff --git a/openapi.go b/openapi.go index e85f9ca..2231355 100644 --- a/openapi.go +++ b/openapi.go @@ -35,7 +35,7 @@ func (bot *Bot) GetOpenAPI(ep, contenttype string, ptr any) error { if resp.StatusCode == http.StatusNoContent { return nil } - if resp.StatusCode != http.StatusOK { + if resp.StatusCode >= http.StatusBadRequest { return errors.Wrap(errors.New("code: "+strconv.Itoa(resp.StatusCode)+", msg: "+resp.Status), getCallerFuncName()) } if ptr == nil { @@ -67,7 +67,7 @@ func (bot *Bot) PutOpenAPI(ep, contenttype string, ptr any, body io.Reader) erro if resp.StatusCode == http.StatusNoContent { return nil } - if resp.StatusCode != http.StatusOK { + if resp.StatusCode >= http.StatusBadRequest { return errors.Wrap(errors.New("code: "+strconv.Itoa(resp.StatusCode)+", msg: "+resp.Status), getCallerFuncName()) } if ptr == nil { @@ -97,7 +97,7 @@ func (bot *Bot) DeleteOpenAPI(ep, contenttype string, body io.Reader) error { if resp.StatusCode == http.StatusNoContent { return nil } - if resp.StatusCode != http.StatusOK { + if resp.StatusCode >= http.StatusBadRequest { return errors.Wrap(errors.New("code: "+strconv.Itoa(resp.StatusCode)+", msg: "+resp.Status), getCallerFuncName()) } return nil @@ -119,7 +119,7 @@ func (bot *Bot) PostOpenAPI(ep, contenttype string, ptr any, body io.Reader) err if resp.StatusCode == http.StatusNoContent { return nil } - if resp.StatusCode != http.StatusOK { + if resp.StatusCode >= http.StatusBadRequest { return errors.Wrap(errors.New("code: "+strconv.Itoa(resp.StatusCode)+", msg: "+resp.Status), getCallerFuncName()) } if ptr == nil { @@ -151,7 +151,7 @@ func (bot *Bot) PatchOpenAPI(ep, contenttype string, ptr any, body io.Reader) er if resp.StatusCode == http.StatusNoContent { return nil } - if resp.StatusCode != http.StatusOK { + if resp.StatusCode >= http.StatusBadRequest { return errors.Wrap(errors.New("code: "+strconv.Itoa(resp.StatusCode)+", msg: "+resp.Status), getCallerFuncName()) } if ptr == nil {