Fetching sections and tasks

This commit is contained in:
Ian Gulliver
2021-09-10 04:57:54 +00:00
parent 9b0c167ade
commit 274399e5ab
2 changed files with 181 additions and 99 deletions

View File

@@ -10,144 +10,207 @@ import "os"
import "github.com/firestuff/asana-rules/headers" import "github.com/firestuff/asana-rules/headers"
type Client struct { type Client struct {
client *http.Client client *http.Client
} }
type Project struct { type Project struct {
GID string `json:"gid"` GID string `json:"gid"`
Name string `json:"name"` Name string `json:"name"`
}
type Section struct {
GID string `json:"gid"`
Name string `json:"name"`
} }
type Task struct { type Task struct {
GID string `json:"gid"` GID string `json:"gid"`
Name string `json:"name"` Name string `json:"name"`
} }
type User struct { type User struct {
GID string `json:"gid"` GID string `json:"gid"`
Name string `json:"name"` Name string `json:"name"`
Email string `json:"email"` Email string `json:"email"`
}
type UserTaskList struct {
GID string `json:"gid"`
Name string `json:"name"`
} }
type Workspace struct { type Workspace struct {
GID string `json:"gid"` GID string `json:"gid"`
Name string `json:"name"` Name string `json:"name"`
} }
type projectsResponse struct { type projectsResponse struct {
Data []*Project `json:"data"` Data []*Project `json:"data"`
}
type sectionsResponse struct {
Data []*Section `json:"data"`
}
type tasksResponse struct {
Data []*Task `json:"data"`
} }
type userResponse struct { type userResponse struct {
Data *User `json:"data"` Data *User `json:"data"`
}
type userTaskListResponse struct {
Data *UserTaskList `json:"data"`
} }
type workspacesResponse struct { type workspacesResponse struct {
Data []*Workspace `json:"data"` Data []*Workspace `json:"data"`
} }
func NewClient(token string) *Client { func NewClient(token string) *Client {
c := &Client{ c := &Client{
client: &http.Client{}, client: &http.Client{},
} }
hdrs := headers.NewHeaders(c.client) hdrs := headers.NewHeaders(c.client)
hdrs.Add("Accept", "application/json") hdrs.Add("Accept", "application/json")
hdrs.Add("Authorization", fmt.Sprintf("Bearer %s", token)) hdrs.Add("Authorization", fmt.Sprintf("Bearer %s", token))
return c return c
} }
func NewClientFromEnv() *Client { func NewClientFromEnv() *Client {
return NewClient(os.Getenv("ASANA_TOKEN")) return NewClient(os.Getenv("ASANA_TOKEN"))
} }
func (c *Client) GetMe() (*User, error) { func (c *Client) GetMe() (*User, error) {
resp := &userResponse{} resp := &userResponse{}
err := c.get("users/me", nil, resp) err := c.get("users/me", nil, resp)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return resp.Data, nil return resp.Data, nil
} }
func (c *Client) GetProjects(workspaceGID string) ([]*Project, error) { func (c *Client) GetProjects(workspaceGID string) ([]*Project, error) {
resp := &projectsResponse{} path := fmt.Sprintf("workspaces/%s/projects", workspaceGID)
path := fmt.Sprintf("workspaces/%s/projects", workspaceGID) resp := &projectsResponse{}
err := c.get(path, nil, resp) err := c.get(path, nil, resp)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return resp.Data, nil return resp.Data, nil
} }
func (c *Client) GetUserTaskList(userGID string) (*UserTaskList, error) { func (c *Client) GetSections(projectGID string) ([]*Section, error) {
resp := &userTaskListResponse{} path := fmt.Sprintf("projects/%s/sections", projectGID)
path := fmt.Sprintf("users/%s/user_task_list", userGID) resp := &sectionsResponse{}
err := c.get(path, nil, resp) err := c.get(path, nil, resp)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return resp.Data, nil return resp.Data, nil
}
func (c *Client) GetTasksFromSection(sectionGID string) ([]*Task, error) {
path := fmt.Sprintf("sections/%s/tasks", sectionGID)
resp := &tasksResponse{}
err := c.get(path, nil, resp)
if err != nil {
return nil, err
}
return resp.Data, nil
}
func (c *Client) GetUserTaskList(userGID, workspaceGID string) (*UserTaskList, error) {
path := fmt.Sprintf("users/%s/user_task_list", userGID)
values := &url.Values{}
values.Add("workspace", workspaceGID)
resp := &userTaskListResponse{}
err := c.get(path, values, resp)
if err != nil {
return nil, err
}
return resp.Data, nil
} }
func (c *Client) GetWorkspaces() ([]*Workspace, error) { func (c *Client) GetWorkspaces() ([]*Workspace, error) {
resp := &workspacesResponse{} resp := &workspacesResponse{}
err := c.get("workspaces", nil, resp) err := c.get("workspaces", nil, resp)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return resp.Data, nil return resp.Data, nil
} }
// Returns one workspace if there is only one // Returns one workspace if there is only one
func (c *Client) GetWorkspace() (*Workspace, error) { func (c *Client) GetWorkspace() (*Workspace, error) {
workspaces, err := c.GetWorkspaces() workspaces, err := c.GetWorkspaces()
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(workspaces) != 1 { if len(workspaces) != 1 {
return nil, fmt.Errorf("%d workspaces found", len(workspaces)) return nil, fmt.Errorf("%d workspaces found", len(workspaces))
} }
return workspaces[0], nil return workspaces[0], nil
} }
const baseURL = "https://app.asana.com/api/1.0/" const baseURL = "https://app.asana.com/api/1.0/"
func (c *Client) get(path string, values *url.Values, out interface{}) error { func (c *Client) get(path string, values *url.Values, out interface{}) error {
if values == nil { if values == nil {
values = &url.Values{} values = &url.Values{}
} }
values.Add("limit", "100") values.Add("limit", "100")
url := fmt.Sprintf("%s%s?%s", baseURL, path, values.Encode()) url := fmt.Sprintf("%s%s?%s", baseURL, path, values.Encode())
fmt.Printf("%s\n", url)
req, err := http.NewRequest("GET", url, nil) req, err := http.NewRequest("GET", url, nil)
if err != nil { if err != nil {
return err return err
} }
resp, err := c.client.Do(req) resp, err := c.client.Do(req)
if err != nil { if err != nil {
return err return err
} }
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return err
} }
return fmt.Errorf("%s: %s", resp.Status, string(body)) return fmt.Errorf("%s: %s", resp.Status, string(body))
} }
dec := json.NewDecoder(resp.Body) dec := json.NewDecoder(resp.Body)
err = dec.Decode(out) err = dec.Decode(out)
if err != nil { if err != nil {
return err return err
} }
return nil return nil
}
func (s *Section) String() string {
return fmt.Sprintf("%s (%s)", s.GID, s.Name)
}
func (t *Task) String() string {
return fmt.Sprintf("%s (%s)", t.GID, t.Name)
}
func (u *User) String() string {
return fmt.Sprintf("%s (%s <%s>)", u.GID, u.Name, u.Email)
}
func (utl *UserTaskList) String() string {
return fmt.Sprintf("%s (%s)", utl.GID, utl.Name)
}
func (wrk *Workspace) String() string {
return fmt.Sprintf("%s (%s)", wrk.GID, wrk.Name)
} }

