This repository was archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtask.go
More file actions
92 lines (83 loc) · 2.76 KB
/
task.go
File metadata and controls
92 lines (83 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package api
import (
"encoding/json"
"fmt"
"net/url"
)
// Task maps the JSON returned by TimeCamp API /tasks.
//
// API Docs: https://github.com/timecamp/timecamp-api/blob/master/sections/tasks.md
// Created with https://mholt.github.io/json-to-go/
type Task struct {
TaskID int `json:"task_id"`
ParentID int `json:"parent_id"`
AssignedBy int `json:"assigned_by"`
Name string `json:"name"`
ExternalTaskID string `json:"external_task_id"`
ExternalParentID string `json:"external_parent_id"`
Level int `json:"level"`
Archived int `json:"archived"`
Tags string `json:"tags"`
Budgeted int `json:"budgeted"`
BudgetUnit string `json:"budget_unit"`
RootGroupID int `json:"root_group_id"`
Billable int `json:"billable"`
Note string `json:"note"`
PublicHash string `json:"public_hash"`
AddDate string `json:"add_date"`
ModifyTime string `json:"modify_time"`
Color string `json:"color"`
Users interface{} `json:"users"`
UserAccessType int `json:"user_access_type"`
}
// IsProject is true if task is a project (=top-level task) in TimeCamp
func (t Task) IsProject() bool {
// Alternatively `t.Level == "1"` could be used to identify project tasks
return t.ParentID == 0
}
type TaskParams struct {
OnlyArchivedTasks bool
OnlyActiveTasks bool
}
// GetTasks wraps the "GET /tasks" api endpoint.
// Both "Projects" and "Tasks" in TimeCamp's UI are tasks.
func GetTasks(c Connection, params TaskParams) ([]Task, error) {
queryUrl, err := taskUrl(c, params)
if err != nil {
return nil, err
}
data, err := httpGet(queryUrl)
if err != nil {
return nil, err
}
// The returned json contains dynamic keys for the task object.
// It therefore can only be unmarshalled into a map.
var result map[string]Task
err = json.Unmarshal(data, &result)
if err != nil {
return nil, err
}
// The key is redundant, strip it.
var tasks []Task
for _, t := range result {
tasks = append(tasks, t)
}
return tasks, nil
}
func taskUrl(connection Connection, params TaskParams) (string, error) {
var exclude string
if params.OnlyActiveTasks && params.OnlyArchivedTasks {
return "", fmt.Errorf("at least one of active or archived tasks must be included")
} else if params.OnlyActiveTasks {
exclude = "exclude_archived=0"
} else if params.OnlyArchivedTasks {
exclude = "exclude_archived=1"
} else {
exclude = "" //nothing excluded
}
queryUrl, err := url.Parse(connection.ApiUrl + "/tasks/format/json/api_token/" + connection.Token + "?" + exclude)
if err != nil {
return "", err
}
return queryUrl.String(), err
}