88 lines
2.1 KiB
Go
88 lines
2.1 KiB
Go
package memos
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/imroc/req/v3"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type Resource struct {
|
|
Content []byte `json:"content,omitempty"` // req
|
|
Filename string `json:"filename,omitempty"` // req
|
|
Type string `json:"type,omitempty"` // req resp
|
|
Name string `json:"name,omitempty"` // resp
|
|
Uid string `json:"uid,omitempty"` // resp
|
|
}
|
|
|
|
type Memo struct {
|
|
Content string `json:"content,omitempty"` // req
|
|
Visibility string `json:"visibility,omitempty"` // req
|
|
Name string `json:"name,omitempty"` // resp
|
|
Uid string `json:"uid,omitempty"` // resp
|
|
}
|
|
|
|
type PostData struct {
|
|
Host string
|
|
Token string
|
|
Content string
|
|
ChannelID int64
|
|
ChannelTitle string
|
|
Resources []Resource
|
|
}
|
|
|
|
func Post(pd PostData) error {
|
|
client := req.C().EnableInsecureSkipVerify().SetBaseURL(pd.Host).SetCommonBearerAuthToken(pd.Token)
|
|
|
|
resources := []Resource{}
|
|
|
|
for _, res := range pd.Resources {
|
|
var result Resource
|
|
|
|
resp, err := client.R().SetSuccessResult(&result).SetBodyJsonMarshal(res).Post("/api/v1/resources")
|
|
if err != nil {
|
|
return errors.Wrap(err, "post resource failed")
|
|
}
|
|
|
|
if !resp.IsSuccessState() {
|
|
return errors.New("post memo failed, body: " + resp.String())
|
|
}
|
|
|
|
resources = append(resources, result)
|
|
}
|
|
|
|
var createMemoResp Memo
|
|
|
|
resp, err := client.R().
|
|
SetSuccessResult(&createMemoResp).
|
|
SetBodyJsonMarshal(Memo{
|
|
Content: fmt.Sprintf("#%s #%d \n%s", pd.ChannelTitle, pd.ChannelID, pd.Content),
|
|
Visibility: "PUBLIC",
|
|
}).
|
|
Post("api/v1/memos")
|
|
if err != nil {
|
|
return errors.Wrap(err, "post memo failed")
|
|
}
|
|
|
|
if !resp.IsSuccessState() {
|
|
return errors.New("post memo failed, body: " + resp.String())
|
|
}
|
|
|
|
type Resources struct {
|
|
Resources []Resource `json:"resources"`
|
|
}
|
|
|
|
resp, err = client.R().
|
|
SetBodyJsonMarshal(Resources{Resources: resources}).
|
|
Patch(fmt.Sprintf("/api/v1/%s/resources", createMemoResp.Name))
|
|
if err != nil {
|
|
return errors.Wrap(err, "patch resources failed")
|
|
}
|
|
|
|
if !resp.IsSuccessState() {
|
|
return errors.New("post memo failed, body: " + resp.String())
|
|
}
|
|
|
|
return nil
|
|
}
|