mirror of
https://github.com/fumiama/NanoBot.git
synced 2026-06-11 13:40:26 +08:00
fix http
This commit is contained in:
@@ -1,12 +1,16 @@
|
|||||||
package nano
|
package nano
|
||||||
|
|
||||||
var (
|
const (
|
||||||
// StandardAPI 正式环境接口域名
|
// StandardAPI 正式环境接口域名
|
||||||
StandardAPI = `https://api.sgroup.qq.com`
|
StandardAPI = `https://api.sgroup.qq.com`
|
||||||
// SandboxAPI 沙箱环境接口域名
|
// SandboxAPI 沙箱环境接口域名
|
||||||
SandboxAPI = `https://sandbox.api.sgroup.qq.com`
|
SandboxAPI = `https://sandbox.api.sgroup.qq.com`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
OpenAPI = StandardAPI // OpenAPI 实际使用的 API, 可自行配置
|
||||||
|
)
|
||||||
|
|
||||||
// CodeMessageBase 各种消息都有的 code + message 基类
|
// CodeMessageBase 各种消息都有的 code + message 基类
|
||||||
type CodeMessageBase struct {
|
type CodeMessageBase struct {
|
||||||
C int `json:"code"`
|
C int `json:"code"`
|
||||||
|
|||||||
2
http.go
2
http.go
@@ -17,7 +17,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func newHTTPEndpointRequestWithAuth(method, contenttype, ep string, auth string, body io.Reader) (req *http.Request, err error) {
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import "testing"
|
|||||||
|
|
||||||
func TestWriteHTTPQueryIfNotNil(t *testing.T) {
|
func TestWriteHTTPQueryIfNotNil(t *testing.T) {
|
||||||
expstr := "https://api.sgroup.qq.com/testapi?b=1&d=0.5"
|
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 {
|
if str != expstr {
|
||||||
t.Fatal("expected", expstr, "but got", str)
|
t.Fatal("expected", expstr, "but got", str)
|
||||||
}
|
}
|
||||||
|
|||||||
10
openapi.go
10
openapi.go
@@ -35,7 +35,7 @@ func (bot *Bot) GetOpenAPI(ep, contenttype string, ptr any) error {
|
|||||||
if resp.StatusCode == http.StatusNoContent {
|
if resp.StatusCode == http.StatusNoContent {
|
||||||
return nil
|
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 errors.Wrap(errors.New("code: "+strconv.Itoa(resp.StatusCode)+", msg: "+resp.Status), getCallerFuncName())
|
||||||
}
|
}
|
||||||
if ptr == nil {
|
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 {
|
if resp.StatusCode == http.StatusNoContent {
|
||||||
return nil
|
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 errors.Wrap(errors.New("code: "+strconv.Itoa(resp.StatusCode)+", msg: "+resp.Status), getCallerFuncName())
|
||||||
}
|
}
|
||||||
if ptr == nil {
|
if ptr == nil {
|
||||||
@@ -97,7 +97,7 @@ func (bot *Bot) DeleteOpenAPI(ep, contenttype string, body io.Reader) error {
|
|||||||
if resp.StatusCode == http.StatusNoContent {
|
if resp.StatusCode == http.StatusNoContent {
|
||||||
return nil
|
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 errors.Wrap(errors.New("code: "+strconv.Itoa(resp.StatusCode)+", msg: "+resp.Status), getCallerFuncName())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -119,7 +119,7 @@ func (bot *Bot) PostOpenAPI(ep, contenttype string, ptr any, body io.Reader) err
|
|||||||
if resp.StatusCode == http.StatusNoContent {
|
if resp.StatusCode == http.StatusNoContent {
|
||||||
return nil
|
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 errors.Wrap(errors.New("code: "+strconv.Itoa(resp.StatusCode)+", msg: "+resp.Status), getCallerFuncName())
|
||||||
}
|
}
|
||||||
if ptr == nil {
|
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 {
|
if resp.StatusCode == http.StatusNoContent {
|
||||||
return nil
|
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 errors.Wrap(errors.New("code: "+strconv.Itoa(resp.StatusCode)+", msg: "+resp.Status), getCallerFuncName())
|
||||||
}
|
}
|
||||||
if ptr == nil {
|
if ptr == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user