diff --git a/app/common/url_consts.go b/app/common/url_consts.go index f47bdc3..d6ef0d0 100644 --- a/app/common/url_consts.go +++ b/app/common/url_consts.go @@ -47,4 +47,7 @@ const ( // bot offline OfflineBot = "/user/offline" + + // Asset about + AssetCreate = "/asset/create" ) diff --git a/app/core/action/asset.go b/app/core/action/asset.go new file mode 100644 index 0000000..9bda9dd --- /dev/null +++ b/app/core/action/asset.go @@ -0,0 +1,36 @@ +package action + +import ( + "bytes" + "github.com/aimerny/kook-go/app/common" + "github.com/aimerny/kook-go/app/core/model" + jsoniter "github.com/json-iterator/go" + "mime/multipart" +) + +func AssetCreate(filename string, content []byte) (*model.AssetResp, error) { + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + + file, err := writer.CreateFormFile("file", filename) + if err != nil { + return nil, err + } + file.Write(content) + err = writer.Close() + if err != nil { + return nil, err + } + headers := make(map[string]string) + headers["Content-Type"] = writer.FormDataContentType() + resp, err := doPostWithHeaders(common.AssetCreate, body.Bytes(), headers) + if err != nil { + return nil, err + } + result := &model.KookResponse[*model.AssetResp]{} + err = jsoniter.Unmarshal(resp, &result) + if err != nil { + return nil, err + } + return result.Data, nil +} diff --git a/app/core/action/base.go b/app/core/action/base.go index b97cfb1..f0ea20a 100644 --- a/app/core/action/base.go +++ b/app/core/action/base.go @@ -29,3 +29,7 @@ func doPost(actionUrl string, body any) ([]byte, error) { } return response, nil } + +func doPostWithHeaders(actionUrl string, body []byte, headers map[string]string) ([]byte, error) { + return helper.PostWithHeaders(common.BaseUrl+common.V3Url+actionUrl, body, headers) +} diff --git a/app/core/action/channel.go b/app/core/action/channel.go index 0ef1e22..a9f4765 100644 --- a/app/core/action/channel.go +++ b/app/core/action/channel.go @@ -21,12 +21,12 @@ func channelList(guildId string, page, pageSize int) *model.ChannelListResp { if err != nil { log.WithError(err).Error("get channels failed") } - guildResp := &model.ChannelListResp{} - err = jsoniter.Unmarshal(resp, &guildResp) + channelListResp := &model.ChannelListResp{} + err = jsoniter.Unmarshal(resp, &channelListResp) if err != nil { log.WithError(err).Error("unmarshal resp failed") } - return guildResp + return channelListResp } func PageChannelList(guildId string, page, pageSize int) *model.ChannelListResp { diff --git a/app/core/helper/http_helper.go b/app/core/helper/http_helper.go index af84c23..3dad1ec 100644 --- a/app/core/helper/http_helper.go +++ b/app/core/helper/http_helper.go @@ -31,7 +31,6 @@ func Get(url string) (*http.Response, error) { } func Post(url string, body interface{}) ([]byte, error) { - client := &http.Client{} bodyData, err := json.Marshal(body) if err != nil { return nil, err @@ -41,6 +40,24 @@ func Post(url string, body interface{}) ([]byte, error) { request, err := http.NewRequest("POST", url, buffer) request.Header.Add("Authorization", fmt.Sprintf("Bot %s", globalToken)) request.Header.Add("Content-Type", "application/json; charset=UTF-8") + return postAction(request) +} + +func PostWithHeaders(url string, body []byte, headers map[string]string) ([]byte, error) { + buffer := bytes.NewBuffer(body) + request, err := http.NewRequest("POST", url, buffer) + if err != nil { + return nil, err + } + request.Header.Add("Authorization", fmt.Sprintf("Bot %s", globalToken)) + for key, value := range headers { + request.Header.Add(key, value) + } + return postAction(request) +} + +func postAction(request *http.Request) ([]byte, error) { + client := &http.Client{} resp, err := client.Do(request) if err != nil { return nil, err diff --git a/app/core/model/dto.go b/app/core/model/dto.go index 01edeb4..9ad67c5 100644 --- a/app/core/model/dto.go +++ b/app/core/model/dto.go @@ -82,3 +82,8 @@ type ChannelInfo struct { LimitAmount int `json:"limit_amount"` IsCategory bool `json:"is_category"` } + +// ===== Asset ===== +type AssetResp struct { + Url string `json:"url"` +}