55
main.go
View File

@@ -5,26 +5,45 @@ import "fmt"
import "github.com/firestuff/asana-rules/asana" import "github.com/firestuff/asana-rules/asana"
func main() { func main() {
a := asana.NewClientFromEnv() a := asana.NewClientFromEnv()
/* me, err := a.GetMe()
me, err := a.GetMe() if err != nil {
if err != nil { panic(err)
panic(err) }
}
*/
wrk, err := a.GetWorkspace() fmt.Printf("User: %s\n", me)
if err != nil {
panic(err)
}
prjs, err := a.GetProjects(wrk.GID) wrk, err := a.GetWorkspace()
if err != nil { if err != nil {
panic(err) panic(err)
} }
for _, prj := range prjs { fmt.Printf("Workspace: %s\n", wrk)
fmt.Printf("%#v\n", prj)
} utl, err := a.GetUserTaskList(me.GID, wrk.GID)
if err != nil {
panic(err)
}
fmt.Printf("User Task List: %s\n", utl)
secs, err := a.GetSections(utl.GID)
if err != nil {
panic(err)
}
fmt.Printf("Sections:\n")
for _, sec := range secs {
fmt.Printf("\t%s\n", sec)
tasks, err := a.GetTasksFromSection(sec.GID)
if err != nil {
panic(err)
}
for _, task := range tasks {
fmt.Printf("\t\t%s\n", task)
}
}
} }