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

11
main.go
View File

@@ -21,14 +21,14 @@ func main() {
fmt.Printf("Workspace: %s\n", wrk)
utl, err := a.GetUserTaskList(me.GID, wrk.GID)
utl, err := a.GetUserTaskList(me, wrk)
if err != nil {
panic(err)
}
fmt.Printf("User Task List: %s\n", utl)
secs, err := a.GetSections(utl.GID)
secs, err := a.GetSections(utl)
if err != nil {
panic(err)
}
@@ -37,7 +37,12 @@ func main() {
for _, sec := range secs {
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 {
panic(err)
}