-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathopenapi.go
135 lines (118 loc) · 4.55 KB
/
openapi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package nano
import (
"encoding/json"
"io"
"net/http"
"reflect"
"strconv"
"strings"
"unsafe"
"github.com/pkg/errors"
)
const (
// StandardAPI 正式环境接口域名
StandardAPI = `https://api.sgroup.qq.com`
// SandboxAPI 沙箱环境接口域名
SandboxAPI = `https://sandbox.api.sgroup.qq.com`
// AccessTokenAPI 获取接口凭证的 API
AccessTokenAPI = "https://bots.qq.com/app/getAppAccessToken"
)
var (
OpenAPI = StandardAPI // OpenAPI 实际使用的 API, 默认 StandardAPI, 可自行赋值配置
)
// CodeMessageBase 各种消息都有的 code + message 基类
type CodeMessageBase struct {
C int `json:"code"`
M string `json:"message"`
}
func (bot *Bot) dohttprequest(constructer HTTPRequsetConstructer, ep, contenttype string, ptr any, body io.Reader) error {
appid := ""
if bot.IsV2() {
appid = bot.AppID
}
req, err := constructer(ep, contenttype, bot.Authorization(), appid, body)
if err != nil {
return errors.Wrap(err, getCallerFuncName())
}
resp, err := bot.client.Do(req)
if err != nil {
return errors.Wrap(err, getCallerFuncName())
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNoContent {
return nil
}
errsb := strings.Builder{}
var respbase *CodeMessageBase
if resp.StatusCode >= http.StatusBadRequest {
errsb.WriteString("code: ")
errsb.WriteString(resp.Status)
}
if ptr == nil {
goto RET
}
err = json.NewDecoder(resp.Body).Decode(ptr)
if err != nil {
if errsb.Len() > 0 {
errsb.WriteString(", ")
}
errsb.WriteString("json: ")
errsb.WriteString(err.Error())
goto RET
}
if reflect.ValueOf(ptr).Elem().Kind() == reflect.Slice {
return nil
}
respbase = (*CodeMessageBase)(*(*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(&ptr), unsafe.Sizeof(uintptr(0)))))
if respbase.C != 0 {
if errsb.Len() > 0 {
errsb.WriteString(", ")
}
errsb.WriteString("err: [")
errsb.WriteString(strconv.Itoa(respbase.C))
errsb.WriteString("] ")
if len([]rune(respbase.M)) > 256 {
errsb.WriteString(string([]rune(respbase.M)[:256]))
errsb.WriteString("...")
} else {
errsb.WriteString(respbase.M)
}
}
RET:
if errsb.Len() > 0 {
return errors.Wrap(errors.New(errsb.String()), getCallerFuncName())
}
return nil
}
//go:generate go run codegen/getopenapiof/main.go ShardWSSGateway User Guild Channel Member RoleMembers GuildRoleList ChannelPermissions Message MessageSetting PinsMessage Schedule MessageReactionUsers
// GetOpenAPI 从 ep 获取 json 结构化数据写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase
func (bot *Bot) GetOpenAPI(ep, contenttype string, ptr any) error {
return bot.dohttprequest(NewHTTPEndpointGetRequestWithAuth, ep, contenttype, ptr, nil)
}
// GetOpenAPIWithBody 不规范地从 ep 获取 json 结构化数据写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase
func (bot *Bot) GetOpenAPIWithBody(ep, contenttype string, ptr any, body io.Reader) error {
return bot.dohttprequest(NewHTTPEndpointGetRequestWithAuth, ep, contenttype, ptr, body)
}
//go:generate go run codegen/putopenapiof/main.go GuildRoleChannelID PinsMessage
// PutOpenAPI 向 ep 发送 PUT 并获取 json 结构化数据返回写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase
func (bot *Bot) PutOpenAPI(ep, contenttype string, ptr any, body io.Reader) error {
return bot.dohttprequest(NewHTTPEndpointPutRequestWithAuth, ep, contenttype, ptr, body)
}
// DeleteOpenAPI 向 ep 发送 DELETE 请求
func (bot *Bot) DeleteOpenAPI(ep, contenttype string, body io.Reader) error {
return bot.dohttprequest(NewHTTPEndpointDeleteRequestWithAuth, ep, contenttype, nil, body)
}
// DeleteOpenAPIWithPtr 带返回值地向 ep 发送 DELETE 请求
func (bot *Bot) DeleteOpenAPIWithPtr(ep, contenttype string, ptr any, body io.Reader) error {
return bot.dohttprequest(NewHTTPEndpointDeleteRequestWithAuth, ep, contenttype, ptr, body)
}
//go:generate go run codegen/postopenapiof/main.go Channel GuildRoleCreate Message DMS
// PostOpenAPI 从 ep 得到 json 结构化数据返回值写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase
func (bot *Bot) PostOpenAPI(ep, contenttype string, ptr any, body io.Reader) error {
return bot.dohttprequest(NewHTTPEndpointPostRequestWithAuth, ep, contenttype, ptr, body)
}
//go:generate go run codegen/patchopenapiof/main.go Channel GuildRolePatch
// PatchOpenAPI 从 ep 得到 json 结构化数据返回值写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase
func (bot *Bot) PatchOpenAPI(ep, contenttype string, ptr any, body io.Reader) error {
return bot.dohttprequest(NewHTTPEndpointPatchRequestWithAuth, ep, contenttype, ptr, body)
}