Asana Integeration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.4 KiB

3 years ago
  1. package task
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "git.drinkme.beer/yinghe/log"
  7. "git.drinkme.beer/yinghe/asana/util"
  8. )
  9. const (
  10. AddFollowersURI = "/api/1.0/tasks/%s/addFollowers"
  11. )
  12. type Follower struct {
  13. Followers []string `json:"followers,omitempty"`
  14. }
  15. type FollowersRequest struct {
  16. Data Follower `json:"data"`
  17. }
  18. func (f Follower) Add(token, taskID string) error {
  19. var (
  20. req FollowersRequest
  21. )
  22. if "" == token || "" == taskID {
  23. log.Errorf("illegal request")
  24. return fmt.Errorf("illegal request")
  25. }
  26. if nil == f.Followers {
  27. log.Errorf("invalid request, followers")
  28. return fmt.Errorf("none followers")
  29. }
  30. req.Data = f
  31. buf, err := json.Marshal(&req)
  32. if nil != err {
  33. log.Errorf("invalid arguments: %s", err.Error())
  34. return err
  35. }
  36. log.Infof("request ==> %s", string(buf))
  37. headers := make(map[string]string)
  38. headers["Authorization"] = token
  39. headers["Content-Type"] = util.ContentType
  40. client := util.NewHttpClient(util.AsanaHost,
  41. fmt.Sprintf(AddFollowersURI, taskID),
  42. util.HttpPostMethod, buf)
  43. client.Headers = headers
  44. err = client.Request()
  45. if nil != err {
  46. log.Errorf("failed to create new Asana task: %s", err.Error())
  47. return err
  48. }
  49. log.Infof("response status: %d", client.HTTPStatus)
  50. if http.StatusOK != client.HTTPStatus {
  51. log.Errorf("unexpected response")
  52. err = fmt.Errorf("unexpected response")
  53. return err
  54. } else {
  55. return nil
  56. }
  57. }