Browse Source

rich text

master
ldf 3 years ago
parent
commit
93bc316578
  1. 9
      module/error_response.go
  2. 4
      module/story/stories.go
  3. 68
      module/task/follower.go
  4. 143
      module/task/task.go
  5. 20
      module/users/user.go
  6. 16
      text/rich_text.go
  7. 1
      util/constant.go
  8. 1
      util/http.go

9
module/error_response.go

@ -0,0 +1,9 @@
package module
type Error struct {
Code string `json:"error"`
Message string `json:"message"`
Help string `json:"help"`
}
type Errors []Error

4
module/story/stories.go

@ -21,9 +21,9 @@ type Data struct {
type Request struct {
HtmlText string `json:"html_text,omitempty"`
Text string `json:"text"`
Text string `json:"text,omitempty"`
StickerName string `json:"sticker_name,omitempty"`
IsPinned bool `json:"is_pinned"`
IsPinned bool `json:"is_pinned,omitempty"`
taskID string
ticketID string

68
module/task/follower.go

@ -0,0 +1,68 @@
package task
import (
"encoding/json"
"fmt"
"net/http"
"git.drinkme.beer/yinghe/log"
"git.drinkme.beer/yinghe/asana/util"
)
const (
AddFollowersURI = "/api/1.0/tasks/%s/addFollowers"
)
type Follower struct {
Followers []string `json:"followers,omitempty"`
}
type FollowersRequest struct {
Data Follower `json:"data"`
}
func (f Follower) Add(token, taskID string) error {
var (
req FollowersRequest
)
if "" == token || "" == taskID {
log.Errorf("illegal request")
return fmt.Errorf("illegal request")
}
if nil == f.Followers {
log.Errorf("invalid request, followers")
return fmt.Errorf("none followers")
}
req.Data = f
buf, err := json.Marshal(&req)
if nil != err {
log.Errorf("invalid arguments: %s", err.Error())
return err
}
log.Infof("request ==> %s", string(buf))
headers := make(map[string]string)
headers["Authorization"] = token
headers["Content-Type"] = util.ContentType
client := util.NewHttpClient(util.AsanaHost,
fmt.Sprintf(AddFollowersURI, taskID),
util.HttpPostMethod, buf)
client.Headers = headers
err = client.Request()
if nil != err {
log.Errorf("failed to create new Asana task: %s", err.Error())
return err
}
log.Infof("response status: %d", client.HTTPStatus)
if http.StatusOK != client.HTTPStatus {
log.Errorf("unexpected response")
err = fmt.Errorf("unexpected response")
return err
} else {
return nil
}
}

143
module/task/task.go

@ -3,10 +3,12 @@ package task
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"git.drinkme.beer/yinghe/log"
"git.drinkme.beer/yinghe/asana/module"
"git.drinkme.beer/yinghe/asana/util"
)
@ -16,17 +18,18 @@ type Data struct {
type Request struct {
ResourceSubtype string `json:"resource_subtype,omitempty"`
Assignee string `json:"assignee"`
Name string `json:"name"`
Completed bool `json:"completed,omitempty"`
Assignee string `json:"assignee,omitempty"`
Name string `json:"name,omitempty"`
Completed bool `json:"completed"`
DueOn string `json:"due_on,omitempty"`
Liked bool `json:"linked,omitempty"`
// Notes refer to the content of each Ticket
Notes string `json:"notes"`
Notes string `json:"notes,omitempty"`
HtmlNotes string `json:"html_notes,omitempty"`
StartOn string `json:"start_on,omitempty"`
CustomFields map[string]string `json:"custom_fields"`
Projects []string `json:"projects"`
Workspace string `json:"workspace"`
CustomFields map[string]string `json:"custom_fields,omitempty"`
Projects []string `json:"projects,omitempty"`
Workspace string `json:"workspace,omitempty"`
TicketID string `json:"-"`
TicketType string `json:"-"`
@ -49,19 +52,118 @@ type Response struct {
}
const (
URI = "/api/1.0/tasks"
URICreateTask = "/api/1.0/tasks"
URIUpdateTask = "/api/1.0/tasks/%s"
ResourceSubtype = "default_task"
)
func (r Request) Update(taskID string) error {
var (
uri = fmt.Sprintf(URIUpdateTask, taskID)
err error
)
c, err := r.call(uri, util.HttpPutMethod)
if nil != err || nil == c {
return err
}
if http.StatusOK != c.HTTPStatus {
log.Infof("response: %s", string(c.Body))
log.Errorf("unexpected response")
err = errors.New("unexpected response")
return err
}
return nil
}
func (r Request) Create() (resp *Response, err error) {
var (
reqData Data
respData struct {
Response Response `json:"data"`
Errors module.Errors `json:"errors"`
}
)
client, err := r.call(URICreateTask, util.HttpPostMethod)
resp = new(Response)
log.Infof("response status: %d", client.HTTPStatus)
if http.StatusCreated != client.HTTPStatus {
log.Infof("response: %s", string(client.Body))
log.Errorf("unexpected response")
err = errors.New("unexpected response")
return
}
err = json.Unmarshal(client.Body, &respData)
if nil != err {
log.Infof("response: %s", string(client.Body))
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) 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
)
headers := make(map[string]string)
headers["Authorization"] = r.paToken
@ -76,32 +178,19 @@ func (r Request) Create() (resp *Response, err error) {
buf, err := json.Marshal(&reqData)
if nil != err {
log.Errorf("failed to generate task request: %s", err.Error())
return
return nil, err
}
log.Infof("request: %s", string(buf))
client := util.NewHttpClient(util.AsanaHost, URI, util.HttpPostMethod, 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
}
log.Infof("response status: %d", client.HTTPStatus)
if http.StatusCreated != client.HTTPStatus {
log.Errorf("unexpected response")
err = errors.New("unexpected response")
return
return nil, err
}
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
return client, nil
}

20
module/users/user.go

@ -16,21 +16,21 @@ const (
)
type User struct {
ID string `json:"gid"`
ResourceType string `json:"resource_type"`
Name string `json:"name"`
Email string `json:"email"`
ID string `json:"gid,omitempty"`
ResourceType string `json:"resource_type,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
Photo map[string]string `json:"photo"`
Photo map[string]string `json:"photo,omitempty"`
Workspaces []struct {
ID string `json:"gid"`
ResourceType string `json:"resource_type"`
Name string `json:"name"`
} `json:"workspaces"`
ID string `json:"gid,omitempty"`
ResourceType string `json:"resource_type,omitempty"`
Name string `json:"name,omitempty"`
} `json:"workspaces,omitempty"`
}
type Response struct {
Data User `json:"data"`
Data User `json:"data,omitempty"`
}
func (u User) Get(paToken string) (*User, error) {

16
text/rich_text.go

@ -0,0 +1,16 @@
package text
import "fmt"
const (
DataMention = `<a data-asana-gid="%s"/>`
)
// `<body>
// testing
// <a data-asana-gid="1199521781039350"/>
// <a data-asana-gid="1200773217477032"/>
// </body>`
func Mention(gid string) string {
return fmt.Sprintf(DataMention, gid)
}

1
util/constant.go

@ -6,6 +6,7 @@ const (
ContentType = "application/json"
HttpGetMethod = "GET"
HttpPutMethod = "PUT"
HttpPostMethod = "POST"
AsanaDateFormat = "2006-01-02"

1
util/http.go

@ -139,7 +139,6 @@ func (c *Client) Request() (err error) {
c.HTTPStatus = resp.StatusCode
c.Body, err = ioutil.ReadAll(resp.Body)
log.Infof("response: %s", string(c.Body))
return
}

Loading…
Cancel
Save