forked from mbbx6spp/rebar-templates
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webmachine_resource.erl
277 lines (252 loc) · 8.51 KB
/
webmachine_resource.erl
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
%%%' HEADER
%%% @author {{author_name}} <{{author_email}}>
%%% @copyright {{copyright_year}} {{author_name}}.
%%% @doc {{description}}
-module({{app_name}}_{{name}}_resource).
-author('{{author_name}} <{{author_email}}>').
% Webmachine callback exports
-export([init/1, resource_exists/2, is_authorized/2, forbidden/2]).
-export([malformed_request/2, allowed_methods/2, content_types_provided/2]).
-export([content_types_accepted/2, charsets_provided/2, encodings_provided/2]).
-export([previously_existed/2, moved_permanently/2, moved_temporarily/2]).
-export([last_modified/2, expires/2, generate_etag/2, finish_request/2]).
-export([service_available/2]).
% to_X callback exports
-export([to_json/2, to_plain_text/2, to_html/2]).
-include_lib("webmachine/include/webmachine.hrl").
-define(DEFAULT_ENCODINGS, [{"identity", fun (X) -> X end}]).
-define(DEFAULT_CONTENT_TYPES, [
{"application/json", to_json},
{"text/plain", to_plain_text},
{"text/html", to_html}
]).
-ifdef(TEST).
-compile(export_all).
-endif.
%%%.
%%%' WEBMACHINE CALLBACKS
-spec init(InitArgs) -> {ok, State} | {error, Reason}
when
InitArgs :: list(),
State :: any(),
Reason :: term().
init([]) -> {ok, undefined}.
-spec resource_exists(ReqData, State) ->
{Result, NewReqData, NewState}
when
ReqData :: #wm_reqdata{},
State :: any(),
Result :: boolean() | {error, term()} | {halt, integer()},
NewReqData :: #wm_reqdata{},
NewState :: any().
resource_exists(ReqData, State) ->
{true, ReqData, State}.
-spec service_available(ReqData, State) ->
{Result, NewReqData, NewState}
when
ReqData :: #wm_reqdata{},
State :: any(),
Result :: boolean() | {error, term()} | {halt, integer()},
NewReqData :: #wm_reqdata{},
NewState :: any().
service_available(ReqData, State) ->
{true, ReqData, State}.
-spec is_authorized(ReqData, State) ->
{Result, NewReqData, NewState}
when
ReqData :: #wm_reqdata{},
State :: any(),
Result :: boolean() | {error, term()} | {halt, integer()},
NewReqData :: #wm_reqdata{},
NewState :: any().
is_authorized(ReqData, State) ->
{true, ReqData, State}.
-spec forbidden(ReqData, State) ->
{Result, NewReqData, NewState}
when
ReqData :: #wm_reqdata{},
State :: any(),
Result :: boolean() | {error, term()} | {halt, integer()},
NewReqData :: #wm_reqdata{},
NewState :: any().
forbidden(ReqData, State) ->
{false, ReqData, State}.
-spec malformed_request(ReqData, State) ->
{Result, NewReqData, NewState}
when
ReqData :: #wm_reqdata{},
State :: any(),
Result :: boolean() | {error, term()} | {halt, integer()},
NewReqData :: #wm_reqdata{},
NewState :: any().
malformed_request(ReqData, State) ->
{false, ReqData, State}.
-spec allowed_methods(ReqData, State) ->
{Result, NewReqData, NewState}
when
ReqData :: #wm_reqdata{},
State :: any(),
Result :: list(atom()) | {error, term()} | {halt, integer()},
NewReqData :: #wm_reqdata{},
NewState :: any().
allowed_methods(ReqData, State) ->
% TODO: Determine what methods are relevant to incoming request.
Methods = ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS'],
% NOTE By default will return empty list to force developer to think about this!:)
{[], ReqData, State}.
-spec content_types_provided(ReqData, State) ->
{Result, NewReqData, NewState}
when
ReqData :: #wm_reqdata{},
State :: any(),
Result :: [{MimeType :: string(), MimeTypeConverter :: function()}] |
[] | {error, term()} | {halt, integer()},
NewReqData :: #wm_reqdata{},
NewState :: any().
content_types_provided(ReqData, State) ->
ContentTypes = ?DEFAULT_CONTENT_TYPES,
{ContentTypes, ReqData, State}.
-spec content_types_accepted(ReqData, State) ->
{Result, NewReqData, NewState}
when
ReqData :: #wm_reqdata{},
State :: any(),
Result :: [{MimeType :: string(), MimeTypeConverter :: function()}] |
[] | {error, term()} | {halt, integer()},
NewReqData :: #wm_reqdata{},
NewState :: any().
content_types_accepted(ReqData, State) ->
ContentTypes = ?DEFAULT_CONTENT_TYPES,
{ContentTypes, ReqData, State}.
-spec charsets_provided(ReqData, State) ->
{Result, NewReqData, NewState}
when
ReqData :: #wm_reqdata{},
State :: any(),
Result :: [{Charset :: string(), CharsetConverter :: function()}] |
no_charset | {error, term()} | {halt, integer()},
NewReqData :: #wm_reqdata{},
NewState :: any().
charsets_provided(ReqData, State) ->
Charsets = [
],
{Charsets, ReqData, State}.
-spec encodings_provided(ReqData, State) ->
{Result, NewReqData, NewState}
when
ReqData :: #wm_reqdata{},
State :: any(),
Result :: [{Charset :: string(), CharsetConverter :: function()}] |
no_charset | {error, term()} | {halt, integer()},
NewReqData :: #wm_reqdata{},
NewState :: any().
encodings_provided(ReqData, State) ->
Encodings = case wrq:method(ReqData) of
'GET' ->
[ {"gzip", fun (X) -> zlib:gzip(X) end} | ?DEFAULT_ENCODINGS ];
_OtherMethod ->
?DEFAULT_ENCODINGS
end,
{Encodings, ReqData, State}.
-spec previously_existed(ReqData, State) ->
{Result, NewReqData, NewState}
when
ReqData :: #wm_reqdata{},
State :: any(),
Result :: boolean() | {error, term()} | {halt, integer()},
NewReqData :: #wm_reqdata{},
NewState :: any().
previously_existed(ReqData, State) ->
{false, ReqData, State}.
-spec moved_permanently(ReqData, State) ->
{Result, NewReqData, NewState}
when
ReqData :: #wm_reqdata{},
State :: any(),
Result :: boolean() | {error, term()} | {halt, integer()},
NewReqData :: #wm_reqdata{},
NewState :: any().
moved_permanently(ReqData, State) ->
{false, ReqData, State}.
-spec moved_temporarily(ReqData, State) ->
{Result, NewReqData, NewState}
when
ReqData :: #wm_reqdata{},
State :: any(),
Result :: boolean() | {error, term()} | {halt, integer()},
NewReqData :: #wm_reqdata{},
NewState :: any().
moved_temporarily(ReqData, State) ->
{false, ReqData, State}.
-spec last_modified(ReqData, State) ->
{Result, NewReqData, NewState}
when
ReqData :: #wm_reqdata{},
State :: any(),
Year :: integer(4),
Month :: integer(2),
Date :: integer(2),
Hour :: integer(2),
Minute :: integer(2),
Second :: integer(2),
Result :: undefined | { {Year, Month, Date}, {Hour, Minute, Second} } |
{error, term()} | {halt, integer()},
NewReqData :: #wm_reqdata{},
NewState :: any().
last_modified(ReqData, State) ->
% TODO: Figure out way to set this appropriately for resource
{undefined, ReqData, State}.
-spec expires(ReqData, State) ->
{Result, NewReqData, NewState}
when
ReqData :: #wm_reqdata{},
State :: any(),
Year :: integer(4),
Month :: integer(2),
Date :: integer(2),
Hour :: integer(2),
Minute :: integer(2),
Second :: integer(2),
Result :: undefined | { {Year, Month, Date}, {Hour, Minute, Second} } |
{error, term()} | {halt, integer()},
NewReqData :: #wm_reqdata{},
NewState :: any().
expires(ReqData, State) ->
% TODO: Figure out way to set this appropriately for resource
{undefined, ReqData, State}.
-spec generate_etag(ReqData, State) ->
{Result, NewReqData, NewState}
when
ReqData :: #wm_reqdata{},
State :: any(),
Result :: undefined | string() | {error, term()} | {halt, integer()},
NewReqData :: #wm_reqdata{},
NewState :: any().
generate_etag(ReqData, State) ->
% TODO: Figure out way to set this appropriately for resource
{undefined, ReqData, State}.
-spec finish_request(ReqData, State) ->
{Result, NewReqData, NewState}
when
ReqData :: #wm_reqdata{},
State :: any(),
Result :: undefined | string() | {error, term()} | {halt, integer()},
NewReqData :: #wm_reqdata{},
NewState :: any().
finish_request(ReqData, State) ->
% TODO: Modify ReqData object returned to effect processing of HTTP request
% since value returned is actually ignored by Webmachine.
{true, ReqData, State}.
%%%.
%%%' OUTPUT FUNCTIONS
to_html(ReqData, State) ->
{"<html><body></body></html>", ReqData, State}.
to_plain_text(ReqData, State) ->
{"OK", ReqData, State}.
to_json(ReqData, State) ->
{"{'status': 200, 'text': 'OK'}", ReqData, State}.
%%%.
%%%' PRIVATE FUNCTIONS
% TODO: Add private functions here...
%%%.
%%% vim: set filetype=erlang tabstop=2 foldmarker=%%%',%%%. foldmethod=marker: