|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/go-resty/resty/v2" |
| 8 | +) |
| 9 | + |
| 10 | +type TasksResponse struct { |
| 11 | + Data []Task `json:"data"` |
| 12 | +} |
| 13 | + |
| 14 | +type TaskResponse struct { |
| 15 | + Data Task `json:"data"` |
| 16 | +} |
| 17 | + |
| 18 | +type TasksQueryNextPage struct { |
| 19 | + Offset string `json:"offset"` |
| 20 | +} |
| 21 | + |
| 22 | +type TasksQueryResponse struct { |
| 23 | + Data []Task `json:"data"` |
| 24 | + NextPage TasksQueryNextPage `json:"next_page"` |
| 25 | +} |
| 26 | + |
| 27 | +// AsanaClient is a client for interacting with the Asana API. |
| 28 | +type AsanaClient struct { |
| 29 | + client *resty.Client |
| 30 | + token string |
| 31 | + baseURL string |
| 32 | +} |
| 33 | + |
| 34 | +// NewAsanaClient creates and returns a new AsanaClient using the provided personal access token. |
| 35 | +func NewAsanaClient(token string) *AsanaClient { |
| 36 | + c := resty.New() |
| 37 | + c = c.SetBaseURL("https://app.asana.com/api/1.0") |
| 38 | + c.SetHeader("Authorization", "Bearer "+token) |
| 39 | + c.SetHeader("Accept", "application/json") |
| 40 | + return &AsanaClient{ |
| 41 | + client: c, |
| 42 | + token: token, |
| 43 | + baseURL: "https://app.asana.com/api/1.0", |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// Task represents an Asana task with the most common fields. |
| 48 | +type Task struct { |
| 49 | + Gid string `json:"gid,omitempty"` // Globally unique task identifier. |
| 50 | + Name string `json:"name,omitempty"` // The name of the task. |
| 51 | + Completed bool `json:"completed,omitempty"` // Task completion status. |
| 52 | + Liked bool `json:"liked,omitempty"` // Task like status. |
| 53 | + Assignee *User `json:"assignee,omitempty"` // The user assigned to the task. |
| 54 | + DueOn string `json:"due_on,omitempty"` // Task due date (YYYY-MM-DD format). |
| 55 | + DueAt string `json:"due_at,omitempty"` // Task due date (YYYY-MM-DDTHH:MM:SS format). |
| 56 | + StartAt string `json:"start_at,omitempty"` // Task start date (YYYY-MM-DD format). |
| 57 | + StartOn string `json:"start_on,omitempty"` // Task start date (YYYY-MM-DDTHH:MM:SS format). |
| 58 | + Parent string `json:"parent,omitempty"` // Parent task identifier. |
| 59 | + Notes string `json:"notes,omitempty"` // Task description or notes. |
| 60 | + Memberships []Memberships `json:"memberships,omitempty"` // Project memberships.s |
| 61 | + CreatedAt string `json:"created_at,omitempty"` // Timestamp when the task was created. |
| 62 | + UpdatedAt string `json:"modified_at,omitempty"` // Timestamp when the task was last modified. |
| 63 | + ResourceSubtype string `json:"resource_subtype,omitempty"` |
| 64 | + CustomFields []CustomField `json:"custom_fields,omitempty"` |
| 65 | + |
| 66 | + // Internal fields for CreateTask and UpdateTask |
| 67 | + Project string `json:"project,omitempty"` |
| 68 | +} |
| 69 | + |
| 70 | +type Memberships struct { |
| 71 | + Project ProjectSection `json:"project,omitempty"` |
| 72 | + Section ProjectSection `json:"section,omitempty"` |
| 73 | +} |
| 74 | + |
| 75 | +type ProjectSection struct { |
| 76 | + Gid string `json:"gid,omitempty"` // Globally unique project identifier. |
| 77 | + Name string `json:"name,omitempty"` // The name of the project. |
| 78 | +} |
| 79 | + |
| 80 | +// User represents an Asana user. |
| 81 | +type User struct { |
| 82 | + Gid string `json:"gid"` // Globally unique user identifier. |
| 83 | + Name string `json:"name"` // The name of the user. |
| 84 | +} |
| 85 | + |
| 86 | +type CustomField struct { |
| 87 | + Gid string `json:"gid,omitempty"` |
| 88 | + Name string `json:"name,omitempty"` |
| 89 | + DisplayValue *string `json:"display_value,omitempty"` |
| 90 | +} |
| 91 | + |
| 92 | +// GetTasks retrieves tasks from Asana. |
| 93 | +func (ac *AsanaClient) GetTasks() ([]Task, error) { |
| 94 | + resp, err := ac.client.R(). |
| 95 | + SetResult(&TasksResponse{}). |
| 96 | + Get("/tasks") |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + result := resp.Result().(*TasksResponse) |
| 101 | + return result.Data, nil |
| 102 | +} |
| 103 | + |
| 104 | +// CreateTask creates a new task in Asana. |
| 105 | +func (ac *AsanaClient) CreateTask(task Task) (*Task, error) { |
| 106 | + payload := map[string]interface{}{ |
| 107 | + "data": map[string]interface{}{ |
| 108 | + "projects": task.Project, |
| 109 | + "name": task.Name, |
| 110 | + "notes": task.Notes, |
| 111 | + "due_on": task.DueAt, |
| 112 | + // Additional fields can be added here. |
| 113 | + "resource_type": "task", |
| 114 | + }, |
| 115 | + } |
| 116 | + resp, err := ac.client.R(). |
| 117 | + SetBody(payload). |
| 118 | + SetResult(&TaskResponse{}). |
| 119 | + Post("/tasks") |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + |
| 124 | + if resp.IsError() { |
| 125 | + return nil, fmt.Errorf("failed to create task (%d): %s", resp.StatusCode(), resp.String()) |
| 126 | + } |
| 127 | + |
| 128 | + result := resp.Result().(*TaskResponse) |
| 129 | + return &result.Data, nil |
| 130 | +} |
| 131 | + |
| 132 | +// UpdateTask updates an existing task in Asana. |
| 133 | +func (ac *AsanaClient) UpdateTask(id string, task Task) (*Task, error) { |
| 134 | + payload := map[string]interface{}{ |
| 135 | + "data": map[string]interface{}{ |
| 136 | + /* "name": task.Name, |
| 137 | + "notes": task.Notes, |
| 138 | + "due_on": task.DueOn, |
| 139 | + "completed": task.Completed, */ |
| 140 | + }, |
| 141 | + } |
| 142 | + if task.Name != "" { |
| 143 | + payload["data"].(map[string]interface{})["name"] = task.Name |
| 144 | + } |
| 145 | + if task.Notes != "" { |
| 146 | + payload["data"].(map[string]interface{})["notes"] = task.Notes |
| 147 | + } |
| 148 | + if task.DueAt != "" { |
| 149 | + payload["data"].(map[string]interface{})["due_on"] = task.DueAt |
| 150 | + } |
| 151 | + if task.Completed { |
| 152 | + payload["data"].(map[string]interface{})["completed"] = task.Completed |
| 153 | + } |
| 154 | + if task.Liked { |
| 155 | + payload["data"].(map[string]interface{})["liked"] = task.Liked |
| 156 | + } |
| 157 | + if task.StartAt != "" { |
| 158 | + payload["data"].(map[string]interface{})["start_on"] = task.StartAt |
| 159 | + } |
| 160 | + |
| 161 | + log.Printf("Payload: %v (%s)", payload, id) |
| 162 | + resp, err := ac.client.R(). |
| 163 | + SetBody(payload). |
| 164 | + SetResult(&TaskResponse{}). |
| 165 | + Put(fmt.Sprintf("/tasks/%s", id)) |
| 166 | + if err != nil { |
| 167 | + return nil, err |
| 168 | + } |
| 169 | + |
| 170 | + if resp.IsError() { |
| 171 | + return nil, fmt.Errorf("failed to update task (%d): %s", resp.StatusCode(), resp.String()) |
| 172 | + } |
| 173 | + result := resp.Result().(*TaskResponse) |
| 174 | + return &result.Data, nil |
| 175 | +} |
| 176 | + |
| 177 | +// DeleteTask deletes a task in Asana. |
| 178 | +func (ac *AsanaClient) DeleteTask(id string) error { |
| 179 | + resp, err := ac.client.R(). |
| 180 | + Delete(fmt.Sprintf("/tasks/%s", id)) |
| 181 | + |
| 182 | + if err != nil { |
| 183 | + return err |
| 184 | + } |
| 185 | + |
| 186 | + if resp.IsError() { |
| 187 | + return fmt.Errorf("failed to delete task (%d): %s", resp.StatusCode(), resp.String()) |
| 188 | + } |
| 189 | + |
| 190 | + return nil |
| 191 | +} |
0 commit comments