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.

67 lines
1.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package users
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "git.drinkme.beer/yinghe/log"
  8. "git.drinkme.beer/yinghe/asana/util"
  9. )
  10. const (
  11. URI = "/api/1.0/users/%s"
  12. )
  13. type User struct {
  14. ID string `json:"gid,omitempty"`
  15. ResourceType string `json:"resource_type,omitempty"`
  16. Name string `json:"name,omitempty"`
  17. Email string `json:"email,omitempty"`
  18. Photo map[string]string `json:"photo,omitempty"`
  19. Workspaces []struct {
  20. ID string `json:"gid,omitempty"`
  21. ResourceType string `json:"resource_type,omitempty"`
  22. Name string `json:"name,omitempty"`
  23. } `json:"workspaces,omitempty"`
  24. }
  25. type Response struct {
  26. Data User `json:"data,omitempty"`
  27. }
  28. func (u User) Get(paToken string) (*User, error) {
  29. var (
  30. err error
  31. resp Response
  32. )
  33. headers := make(map[string]string)
  34. headers["Authorization"] = paToken
  35. headers["Content-Type"] = util.ContentType
  36. client := util.NewHttpClient(util.AsanaHost, fmt.Sprintf(URI, u.ID), util.HttpGetMethod, nil)
  37. client.Headers = headers
  38. err = client.Request()
  39. if nil != err {
  40. log.Errorf("failed to obtain user info: %s", err.Error())
  41. return nil, err
  42. }
  43. log.Infof("response status: %d", client.HTTPStatus)
  44. if http.StatusOK != client.HTTPStatus {
  45. log.Errorf("unexpected response")
  46. err = errors.New("unexpected response")
  47. return nil, err
  48. }
  49. err = json.Unmarshal(client.Body, &resp)
  50. if nil != err {
  51. log.Errorf("illegal task result: %s", err.Error())
  52. return nil, err
  53. }
  54. return &resp.Data, err
  55. }