0 | module HTTP.API.Server.Method
 1 |
 2 | import HTTP.API.Server.Interface
 3 |
 4 | %default total
 5 |
 6 | checkResponseTypes : All (EncodeVia t) ts -> Request -> Handler ()
 7 | checkResponseTypes a r =
 8 |  let mts := forget $ mapProperty (\x => mediaType @{x}) a
 9 |   in case any (acceptsMedia r.headers) mts of
10 |        True => pure ()
11 |        False =>
12 |          throw $
13 |            requestErrDetails
14 |              "I provide: \{show mts}; Request accepts: \{show $ accept r.headers}"
15 |              unsupportedMediaType415
16 |
17 | public export
18 | Serve ReqMethod where
19 |   InTypes m = []
20 |
21 |   OutTypes m = MethodResults m
22 |
23 |   Constraint (M _ _ _ Nothing0)   = ()
24 |   Constraint (M _ _ fs $ Just0 t) = All (EncodeVia t) fs
25 |
26 |   outs (M _ _ _ Nothing0)  = []
27 |   outs (M _ _ _ $ Just0 t) = [t]
28 |
29 |   canHandle (M m _ _ _) r = m == r.method
30 |   fromRequest m r = pure []
31 |   adjResponse (M _ s _ Nothing0)  _   req resp = pure (setStatus s resp)
32 |   adjResponse (M _ s _ $ Just0 _) [v] req resp = do
33 |     checkResponseTypes con req
34 |     pure $ encodeBody s v req.headers con resp
35 |