1
0
mirror of https://github.com/fumiama/NanoBot.git synced 2026-06-05 10:40:24 +08:00

add: postopenapiof codegen

This commit is contained in:
源文雨
2023-10-10 14:34:36 +09:00
parent 3dbb8d7b34
commit f04743c7a5
5 changed files with 134 additions and 1 deletions

View File

@@ -0,0 +1,51 @@
package main
import (
"os"
"strings"
)
const head = `// Code generated by codegen/postopenapiof. DO NOT EDIT.
package nano
import (
"io"
"unsafe"
"github.com/pkg/errors"
)
`
const template = `
func (bot *Bot) postOpenAPIof[T any](ep string, body io.Reader) (*[T any], error) {
resp := &struct {
CodeMessageBase
[T any]
}{}
err := bot.PostOpenAPI(ep, resp, body)
if err != nil {
err = errors.Wrap(err, getCallerFuncName())
return nil, err
}
return (*[T any])(unsafe.Add(unsafe.Pointer(resp), unsafe.Sizeof(CodeMessageBase{}))), nil
}
`
func main() {
f, err := os.Create("openapi_codegen_postopenapiof.go")
if err != nil {
panic(err)
}
defer f.Close()
_, err = f.WriteString(head)
if err != nil {
panic(err)
}
for _, name := range os.Args[1:] {
_, err = f.WriteString(strings.ReplaceAll(template, "[T any]", name))
if err != nil {
panic(err)
}
}
}

22
http.go
View File

@@ -1,14 +1,17 @@
package nano
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"reflect"
"strings"
)
// NewHTTPEndpointGetRequestWithAuth 新建带鉴权头的 HTTP 请求
// NewHTTPEndpointGetRequestWithAuth 新建带鉴权头的 HTTP GET 请求
func NewHTTPEndpointGetRequestWithAuth(ep string, auth string) (req *http.Request, err error) {
req, err = http.NewRequest("GET", StandardAPI+ep, nil)
if err != nil {
@@ -18,6 +21,16 @@ func NewHTTPEndpointGetRequestWithAuth(ep string, auth string) (req *http.Reques
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)
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
@@ -51,3 +64,10 @@ 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 {
buf := bytes.NewBuffer(make([]byte, 0, 1024))
_ = json.NewEncoder(buf).Encode(ptr)
return buf
}

View File

@@ -2,6 +2,7 @@ package nano
import (
"encoding/json"
"io"
"net/http"
"strconv"
"unsafe"
@@ -32,3 +33,27 @@ func (bot *Bot) GetOpenAPI(ep string, ptr any) error {
}
return nil
}
//go:generate go run codegen/postopenapiof/main.go Channel
// PostOpenAPI 从 ep 得到 json 结构化数据返回值写到 ptr, ptr 必须在开头继承 CodeMessageBase
func (bot *Bot) PostOpenAPI(ep string, ptr any, body io.Reader) error {
req, err := NewHTTPEndpointPostRequestWithAuth(ep, bot.Authorization(), body)
if err != nil {
return errors.Wrap(err, getCallerFuncName())
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrap(err, getCallerFuncName())
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(ptr)
if err != nil {
return errors.Wrap(err, getCallerFuncName())
}
respbbase := (*CodeMessageBase)(*(*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(&ptr), unsafe.Sizeof(uintptr(0)))))
if respbbase.C != 0 {
return errors.Wrap(errors.New("code: "+strconv.Itoa(respbbase.C)+", msg: "+respbbase.M), getCallerFuncName())
}
return nil
}

View File

@@ -34,3 +34,17 @@ func (bot *Bot) GetChannelsOfGuild(id string) (*ChannelArray, error) {
func (bot *Bot) GetChannelByID(id string) (*Channel, error) {
return bot.getOpenAPIofChannel("/channels/" + id)
}
// ChannelPost 子频道 post 操作所用对象
type ChannelPost struct {
Name string `json:"name"`
Type int `json:"type"`
SubType int `json:"sub_type"`
Position int `json:"position"`
ParentID string `json:"parent_id"`
OwnerID string `json:"owner_id,omitempty"`
PrivateType int `json:"private_type"`
PrivateUserIds []string `json:"private_user_ids,omitempty"`
SpeakPermission int `json:"speak_permission,omitempty"`
ApplicationID string `json:"application_id,omitempty"`
}

View File

@@ -0,0 +1,23 @@
// Code generated by codegen/postopenapiof. DO NOT EDIT.
package nano
import (
"io"
"unsafe"
"github.com/pkg/errors"
)
func (bot *Bot) postOpenAPIofChannel(ep string, body io.Reader) (*Channel, error) {
resp := &struct {
CodeMessageBase
Channel
}{}
err := bot.PostOpenAPI(ep, resp, body)
if err != nil {
err = errors.Wrap(err, getCallerFuncName())
return nil, err
}
return (*Channel)(unsafe.Add(unsafe.Pointer(resp), unsafe.Sizeof(CodeMessageBase{}))), nil
}