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

@@ -18,6 +18,11 @@ type Project struct {
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"`
@@ -29,6 +34,11 @@ type User struct {
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"`
@@ -38,10 +48,22 @@ 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"`
} }
@@ -72,8 +94,8 @@ func (c *Client) GetMe() (*User, error) {
} }
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
@@ -81,9 +103,9 @@ func (c *Client) GetProjects(workspaceGID string) ([]*Project, error) {
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
@@ -91,6 +113,28 @@ func (c *Client) GetUserTaskList(userGID string) (*UserTaskList, error) {
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)
@@ -123,7 +167,6 @@ func (c *Client) get(path string, values *url.Values, out interface{}) error {
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 {
@@ -151,3 +194,23 @@ func (c *Client) get(path string, values *url.Values, out interface{}) error {
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)
}

29
main.go
View File

@@ -7,24 +7,43 @@ 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)
} }
*/
fmt.Printf("User: %s\n", me)
wrk, err := a.GetWorkspace() wrk, err := a.GetWorkspace()
if err != nil { if err != nil {
panic(err) panic(err)
} }
prjs, err := a.GetProjects(wrk.GID) fmt.Printf("Workspace: %s\n", wrk)
utl, err := a.GetUserTaskList(me.GID, wrk.GID)
if err != nil { if err != nil {
panic(err) panic(err)
} }
for _, prj := range prjs { fmt.Printf("User Task List: %s\n", utl)
fmt.Printf("%#v\n", prj)
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)
}
} }
} }