diff --git a/module/error_response.go b/module/error_response.go new file mode 100644 index 0000000..53dd6b3 --- /dev/null +++ b/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 diff --git a/module/story/stories.go b/module/story/stories.go index 1e9cb81..92f4d21 100644 --- a/module/story/stories.go +++ b/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 diff --git a/module/task/follower.go b/module/task/follower.go new file mode 100644 index 0000000..7bc276a --- /dev/null +++ b/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 + } +} diff --git a/module/task/task.go b/module/task/task.go index 5c3309f..8813e03 100644 --- a/module/task/task.go +++ b/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"` + Response Response `json:"data"` + Errors module.Errors `json:"errors"` } ) + client, err := r.call(URICreateTask, util.HttpPostMethod) + + 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) { - resp = new(Response) + 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 } diff --git a/module/users/user.go b/module/users/user.go index e247dcc..f4b8a02 100644 --- a/module/users/user.go +++ b/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) { diff --git a/text/rich_text.go b/text/rich_text.go new file mode 100644 index 0000000..5cc7aaf --- /dev/null +++ b/text/rich_text.go @@ -0,0 +1,16 @@ +package text + +import "fmt" + +const ( + DataMention = `` +) + +// `
+// testing +// +// +// ` +func Mention(gid string) string { + return fmt.Sprintf(DataMention, gid) +} diff --git a/util/constant.go b/util/constant.go index 9d0bd07..e678c13 100644 --- a/util/constant.go +++ b/util/constant.go @@ -6,6 +6,7 @@ const ( ContentType = "application/json" HttpGetMethod = "GET" + HttpPutMethod = "PUT" HttpPostMethod = "POST" AsanaDateFormat = "2006-01-02" diff --git a/util/http.go b/util/http.go index e500863..76f838f 100644 --- a/util/http.go +++ b/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 }