-
Notifications
You must be signed in to change notification settings - Fork 1
/
asana.lua
51 lines (47 loc) · 1.3 KB
/
asana.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
--luacheck: allow defined top
-- Setup core constants
local asana = {}
asana.baseUrl = "https://app.asana.com/api/1.0"
asana.reqHeader = {["Authorization"] = "Bearer " .. init.consts.asanaApiKey}
asana.userId = nil
asana.workspaceIds = {}
-- Get Asana userId and workspaceIds
function asana.getIds()
local _, res, _ = hs.http.get(asana.baseUrl .. "/users/me", asana.reqHeader)
res = hs.json.decode(res)
asana.userId = res.data.id
hs.fnutils.each(
res.data.workspaces,
function(x)
asana.workspaceIds[x.name] = x.id
end
)
end
-- Creates a new Asana task with a given name in a given workspace
-- First time function is called it retrieves IDs
function asana.newTask(taskName, workspaceName)
if not asana.userId then
asana.getIds()
end
hs.http.asyncPost(
string.format(
"%s/tasks?assignee=%i&workspace=%i&name=%s",
asana.baseUrl,
asana.userId,
asana.workspaceIds[workspaceName],
hs.http.encodeForQuery(taskName)
),
"", -- requires empty body
asana.reqHeader,
function(code, res)
if code == 201 then
hs.notify.show("Asana", "", "New task added to workspace: " .. workspaceName)
else
hs.notify.show("Asana", "", "Error adding task")
print(res)
hs.toggleConsole()
end
end
)
end
return asana