Real task display

This commit is contained in:
Ian Gulliver
2021-09-10 05:19:28 +00:00
parent 274399e5ab
commit 879a96e2bf
2 changed files with 63 additions and 27 deletions

View File

@@ -6,13 +6,24 @@ import "fmt"
import "net/http" import "net/http"
import "net/url" import "net/url"
import "os" import "os"
import "strings"
import "github.com/firestuff/asana-rules/headers" import "github.com/firestuff/asana-rules/headers"
var _TRUE = true
var TRUE = &_TRUE
var _FALSE = false
var FALSE = &_FALSE
type Client struct { type Client struct {
client *http.Client client *http.Client
} }
type SearchQuery struct {
SectionsAny []*Section
Completed *bool
}
type Project struct { type Project struct {
GID string `json:"gid"` GID string `json:"gid"`
Name string `json:"name"` Name string `json:"name"`
@@ -26,6 +37,7 @@ type Section struct {
type Task struct { type Task struct {
GID string `json:"gid"` GID string `json:"gid"`
Name string `json:"name"` Name string `json:"name"`
HTMLNotes string `json:"html_notes"`
} }
type User struct { type User struct {
@@ -34,14 +46,13 @@ type User struct {
Email string `json:"email"` Email string `json:"email"`
} }
type UserTaskList struct { type Workspace struct {
GID string `json:"gid"` GID string `json:"gid"`
Name string `json:"name"` Name string `json:"name"`
} }
type Workspace struct { type projectResponse struct {
GID string `json:"gid"` Data *Project `json:"data"`
Name string `json:"name"`
} }
type projectsResponse struct { type projectsResponse struct {
@@ -60,10 +71,6 @@ 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"`
} }
@@ -93,8 +100,8 @@ func (c *Client) GetMe() (*User, error) {
return resp.Data, nil return resp.Data, nil
} }
func (c *Client) GetProjects(workspaceGID string) ([]*Project, error) { func (c *Client) GetProjects(workspace *Workspace) ([]*Project, error) {
path := fmt.Sprintf("workspaces/%s/projects", workspaceGID) path := fmt.Sprintf("workspaces/%s/projects", workspace.GID)
resp := &projectsResponse{} resp := &projectsResponse{}
err := c.get(path, nil, resp) err := c.get(path, nil, resp)
if err != nil { if err != nil {
@@ -103,8 +110,8 @@ func (c *Client) GetProjects(workspaceGID string) ([]*Project, error) {
return resp.Data, nil return resp.Data, nil
} }
func (c *Client) GetSections(projectGID string) ([]*Section, error) { func (c *Client) GetSections(project *Project) ([]*Section, error) {
path := fmt.Sprintf("projects/%s/sections", projectGID) path := fmt.Sprintf("projects/%s/sections", project.GID)
resp := &sectionsResponse{} resp := &sectionsResponse{}
err := c.get(path, nil, resp) err := c.get(path, nil, resp)
if err != nil { if err != nil {
@@ -113,8 +120,8 @@ func (c *Client) GetSections(projectGID string) ([]*Section, error) {
return resp.Data, nil return resp.Data, nil
} }
func (c *Client) GetTasksFromSection(sectionGID string) ([]*Task, error) { func (c *Client) GetTasksFromSection(section *Section) ([]*Task, error) {
path := fmt.Sprintf("sections/%s/tasks", sectionGID) path := fmt.Sprintf("sections/%s/tasks", section.GID)
resp := &tasksResponse{} resp := &tasksResponse{}
err := c.get(path, nil, resp) err := c.get(path, nil, resp)
if err != nil { if err != nil {
@@ -123,11 +130,11 @@ func (c *Client) GetTasksFromSection(sectionGID string) ([]*Task, error) {
return resp.Data, nil return resp.Data, nil
} }
func (c *Client) GetUserTaskList(userGID, workspaceGID string) (*UserTaskList, error) { func (c *Client) GetUserTaskList(user *User, workspace *Workspace) (*Project, error) {
path := fmt.Sprintf("users/%s/user_task_list", userGID) path := fmt.Sprintf("users/%s/user_task_list", user.GID)
values := &url.Values{} values := &url.Values{}
values.Add("workspace", workspaceGID) values.Add("workspace", workspace.GID)
resp := &userTaskListResponse{} resp := &projectResponse{}
err := c.get(path, values, resp) err := c.get(path, values, resp)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -158,6 +165,34 @@ func (c *Client) GetWorkspace() (*Workspace, error) {
return workspaces[0], nil return workspaces[0], nil
} }
func (c *Client) Search(workspace *Workspace, q *SearchQuery) ([]*Task, error) {
path := fmt.Sprintf("workspaces/%s/tasks/search", workspace.GID)
values := &url.Values{}
values.Add("opt_fields", "html_notes,name")
if len(q.SectionsAny) > 0 {
gids := []string{}
for _, sec := range q.SectionsAny {
gids = append(gids, sec.GID)
}
values.Add("sections.any", strings.Join(gids, ","))
}
if q.Completed != nil {
values.Add("completed", fmt.Sprintf("%t", *q.Completed))
}
resp := &tasksResponse{}
err := c.get(path, values, resp)
if err != nil {
return nil, err
}
return resp.Data, 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 {
@@ -207,10 +242,6 @@ func (u *User) String() string {
return fmt.Sprintf("%s (%s <%s>)", u.GID, u.Name, u.Email) 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 { func (wrk *Workspace) String() string {
return fmt.Sprintf("%s (%s)", wrk.GID, wrk.Name) return fmt.Sprintf("%s (%s)", wrk.GID, wrk.Name)
} }

11
main.go
View File

@@ -21,14 +21,14 @@ func main() {
fmt.Printf("Workspace: %s\n", wrk) fmt.Printf("Workspace: %s\n", wrk)
utl, err := a.GetUserTaskList(me.GID, wrk.GID) utl, err := a.GetUserTaskList(me, wrk)
if err != nil { if err != nil {
panic(err) panic(err)
} }
fmt.Printf("User Task List: %s\n", utl) fmt.Printf("User Task List: %s\n", utl)
secs, err := a.GetSections(utl.GID) secs, err := a.GetSections(utl)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -37,7 +37,12 @@ func main() {
for _, sec := range secs { for _, sec := range secs {
fmt.Printf("\t%s\n", sec) fmt.Printf("\t%s\n", sec)
tasks, err := a.GetTasksFromSection(sec.GID) q := &asana.SearchQuery{
SectionsAny: []*asana.Section{sec},
Completed: asana.FALSE,
}
tasks, err := a.Search(wrk, q)
if err != nil { if err != nil {
panic(err) panic(err)
} }