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