0 | module HTTP.API.Server.Content
 1 |
 2 | import HTTP.API.Server.Interface
 3 |
 4 | %default total
 5 |
 6 | parameters {auto loc : HTTPLocal}
 7 |   decodeBody : All (`DecodeVia` t) ts -> Request -> Either RequestErr (HList [t])
 8 |   decodeBody []        r = Left $ requestErr unsupportedMediaType415
 9 |   decodeBody (d :: ds) r =
10 |     case contentType r.headers of
11 |       Nothing         => decodeBody ds r
12 |       Just (CT mt ps) => case mt == mediaType @{d} of
13 |         False => decodeBody ds r
14 |         True  =>
15 |           bimap
16 |             (decodeErr badRequest400)
17 |             (\x => [x])
18 |             (decodeVia @{d} ps r.content)
19 |
20 |   public export
21 |   Serve ReqContent where
22 |     Constraint b         = All (`DecodeVia` b.result) b.formats
23 |     InTypes  b           = [b.result]
24 |     OutTypes _           = []
25 |     outs     _           = %search
26 |     canHandle   _ r      = True
27 |     fromRequest _ r      = injectEither $ decodeBody con r
28 |     adjResponse _ [] req = pure
29 |