Engine 0.0.6
Game engine in lua
Loading...
Searching...
No Matches
http.lua
Go to the documentation of this file.
1local zeebo_pipeline = require('src/lib/util/pipeline')
2
3--! @defgroup std
4--! @{
5--! @defgroup http
6--! @pre require @c http
7--! @{
8
9--! @short reduced response
10--! @brief disconnect when receiving status
11local function fast(self)
12 self.speed = '_fast'
13 return self
14end
16local function body(self, content)
17 self.body_content=content
18 return self
19end
20
21local function param(self, name, value)
22 local index = #self.param_list + 1
23 self.param_list[index] = name
24 self.param_dict[name] = value
25 return self
26end
27
28local function header(self, name, value)
29 local index = #self.header_list + 1
30 self.header_list[index] = name
31 self.header_dict[name] = value
32 return self
33end
34
35local function success(self, handler_func)
36 self.success_handler = handler_func
37 return self
38end
40local function failed(self, handler_func)
41 self.failed_handler = handler_func
42 return self
43end
44
45local function error(self, handler_func)
46 self.error_handler = handler_func
47 return self
48end
49
50--! @}
51--! @}
52
53--! @cond
54local function request(method, std, game, application, protocol_handler)
55 local callback_handler = application and application.callbacks and application.callbacks.http
56
57 if not callback_handler then
58 callback_handler = function() end
59 end
60
61 return function (url)
62 local self = {
63 -- content
64 url = url,
65 speed = '',
66 method = method,
67 body_content = '',
68 header_list = {},
69 header_dict = {},
70 param_list = {},
71 param_dict = {},
72 callback_handler = callback_handler,
73 success_handler = function (std, game) end,
74 failed_handler = function (std, game) end,
75 error_handler = function (std, game) end,
76 -- objects
77 std = std,
78 game = game,
80 -- functions
81 fast = fast,
82 body = body,
83 param = param,
84 header = header,
86 failed = failed,
87 error = error,
88 run = zeebo_pipeline.run,
89 -- internal
90 protocol_handler = protocol_handler
91 }
92
93 self.promise = function()
95 end
96
97 self.resolve = function()
99 end
100
101 self.set = function (key, value)
102 self.std.http[key] = value
103 end
104
105 self.pipeline = {
106 -- eval
107 function()
108 self:protocol_handler()
109 end,
110 -- callbacks
111 function()
112 -- global handler
113 self.callback_handler(self.std, self.game)
114 -- local handlers
115 if self.std.http.ok then
116 self.success_handler(self.std, self.game)
117 elseif self.std.http.error or not self.std.http.status then
118 self.error_handler(self.std, self.game)
119 else
120 self.failed_handler(self.std, self.game)
121 end
122 end,
123 -- clean http
124 function ()
125 self.std.http.ok = nil
126 self.std.http.body = nil
127 self.std.http.error = nil
128 self.std.http.status = nil
129 end,
130 -- reset request
131 function()
133 end
134 }
135
136 return self
137 end
138end
139--! @endcond
140
141local function install(std, game, application, protocol)
142 local protocol_handler = protocol.handler
143 local event = nil
144
145 std.http = std.http or {}
146 std.http.get=request('GET', std, game, application, protocol_handler)
147 std.http.head=request('HEAD', std, game, application, protocol_handler)
148 std.http.post=request('POST', std, game, application, protocol_handler)
149 std.http.put=request('PUT', std, game, application, protocol_handler)
150 std.http.delete=request('DELETE', std, game, application, protocol_handler)
151 std.http.patch=request('PATCH', std, game, application, protocol_handler)
152
153 if protocol.install then
154 local m = protocol.install(std, game, application)
155 event = m and m.event
156 end
157
158 return {
159 event=event,
160 std={http=std.http}
161 }
162end
163
164local P = {
166}
167
168return P
local function stop(self)
local function resume(self)
local function require(std, game, application)
local function reset(self)
local function success(self, handler_func)
local function header(self, name, value)
local function body(self, content)
local function error(self, handler_func)
local function fast(self)
reduced response
local function param(self, name, value)
local function failed(self, handler_func)
local function http(std, game)
local function run(args)
local ok
Definition main.lua:40
local application
Definition main.lua:16
local game
Definition main.lua:17
local std
Definition main.lua:18
local event
nclua:event
Definition main.lua:26
local function install(std, game, application, protocol)
local zeebo_pipeline
Definition http.lua:1