-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.go
64 lines (57 loc) · 1.79 KB
/
tasks.go
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
package taskswrapper
import (
"io/ioutil"
"log"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"google.golang.org/api/tasks/v1"
)
// GetTasksService Creates the tasks service using the files
func GetTasksService() (*tasks.Service, error) {
b, err := ioutil.ReadFile("credentials.json")
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
// If modifying these scopes, delete your previously saved token.json.
config, err := google.ConfigFromJSON(b, tasks.TasksScope)
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
client := getClient(config)
if err != nil {
log.Fatalf("Unable to retrieve tasks Client %v", err)
}
ctx := context.Background()
srv, err := tasks.NewService(ctx, option.WithHTTPClient(client))
return srv, err
}
// GetAllTasksLists fetches the user's lists
func GetAllTasksLists(srv *tasks.Service) (*tasks.TaskLists, error) {
userLists, err := srv.Tasklists.List().Do()
return userLists, err
}
// GetTaskList returns TaskList object at index idx
func GetTaskList(srv *tasks.Service, idx int) (*tasks.TaskList, error) {
var list *tasks.TaskList = nil
userLists, err := GetAllTasksLists(srv)
if len(userLists.Items) > (idx - 1) {
list = userLists.Items[idx-1]
} else {
log.Fatalf("List ID out of range!")
}
return list, err
}
// GetFirstTaskList fetches the first Task List from the user
func GetFirstTaskList(srv *tasks.Service) (*tasks.TaskList, error) {
var firstList *tasks.TaskList
userLists, err := GetAllTasksLists(srv)
if len(userLists.Items) > 0 {
firstList = userLists.Items[0]
}
return firstList, err
}
// GetFirstTaskList Sends the list items
func GetAllTaskListItems(srv *tasks.Service, id string) (*tasks.Tasks, error) {
return srv.Tasks.List(id).Do()
}