· 5 years ago · May 07, 2020, 11:06 AM
1if not http then
2 printError( "MineKV requires http API" )
3 printError( "Set http_enable to true in ComputerCraft.cfg" )
4 return
5end
6
7local sHost = "https://"
8
9function get( key )
10 local res, err = http.get(
11 sHost .. textutils.urlEncode( key )
12 )
13
14 if res then
15 local code = res.getResponseCode()
16
17 if code ~= 200 then
18 io.stderr:write( "Failed.\n" )
19 print(err)
20 end
21
22 local body = res.readAll()
23 res.close()
24
25 return body
26 else
27 io.stderr:write( "Failed.\n" )
28 print(err)
29 end
30end
31
32function set( key, value )
33 local res, err = http.post(
34 sHost .. textutils.urlEncode( key ) .. "/" .. textutils.urlEncode( value ), ""
35 )
36
37 if res then
38 local code = res.getResponseCode()
39
40 if code ~= 200 then
41 io.stderr:write( "Failed.\n" )
42 print(err)
43 end
44
45 return
46 else
47 io.stderr:write( "Failed.\n" )
48 print(err)
49 end
50end