-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_tasks.go
69 lines (48 loc) · 1.42 KB
/
api_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
65
66
67
68
69
// Tasks API
package main
import (
"encoding/json"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
func api_getTasks(response http.ResponseWriter, request *http.Request) {
session := GetSessionFromRequest(request)
if session == nil {
ReturnAPIError(response, 401, "UNAUTHORIZED", "You must provide a valid active session to use this API.")
return
}
allTasks := GetVault().tasks.GetAllTasks()
jsonResult, err := json.Marshal(allTasks)
if err != nil {
LogError(err)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
ReturnAPI_JSON(response, request, jsonResult)
}
func api_getTask(response http.ResponseWriter, request *http.Request) {
session := GetSessionFromRequest(request)
if session == nil {
ReturnAPIError(response, 401, "UNAUTHORIZED", "You must provide a valid active session to use this API.")
return
}
vars := mux.Vars(request)
task_id, err := strconv.ParseUint(vars["id"], 10, 64)
if err != nil {
response.WriteHeader(400)
return
}
taskInfo := GetVault().tasks.GetTaskInfo(task_id)
if taskInfo == nil {
ReturnAPIError(response, 404, "TASK_NOT_FOUND", "Task not found.")
return
}
jsonResult, err := json.Marshal(taskInfo)
if err != nil {
LogError(err)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
ReturnAPI_JSON(response, request, jsonResult)
}