1
- % %% @doc Base GET|POST /[entity]s implementation
1
+ % %% @doc Base GET|POST /[entities] implementation
2
2
-module (sr_entities_handler ).
3
3
4
4
-export ([ init /3
25
25
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
26
26
% %% Cowboy Callbacks
27
27
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
28
-
28
+ % % @doc Upgrades to cowboy_rest.
29
+ % % Basically, just returns <code>{upgrade, protocol, cowboy_rest}</code>
30
+ % % @see cowboy_rest:init/3
29
31
-spec init ({atom (), atom ()}, cowboy_req :req (), options ()) ->
30
32
{upgrade , protocol , cowboy_rest }.
31
33
init (_Transport , _Req , _Opts ) ->
32
34
{upgrade , protocol , cowboy_rest }.
33
35
36
+ % % @doc Announces the Req and moves on.
37
+ % % If <code>verbose := true</code> in <code>Opts</code> for this handler
38
+ % % prints out a line indicating that endpoint that was hit.
39
+ % % @see cowboy_rest:rest_init/2
34
40
-spec rest_init (cowboy_req :req (), options ()) ->
35
41
{ok , cowboy_req :req (), state ()}.
36
42
rest_init (Req , Opts ) ->
37
43
Req1 = announce_req (Req , Opts ),
38
44
{ok , Req1 , #{opts => Opts }}.
39
45
46
+ % % @doc Retrieves the list of allowed methods from Trails metadata.
47
+ % % Parses the metadata associated with this path and returns the
48
+ % % corresponding list of endpoints.
49
+ % % @see cowboy_rest:allowed_methods/2
40
50
-spec allowed_methods (cowboy_req :req (), state ()) ->
41
51
{[binary ()], cowboy_req :req (), state ()}.
42
52
allowed_methods (Req , State ) ->
@@ -45,26 +55,37 @@ allowed_methods(Req, State) ->
45
55
Methods = [atom_to_method (Method ) || Method <- maps :keys (Metadata )],
46
56
{Methods , Req , State }.
47
57
58
+ % % @doc Returns <code>false</code> for POST, <code>true</code> otherwise.
59
+ % % @see cowboy_rest:resource_exists/2
48
60
-spec resource_exists (cowboy_req :req (), state ()) ->
49
61
{boolean (), cowboy_req :req (), state ()}.
50
62
resource_exists (Req , State ) ->
51
63
{Method , Req1 } = cowboy_req :method (Req ),
52
64
{Method =/= <<" POST" >>, Req1 , State }.
53
65
66
+ % % @doc Always returns "application/json *" with <code>handle_post</code>.
67
+ % % @see cowboy_rest:content_types_accepted/2
54
68
% % @todo Use swagger's 'consumes' to auto-generate this if possible
55
- % % @see https://github.com/inaka/sumo_rest/issues/7
69
+ % % <a href=" https://github.com/inaka/sumo_rest/issues/7">Issue</a>
56
70
-spec content_types_accepted (cowboy_req :req (), state ()) ->
57
71
{[{{binary (), binary (), '*' }, atom ()}], cowboy_req :req (), state ()}.
58
72
content_types_accepted (Req , State ) ->
59
73
{[{{<<" application" >>, <<" json" >>, '*' }, handle_post }], Req , State }.
60
74
75
+ % % @doc Always returns "application/json" with <code>handle_get</code>.
76
+ % % @see cowboy_rest:content_types_provided/2
61
77
% % @todo Use swagger's 'produces' to auto-generate this if possible
62
- % % @see https://github.com/inaka/sumo_rest/issues/7
78
+ % % <a href=" https://github.com/inaka/sumo_rest/issues/7">Issue</a>
63
79
-spec content_types_provided (cowboy_req :req (), state ()) ->
64
80
{[{binary (), atom ()}], cowboy_req :req (), state ()}.
65
81
content_types_provided (Req , State ) ->
66
82
{[{<<" application/json" >>, handle_get }], Req , State }.
67
83
84
+ % % @doc Returns the list of all entities.
85
+ % % Fetches the entities from <strong>SumoDB</strong> using the
86
+ % % <code>model</code> provided in the options.
87
+ % % @todo Use query-string as filters.
88
+ % % <a href="https://github.com/inaka/sumo_rest/issues/8">Issue</a>
68
89
-spec handle_get (cowboy_req :req (), state ()) ->
69
90
{iodata (), cowboy_req :req (), state ()}.
70
91
handle_get (Req , State ) ->
@@ -74,6 +95,9 @@ handle_get(Req, State) ->
74
95
JSON = sr_json :encode (Reply ),
75
96
{JSON , Req , State }.
76
97
98
+ % % @doc Creates a new entity.
99
+ % % To parse the body, it uses <code>from_json/2</code> from the
100
+ % % <code>model</code> provided in the options.
77
101
-spec handle_post (cowboy_req :req (), state ()) ->
78
102
{{true , binary ()} | false | halt , cowboy_req :req (), state ()}.
79
103
handle_post (Req , State ) ->
@@ -100,6 +124,8 @@ handle_post(Req, State) ->
100
124
{false , Req3 , State }
101
125
end .
102
126
127
+ % % @doc Persists a new entity.
128
+ % % The body must have been parsed beforehand.
103
129
-spec handle_post (sumo :user_doc (), cowboy_req :req (), state ()) ->
104
130
{{true , binary ()}, cowboy_req :req (), state ()}.
105
131
handle_post (Entity , Req1 , State ) ->
@@ -122,6 +148,10 @@ handle_post(Entity, Req1, State) ->
122
148
Location = iolist_to_binary ([Path , $/ , Model :uri_path (PersistedEntity )]),
123
149
{{true , Location }, Req2 , State }.
124
150
151
+ % % @doc Announces the Req.
152
+ % % If <code>verbose := true</code> in <code>Opts</code> for this handler
153
+ % % prints out a line indicating that endpoint that was hit.
154
+ % % @see cowboy_rest:rest_init/2
125
155
-spec announce_req (cowboy_req :req (), options ()) -> cowboy_req :req ().
126
156
announce_req (Req , #{verbose := true }) ->
127
157
{Method , Req1 } = cowboy_req :method (Req ),
0 commit comments