Skip to content

Commit

Permalink
feat: ✨ add asset create action && add post custom header (#29)
Browse files Browse the repository at this point in the history
* feat: ✨ add asset create action && add post custom header

* feat: ✨ fix support asset create
  • Loading branch information
Aimerny authored Aug 4, 2024
1 parent 0daf44d commit 6a65e7d
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 deletions.
3 changes: 3 additions & 0 deletions app/common/url_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@ const (
// bot offline

OfflineBot = "/user/offline"

// Asset about
AssetCreate = "/asset/create"
)
36 changes: 36 additions & 0 deletions app/core/action/asset.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions app/core/action/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 3 additions & 3 deletions app/core/action/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 18 additions & 1 deletion app/core/helper/http_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions app/core/model/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 6a65e7d

Please sign in to comment.