-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsr_entities_handler.erl
232 lines (216 loc) · 8.67 KB
/
sr_entities_handler.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
%%% @doc Base GET|POST /[entities] implementation
-module(sr_entities_handler).
-export([ init/3
, rest_init/2
, allowed_methods/2
, resource_exists/2
, content_types_accepted/2
, content_types_provided/2
, handle_get/2
, handle_post/2
]).
-export([ announce_req/2
, handle_post/3
]).
-type options() :: sr_state:options().
-type state() :: sr_state:state().
-export_type([state/0, options/0]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Cowboy Callbacks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% @doc Upgrades to cowboy_rest.
%% Basically, just returns <code>{upgrade, protocol, cowboy_rest}</code>
%% @see cowboy_rest:init/3
-spec init({atom(), atom()}, cowboy_req:req(), options()) ->
{upgrade, protocol, cowboy_rest}.
init(_Transport, _Req, _Opts) ->
{upgrade, protocol, cowboy_rest}.
%% @doc Announces the Req and moves on.
%% If <code>verbose := true</code> in <code>Opts</code> for this handler
%% prints out a line indicating that endpoint that was hit.
%% @see cowboy_rest:rest_init/2
-spec rest_init(cowboy_req:req(), options()) ->
{ok, cowboy_req:req(), state()}.
rest_init(Req, Opts) ->
Req1 = announce_req(Req, Opts),
#{model := Model} = Opts,
Module = sumo_config:get_prop_value(Model, module),
State = sr_state:new(Opts, Module),
{ok, Req1, State}.
%% @doc Retrieves the list of allowed methods from Trails metadata.
%% Parses the metadata associated with this path and returns the
%% corresponding list of endpoints.
%% @see cowboy_rest:allowed_methods/2
-spec allowed_methods(cowboy_req:req(), state()) ->
{[binary()], cowboy_req:req(), state()}.
allowed_methods(Req, State) ->
#{path := Path} = sr_state:opts(State),
Trail = trails:retrieve(Path),
Metadata = trails:metadata(Trail),
Methods = [atom_to_method(Method) || Method <- maps:keys(Metadata)],
{Methods, Req, State}.
%% @doc Returns <code>false</code> for POST, <code>true</code> otherwise.
%% @see cowboy_rest:resource_exists/2
-spec resource_exists(cowboy_req:req(), state()) ->
{boolean(), cowboy_req:req(), state()}.
resource_exists(Req, State) ->
{Method, Req1} = cowboy_req:method(Req),
{Method =/= <<"POST">>, Req1, State}.
%% @doc Always returns "application/json *" with <code>handle_post</code>.
%% @see cowboy_rest:content_types_accepted/2
%% @todo Use swagger's 'consumes' to auto-generate this if possible
%% <a href="https://github.com/inaka/sumo_rest/issues/7">Issue</a>
-spec content_types_accepted(cowboy_req:req(), state()) ->
{[{{binary(), binary(), '*'}, atom()}], cowboy_req:req(), state()}.
content_types_accepted(Req, State) ->
#{path := Path} = sr_state:opts(State),
{Method, Req2} = cowboy_req:method(Req),
try
Trail = trails:retrieve(Path),
Metadata = trails:metadata(Trail),
AtomMethod = method_to_atom(Method),
#{AtomMethod := #{consumes := Consumes}} = Metadata,
Handler = compose_handler_name(AtomMethod),
RetList = [{iolist_to_binary(X), Handler} || X <- Consumes],
{RetList, Req2, State}
catch
_:_ -> {[{{<<"application">>, <<"json">>, '*'}, handle_post}], Req, State}
end.
%% @doc Always returns "application/json" with <code>handle_get</code>.
%% @see cowboy_rest:content_types_provided/2
%% @todo Use swagger's 'produces' to auto-generate this if possible
%% <a href="https://github.com/inaka/sumo_rest/issues/7">Issue</a>
-spec content_types_provided(cowboy_req:req(), state()) ->
{[{binary(), atom()}], cowboy_req:req(), state()}.
content_types_provided(Req, State) ->
#{path := Path} = sr_state:opts(State),
{Method, Req2} = cowboy_req:method(Req),
try
Trail = trails:retrieve(Path),
Metadata = trails:metadata(Trail),
AtomMethod = method_to_atom(Method),
#{AtomMethod := #{produces := Produces}} = Metadata,
Handler = compose_handler_name(AtomMethod),
RetList = [{iolist_to_binary(X), Handler} || X <- Produces],
{RetList, Req2, State}
catch
_:_ -> {[{<<"application/json">>, handle_get}], Req, State}
end.
%% @doc Returns the list of all entities.
%% Fetches the entities from <strong>SumoDB</strong> using the
%% <code>model</code> provided in the options.
%% @todo Use query-string as filters.
%% <a href="https://github.com/inaka/sumo_rest/issues/8">Issue</a>
-spec handle_get(cowboy_req:req(), state()) ->
{iodata(), cowboy_req:req(), state()}.
handle_get(Req, State) ->
#{model := Model} = sr_state:opts(State),
Module = sr_state:module(State),
{Qs, Req1} = cowboy_req:qs_vals(Req),
Conditions = [ {binary_to_atom(Name, unicode),
Value} || {Name, Value} <- Qs ],
Schema = sumo_internal:get_schema(Model),
Fields = [ sumo_internal:field_name(Field) ||
Field <- sumo_internal:schema_fields(Schema) ],
CompareFun = fun({Name, _}) ->
true =:= lists:member(Name, Fields)
end,
ValidConditions = lists:filter(CompareFun, Conditions),
Entities = case ValidConditions of
[] -> sumo:find_all(Model);
_ -> sumo:find_by(Model, Conditions)
end,
Reply = [Module:to_json(Entity) || Entity <- Entities],
JSON = sr_json:encode(Reply),
{JSON, Req1, State}.
%% @doc Creates a new entity.
%% To parse the body, it uses <code>from_ctx</code> or
%% <code>from_json/2</code> from the
%% <code>model</code> provided in the options.
-spec handle_post(cowboy_req:req(), state()) ->
{{true, binary()} | false | halt, cowboy_req:req(), state()}.
handle_post(Req, State) ->
Module = sr_state:module(State),
try
{SrRequest, Req1} = sr_request:from_cowboy(Req),
Result = case erlang:function_exported(Module, from_ctx, 1) of
false ->
Json = sr_request:body(SrRequest),
Module:from_json(Json);
true ->
Context = #{req => SrRequest, state => State},
Module:from_ctx(Context)
end,
case Result of
{error, Reason} ->
Req2 = cowboy_req:set_resp_body(sr_json:error(Reason), Req1),
{false, Req2, State};
{ok, Entity} ->
handle_post(Entity, Req1, State)
end
catch
_:conflict ->
{ok, Req3} =
cowboy_req:reply(422, [], sr_json:error(<<"Duplicated entity">>), Req),
{halt, Req3, State};
_:badjson ->
Req3 =
cowboy_req:set_resp_body(
sr_json:error(<<"Malformed JSON request">>), Req),
{false, Req3, State}
end.
%% @doc Persists a new entity.
%% The body must have been parsed beforehand.
-spec handle_post(sumo:user_doc(), cowboy_req:req(), state()) ->
{{true, binary()}, cowboy_req:req(), state()}.
handle_post(Entity, Req1, State) ->
#{model := Model, path := Path} = sr_state:opts(State),
Module = sr_state:module(State),
case erlang:function_exported(Module, duplication_conditions, 1) of
false -> proceed;
true ->
Conditions = Module:duplication_conditions(Entity),
case sumo:find_one(Model, Conditions) of
notfound -> proceed;
Duplicate ->
error_logger:warning_msg( "Duplicated ~p with conditions ~p: ~p"
, [Model, Conditions, Duplicate]),
throw(conflict)
end
end,
PersistedEntity = sumo:persist(Model, Entity),
ResBody = sr_json:encode(Module:to_json(PersistedEntity)),
Req2 = cowboy_req:set_resp_body(ResBody, Req1),
Location = Module:location(PersistedEntity, Path),
{{true, Location}, Req2, State}.
%% @doc Announces the Req.
%% If <code>verbose := true</code> in <code>Opts</code> for this handler
%% prints out a line indicating that endpoint that was hit.
%% @see cowboy_rest:rest_init/2
-spec announce_req(cowboy_req:req(), options()) -> cowboy_req:req().
announce_req(Req, #{verbose := true}) ->
{Method, Req1} = cowboy_req:method(Req),
{Path, Req2} = cowboy_req:path(Req1),
_ = error_logger:info_msg("~s ~s", [Method, Path]),
Req2;
announce_req(Req, _Opts) -> Req.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Auxiliary Functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec atom_to_method(get|patch|put|post|delete) -> binary().
atom_to_method(get) -> <<"GET">>;
atom_to_method(patch) -> <<"PATCH">>;
atom_to_method(put) -> <<"PUT">>;
atom_to_method(post) -> <<"POST">>;
atom_to_method(delete) -> <<"DELETE">>.
-spec method_to_atom(binary()) -> atom().
method_to_atom(<<"GET">>) -> get;
method_to_atom(<<"PATCH">>) -> patch;
method_to_atom(<<"PUT">>) -> put;
method_to_atom(<<"POST">>) -> post;
method_to_atom(<<"DELETE">>) -> delete.
-spec compose_handler_name(get|patch|put|post) -> atom().
compose_handler_name(get) -> handle_get;
compose_handler_name(put) -> handle_put;
compose_handler_name(patch) -> handle_patch;
compose_handler_name(post) -> handle_post.