1
0
mirror of https://github.com/fumiama/NanoBot.git synced 2026-06-08 20:20:23 +08:00

add channel patch/delete

This commit is contained in:
源文雨
2023-10-10 22:02:52 +09:00
parent f04743c7a5
commit 30554f4ffc
9 changed files with 268 additions and 51 deletions

24
http.go
View File

@@ -21,6 +21,16 @@ func NewHTTPEndpointGetRequestWithAuth(ep string, auth string) (req *http.Reques
return
}
// NewHTTPEndpointDeleteRequestWithAuth 新建带鉴权头的 HTTP DELETE 请求
func NewHTTPEndpointDeleteRequestWithAuth(ep string, auth string) (req *http.Request, err error) {
req, err = http.NewRequest("DELETE", StandardAPI+ep, nil)
if err != nil {
return
}
req.Header.Add("Authorization", auth)
return
}
// NewHTTPEndpointPostRequestWithAuth 新建带鉴权头的 HTTP POST 请求
func NewHTTPEndpointPostRequestWithAuth(ep string, auth string, body io.Reader) (req *http.Request, err error) {
req, err = http.NewRequest("POST", StandardAPI+ep, body)
@@ -31,6 +41,16 @@ func NewHTTPEndpointPostRequestWithAuth(ep string, auth string, body io.Reader)
return
}
// NewHTTPEndpointPatchRequestWithAuth 新建带鉴权头的 HTTP PATCH 请求
func NewHTTPEndpointPatchRequestWithAuth(ep string, auth string, body io.Reader) (req *http.Request, err error) {
req, err = http.NewRequest("PATCH", StandardAPI+ep, body)
if err != nil {
return
}
req.Header.Add("Authorization", auth)
return
}
// WriteHTTPQueryIfNotNil 如果非空则将请求添加到 baseurl 后
//
// ex. WriteHTTPQueryIfNotNil("http://a.com/api", "a", 0, "b", 1, "c", 2) is http://a.com/api?b=1&c=2
@@ -65,8 +85,8 @@ func WriteHTTPQueryIfNotNil(baseurl string, queries ...any) string {
return sb.String()[:sb.Len()-1]
}
// WritePostBodyFromJSON 从 json 结构体 ptr 写入 bytes.Buffer, 忽略 error (内部使用不会出错)
func WritePostBodyFromJSON(ptr any) *bytes.Buffer {
// WriteBodyFromJSON 从 json 结构体 ptr 写入 bytes.Buffer, 忽略 error (内部使用不会出错)
func WriteBodyFromJSON(ptr any) *bytes.Buffer {
buf := bytes.NewBuffer(make([]byte, 0, 1024))
_ = json.NewEncoder(buf).Encode(ptr)
return buf