Browse Source

asana html text

master v0.1.4
ldf 3 years ago
parent
commit
b68cf87c6b
  1. 36
      module/story/stories.go
  2. 67
      module/task/task.go
  3. 32
      text/rich_text.go
  4. 11
      util/http.go

36
module/story/stories.go

@ -8,6 +8,7 @@ import (
"git.drinkme.beer/yinghe/log"
"git.drinkme.beer/yinghe/asana/module/users"
"git.drinkme.beer/yinghe/asana/util"
)
@ -57,12 +58,19 @@ func (r *Request) SetPAToken(token string) {
type Response struct {
ID string `json:"gid"`
ResourceType string `json:"resource_type"`
ResourceSubtype string `json:"resource_subtype"`
Type string `json:"type"`
Text string `json:"text"`
// IsPinned Conditional
// Whether the story should be pinned on the resource.
IsPinned bool `json:"is_pinned"`
StickerName string `json:"sticker_name,omitempty"`
CreatedAt string `json:"created_at"`
CreatedBy users.User `json:"created_by"`
}
func (r *Response) IsComplete() bool {
return "marked_complete" == r.ResourceSubtype
}
func (r Request) validate() error {
@ -125,3 +133,31 @@ func (r Request) Create() (resp *Response, err error) {
log.Infof("[%s]new comment ID:%s", r.taskID, resp.ID)
return
}
func (r Request) Get() ([]Response, error) {
var (
resp struct {
Data []Response `json:"data"`
}
)
headers := make(map[string]string)
headers["Authorization"] = r.paToken
headers["Content-Type"] = util.ContentType
client := util.NewHttpClient(util.AsanaHost, fmt.Sprintf(URI, r.taskID), util.HttpGetMethod, nil)
client.Headers = headers
err := client.Request()
if nil != err || http.StatusOK != client.HTTPStatus {
client.Print()
log.Errorf("failed")
}
err = json.Unmarshal(client.Body, &resp)
if nil != err {
log.Errorf("invalid response")
return nil, fmt.Errorf("invalid response")
}
return resp.Data, nil
}

67
module/task/task.go

@ -67,7 +67,7 @@ func (r Request) Update(taskID string) error {
return err
}
if http.StatusOK != c.HTTPStatus {
log.Infof("response: %s", string(c.Body))
c.Print()
log.Errorf("unexpected response")
err = errors.New("unexpected response")
return err
@ -86,7 +86,7 @@ func (r Request) Create() (resp *Response, err error) {
log.Infof("response status: %d", client.HTTPStatus)
if http.StatusCreated != client.HTTPStatus {
log.Infof("response: %s", string(client.Body))
client.Print()
log.Errorf("unexpected response")
err = errors.New("unexpected response")
return
@ -94,7 +94,7 @@ func (r Request) Create() (resp *Response, err error) {
err = json.Unmarshal(client.Body, &respData)
if nil != err {
log.Infof("response: %s", string(client.Body))
client.Print()
log.Errorf("illegal task result: %s", err.Error())
return
}
@ -105,64 +105,11 @@ func (r Request) Create() (resp *Response, err error) {
return
}
//func (r Request) Create() (resp *Response, err error) {
// var (
// reqData Data
// respData struct {
// Response Response `json:"data"`
// }
// )
//
// resp = new(Response)
//
// headers := make(map[string]string)
// headers["Authorization"] = r.paToken
// headers["Content-Type"] = util.ContentType
//
// if "" == r.ResourceSubtype {
// r.ResourceSubtype = ResourceSubtype
// }
//
// reqData.Request = r
//
// buf, err := json.Marshal(&reqData)
// if nil != err {
// log.Errorf("failed to generate task request: %s", err.Error())
// return
// }
//
// log.Infof("request: %s", string(buf))
//
// client := util.NewHttpClient(util.AsanaHost, URI, util.HttpPostMethod, buf)
// client.Headers = headers
//
// err = client.Request()
// if nil != err {
// log.Errorf("failed to create new Asana task: %s", err.Error())
// return
// }
// log.Infof("response status: %d", client.HTTPStatus)
// if http.StatusCreated != client.HTTPStatus {
// log.Errorf("unexpected response")
// err = errors.New("unexpected response")
// return
// }
//
// err = json.Unmarshal(client.Body, &respData)
// if nil != err {
// log.Errorf("illegal task result: %s", err.Error())
// return
// }
// resp = &respData.Response
// log.Infof("[%s]new task ID:%s", r.TicketID, resp.ID)
// return
//}
func (r Request) call(uri, httpMethod string) (*util.Client, error) {
var (
err error
reqData Data
client = new(util.Client)
)
headers := make(map[string]string)
@ -178,18 +125,18 @@ func (r Request) call(uri, httpMethod string) (*util.Client, error) {
buf, err := json.Marshal(&reqData)
if nil != err {
log.Errorf("failed to generate task request: %s", err.Error())
return nil, err
return client, err
}
log.Infof("request: %s", string(buf))
client := util.NewHttpClient(util.AsanaHost, uri, httpMethod, buf)
client = util.NewHttpClient(util.AsanaHost, uri, httpMethod, buf)
client.Headers = headers
err = client.Request()
if nil != err {
log.Errorf("failed to create new Asana task: %s", err.Error())
return nil, err
return client, err
}
return client, nil

32
text/rich_text.go

@ -1,6 +1,11 @@
package text
import "fmt"
import (
"fmt"
"html"
"regexp"
"strings"
)
const (
DataMention = `<a data-asana-gid="%s"/>`
@ -14,3 +19,28 @@ const (
func Mention(gid string) string {
return fmt.Sprintf(DataMention, gid)
}
func DeleteHtmlTags(src string) string {
var (
line int
v, des string
)
if "" == src || 0 == len(src) {
return ""
}
reg := regexp.MustCompile(`<.+?>`)
str := reg.ReplaceAllString(src, " ")
s := strings.Split(str, "\n")
for _, v = range s {
if 0 == len(strings.TrimSpace(v)) {
continue
}
des = des + fmt.Sprintf("\n%s", v)
line++
}
if 0 == line {
return ""
}
return html.UnescapeString(des)
}

11
util/http.go

@ -1,7 +1,6 @@
package util
import (
"bufio"
"bytes"
"context"
"errors"
@ -80,13 +79,21 @@ type Client struct {
HTTPStatus int
Body []byte // Indicates both Request Body & Response Body
TraceId interface{}
Buffer *bufio.Reader
//Buffer *bufio.Reader
}
func (c *Client) AddHeader(k, v string) {
c.Headers[k] = v
}
func (c *Client) Print() {
if nil != c.Body {
log.Infof(string(c.Body))
} else {
log.Infof("nil")
}
}
type Bytes []byte
func (self Bytes) BuildRequest() []byte {

Loading…
Cancel
Save