annotate src/api/Api.luan @ 50:0d99cec5142c

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 17 Nov 2022 22:00:59 -0700
parents 9166f6a14021
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local new_error = Luan.new_error or error()
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local Parsers = require "luan:Parsers.luan"
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local json_string = Parsers.json_string or error()
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local Io = require "luan:Io.luan"
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Http = require "luan:http/Http.luan"
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Api = {}
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 function Api.api(fn)
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 return function()
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 Io.stdout = Http.response.text_writer()
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 try
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local rtn = fn()
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 %><%=json_string(rtn)%><%
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 catch e
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 if e.error ~= nil then
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 %><%=json_string(e.error)%><%
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 return
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 end
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 e.throw()
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 end
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 end
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 end
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 function Api.user_error(msg)
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 local e = new_error(msg)
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 e.error = {
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 okay = false
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 error = msg
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 }
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 e.throw()
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 end
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 return Api