-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.lua
106 lines (97 loc) · 2.27 KB
/
api.lua
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
local vim = vim
local json = require('nvim-todoist.lib.json')
local base_uri = 'https://api.todoist.com/rest/v1'
local api = {}
local function auth_str(api_key)
return '"Authorization: Bearer ' .. api_key .. '"'
end
function api.fetch_active_tasks(api_key, cb)
vim.fn.jobstart(
string.format('curl -X GET "%s" -H %s', base_uri .. '/tasks', auth_str(api_key)),
{
stdout_buffered = true,
on_stdout =
function(_, d, _)
local json = json.decode(table.concat(d))
cb(json)
end,
}
)
end
function api.fetch_projects(api_key, cb)
vim.fn.jobstart(
string.format('curl -X GET "%s" -H %s', base_uri .. '/projects', auth_str(api_key)),
{
stdout_buffered = true,
on_stdout =
function(_, d, _)
cb(json.decode(table.concat(d)))
end,
}
)
end
function api.update_task(api_key, task_id, data)
vim.fn.jobstart(
string.format(
'curl -X POST "%s" -H %s --data \'%s\'',
base_uri .. '/tasks/' .. tostring(task_id),
auth_str(api_key),
json.encode(data)
)
)
end
function api.close_task(api_key, task_id)
vim.fn.jobstart(
string.format(
'curl -X POST "%s" -H %s',
base_uri .. '/tasks/' .. tostring(task_id) .. '/close',
auth_str(api_key)
)
)
end
function api.reopen_task(api_key, task_id)
vim.fn.jobstart(
string.format(
'curl -X POST "%s" -H %s',
base_uri .. '/tasks/' .. tostring(task_id) .. '/reopen',
auth_str(api_key)
)
)
end
function api.create_task(api_key, data, cb)
vim.fn.jobstart(
string.format(
'curl -X POST "%s" --data \'%s\' -H %s -H "Content-Type: application/json"',
base_uri .. '/tasks',
json.encode(data),
auth_str(api_key)
),
cb and
{
stdout_buffered = true,
on_stdout =
function(_, d, _)
cb(json.decode(table.concat(d)))
end,
} or
nil
)
end
function api.delete_task(api_key, task_id, cb)
vim.fn.jobstart(
string.format(
'curl -X DELETE "%s" -H %s',
base_uri .. '/tasks/' .. tostring(task_id),
auth_str(api_key)
),
cb and
{
stdout_buffered = true,
on_stdout = function(_, d, _)
cb(json.decode(table.concat(d)))
end,
} or
nil
)
end
return api