-
Notifications
You must be signed in to change notification settings - Fork 10
/
sr_request.erl
55 lines (45 loc) · 1.54 KB
/
sr_request.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
-module(sr_request).
-export([ from_cowboy/1
, body/1
, headers/1
, path/1
, bindings/1
]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Types
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-opaque req() ::
#{ body := sr_json:json()
, headers := [{binary(), iodata()}]
, path := binary()
, bindings := #{atom() => any()}
}.
-export_type [req/0].
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Constructor, getters/setters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec from_cowboy(cowboy_req:req()) -> {req(), cowboy_req:req()}.
from_cowboy(CowboyReq) ->
{ok, RawBody, CowboyReq1} = cowboy_req:body(CowboyReq),
Body = sr_json:decode(RawBody),
{Headers, CowboyReq2} = cowboy_req:headers(CowboyReq1),
{Path, CowboyReq3} = cowboy_req:path(CowboyReq2),
{BindingsList, CowboyReq4} = cowboy_req:bindings(CowboyReq3),
Request = #{ body => Body
, headers => Headers
, path => Path
, bindings => maps:from_list(BindingsList)
},
{Request, CowboyReq4}.
-spec body(req()) -> sr_json:json() | undefined.
body(#{body := Body}) ->
Body.
-spec headers(req()) -> [{binary(), iodata()}].
headers(#{headers := Headers}) ->
Headers.
-spec path(req()) -> binary().
path(#{path := Path}) ->
Path.
-spec bindings(req()) -> #{atom() => any()}.
bindings(#{bindings := Bindings}) ->
Bindings.