@@ -8,6 +8,7 @@
88{-# LANGUAGE MultiParamTypeClasses #-}
99{-# LANGUAGE OverloadedStrings #-}
1010{-# LANGUAGE PolyKinds #-}
11+{-# LANGUAGE Rank2Types #-}
1112{-# LANGUAGE ScopedTypeVariables #-}
1213{-# LANGUAGE TupleSections #-}
1314{-# LANGUAGE TypeFamilies #-}
@@ -25,7 +26,9 @@
2526module Servant.Reflex
2627 ( client
2728 , clientWithOpts
29+ , clientWithOptsAndResultHandler
2830 , clientWithRoute
31+ , clientWithRouteAndResultHandler
2932 , BuildHeaderKeysTo(..)
3033 , toHeaders
3134 , HasClient
@@ -36,34 +39,36 @@ module Servant.Reflex
3639
3740------------------------------------------------------------------------------
3841import Control.Applicative
39−import Data.Monoid ((<>))
40−import qualified Data.Set as Set
41−import qualified Data.Text.Encoding as E
42−import Data.CaseInsensitive (mk)
42+import qualified Data.CaseInsensitive as CI
4343import Data.Functor.Identity
44−import Data.Proxy (Proxy (..))
4544import qualified Data.Map as Map
45+import Data.Monoid ((<>))
46+import Data.Proxy (Proxy (..))
47+import qualified Data.Set as Set
4648import Data.Text (Text)
4749import qualified Data.Text as T
50+import qualified Data.Text.Encoding as E
4851import GHC.Exts (Constraint)
4952import GHC.TypeLits (KnownSymbol, symbolVal)
50−import Servant.API ((:<|>)(..),(:>), BasicAuth,
51− BasicAuthData, BuildHeadersTo(..),
52− Capture, contentType, Header,
53− Headers(..), HttpVersion, IsSecure,
54− MimeRender(..), MimeUnrender,
53+import Servant.API ((:<|>) (..), (:>), BasicAuth,
54+ BasicAuthData, BuildHeadersTo (..),
55+ Capture, Header, Headers (..),
56+ HttpVersion, IsSecure,
57+ MimeRender (..), MimeUnrender,
5558 NoContent, QueryFlag, QueryParam,
56− QueryParams, Raw, ReflectMethod(..),
59+ QueryParams, Raw, ReflectMethod (..),
5760 RemoteHost, ReqBody,
58− ToHttpApiData(..), Vault, Verb)
61+ ToHttpApiData (..), Vault, Verb,
62+ contentType)
63+import Servant.API.Description (Summary)
5964import qualified Servant.Auth as Auth
6065
6166import Reflex.Dom.Core (Dynamic, Event, Reflex,
62− XhrRequest(..),
63− XhrResponseHeaders(..),
64− XhrResponse(..), attachPromptlyDynWith, constDyn, ffor, fmapMaybe,
65− leftmost, performRequestsAsync,
66− )
67+ XhrRequest (..), XhrResponse (..),
68+ XhrResponseHeaders (..),
69+ attachPromptlyDynWith, constDyn, ffor,
70+ fmapMaybe, leftmost,
71+ performRequestsAsync)
6772------------------------------------------------------------------------------
6873import Servant.Common.BaseUrl (BaseUrl(..), Scheme(..), baseUrlWidget,
6974 showBaseUrl,
@@ -98,7 +103,7 @@ import Servant.Common.Req (ClientOptions(..),
98103-- >
99104-- > getAllBooks :: Event t l -> m (Event t (l, ReqResult [Book]))
100105-- > postNewBook :: Dynamic t (Maybe Book) -> Event t l
101−-- -> m (Event t (l, ReqResult Book)))
106+-- > -> m (Event t (l, ReqResult Book)))
102107-- > (getAllBooks :<|> postNewBook) = client myApi host
103108-- > where host = constDyn $ BaseUrl Http "localhost" 8080
104109client
@@ -120,21 +125,53 @@ clientWithOpts
120125 -> Client t m layout tag
121126clientWithOpts p q t baseurl = clientWithRoute p q t defReq baseurl
122127
128+-- | Like 'clientWithOpts' but allows passing a function which will process the
129+-- result event in some way. This can be used to handle errors in a uniform way
130+-- across call sites.
131+clientWithOptsAndResultHandler
132+ :: (HasClient t m layout tag)
133+ => Proxy layout
134+ -> Proxy m
135+ -> Proxy tag
136+ -> Dynamic t BaseUrl
137+ -> ClientOptions
138+ -> (forall a. Event t (ReqResult tag a) -> m (Event t (ReqResult tag a)))
139+ -> Client t m layout tag
140+clientWithOptsAndResultHandler p q t = clientWithRouteAndResultHandler p q t defReq
141+
123142
124143-- | This class lets us define how each API combinator
125144-- influences the creation of an HTTP request. It's mostly
126145-- an internal class, you can just use 'client'.
127−class HasClient t m layout (tag :: *) where
146+class Monad m => HasClient t m layout (tag :: *) where
128147 type Client t m layout tag :: *
129− clientWithRoute :: Proxy layout -> Proxy m -> Proxy tag -> Req t -> Dynamic t BaseUrl -> ClientOptions -> Client t m layout tag
148+ clientWithRoute
149+ :: Proxy layout
150+ -> Proxy m
151+ -> Proxy tag
152+ -> Req t
153+ -> Dynamic t BaseUrl
154+ -> ClientOptions
155+ -> Client t m layout tag
156+ clientWithRoute l m t r b o = clientWithRouteAndResultHandler l m t r b o return
157+
158+ clientWithRouteAndResultHandler
159+ :: Proxy layout
160+ -> Proxy m
161+ -> Proxy tag
162+ -> Req t
163+ -> Dynamic t BaseUrl
164+ -> ClientOptions
165+ -> (forall a. Event t (ReqResult tag a) -> m (Event t (ReqResult tag a)))
166+ -> Client t m layout tag
130167
131168
132169instance (HasClient t m a tag, HasClient t m b tag) => HasClient t m (a :<|> b) tag where
133170 type Client t m (a :<|> b) tag = Client t m a tag :<|> Client t m b tag
134171
135− clientWithRoute Proxy q pTag req baseurl opts =
136− clientWithRoute (Proxy :: Proxy a) q pTag req baseurl opts :<|>
137− clientWithRoute (Proxy :: Proxy b) q pTag req baseurl opts
172+ clientWithRouteAndResultHandler Proxy q pTag req baseurl opts wrap =
173+ clientWithRouteAndResultHandler (Proxy :: Proxy a) q pTag req baseurl opts wrap :<|>
174+ clientWithRouteAndResultHandler (Proxy :: Proxy b) q pTag req baseurl opts wrap
138175
139176
140177-- Capture. Example:
@@ -155,11 +192,9 @@ instance (SupportsServantReflex t m, ToHttpApiData a, HasClient t m sublayout ta
155192 type Client t m (Capture capture a :> sublayout) tag =
156193 Dynamic t (Either Text a) -> Client t m sublayout tag
157194
158− clientWithRoute Proxy q t req baseurl opts val =
159− clientWithRoute (Proxy :: Proxy sublayout)
160− q t
161− (prependToPathParts p req)
162− baseurl opts
195+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap val =
196+ clientWithRouteAndResultHandler
197+ (Proxy :: Proxy sublayout) q t (prependToPathParts p req) baseurl opts wrap
163198 where p = (fmap . fmap) (toUrlPiece) val
164199
165200
@@ -172,8 +207,8 @@ instance {-# OVERLAPPABLE #-}
172207 Event t tag -> m (Event t (ReqResult tag a))
173208 -- TODO how to access input types here?
174209 -- ExceptT ServantError IO a
175− clientWithRoute Proxy _ _ req baseurl opts trigs =
176− fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity $ req') baseurl opts trigs
210+ clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs =
211+ wrap =<< fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity $ req') baseurl opts trigs
177212 where method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
178213 req' = req { reqMethod = method }
179214
@@ -186,29 +221,33 @@ instance {-# OVERLAPPING #-}
186221 Event t tag -> m (Event t (ReqResult tag NoContent))
187222 -- TODO: how to access input types here?
188223 -- ExceptT ServantError IO NoContent
189− clientWithRoute Proxy _ _ req baseurl opts =
190− (fmap . fmap) runIdentity . performRequestsNoBody method (constDyn $ Identity req) baseurl opts
224+ clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs =
225+ wrap =<< fmap runIdentity <$> performRequestsNoBody method (constDyn $ Identity req) baseurl opts trigs
191226 where method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
192227
193228
194229toHeaders :: BuildHeadersTo ls => ReqResult tag a -> ReqResult tag (Headers ls a)
195230toHeaders r =
196− let toBS = E.encodeUtf8
197− hdrs = maybe []
198− (\xhr -> fmap (\(h,v) -> (mk (toBS h), toBS v))
231+ let hdrs = maybe []
232+ (\xhr -> fmap (\(h,v) -> (CI.map E.encodeUtf8 h, E.encodeUtf8 v))
199233 (Map.toList $ _xhrResponse_headers xhr))
200234 (response r)
201235 in ffor r $ \a -> Headers {getResponse = a ,getHeadersHList = buildHeadersTo hdrs}
202236
237+
203238class BuildHeaderKeysTo hs where
204− buildHeaderKeysTo :: Proxy hs -> [T.Text]
239+ buildHeaderKeysTo :: Proxy hs -> [CI.CI T.Text]
205240
206241instance {-# OVERLAPPABLE #-} BuildHeaderKeysTo '[]
207242 where buildHeaderKeysTo _ = []
208243
209244instance {-# OVERLAPPABLE #-} (BuildHeaderKeysTo xs, KnownSymbol h)
210245 => BuildHeaderKeysTo ((Header h v) ': xs) where
211− buildHeaderKeysTo _ = T.pack (symbolVal (Proxy :: Proxy h)) : buildHeaderKeysTo (Proxy :: Proxy xs)
246+ buildHeaderKeysTo _ =
247+ let
248+ thisKey = CI.mk $ T.pack (symbolVal (Proxy :: Proxy h))
249+ in thisKey : buildHeaderKeysTo (Proxy :: Proxy xs)
250+
212251
213252-- HEADERS Verb (Content) --
214253-- Headers combinator not treated in fully general case,
@@ -223,10 +262,10 @@ instance {-# OVERLAPPABLE #-}
223262 ) => HasClient t m (Verb method status cts' (Headers ls a)) tag where
224263 type Client t m (Verb method status cts' (Headers ls a)) tag =
225264 Event t tag -> m (Event t (ReqResult tag (Headers ls a)))
226− clientWithRoute Proxy _ _ req baseurl opts trigs = do
265+ clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs = do
227266 let method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
228267 resp <- fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity req') baseurl opts trigs
229− return $ toHeaders <$> resp
268+ wrap $ toHeaders <$> resp
230269 where req' = req { respHeaders =
231270 OnlyHeaders (Set.fromList (buildHeaderKeysTo (Proxy :: Proxy ls)))
232271 }
@@ -239,10 +278,10 @@ instance {-# OVERLAPPABLE #-}
239278 ) => HasClient t m (Verb method status cts (Headers ls NoContent)) tag where
240279 type Client t m (Verb method status cts (Headers ls NoContent)) tag
241280 = Event t tag -> m (Event t (ReqResult tag (Headers ls NoContent)))
242− clientWithRoute Proxy _ _ req baseurl opts trigs = do
281+ clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs = do
243282 let method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
244283 resp <- fmap runIdentity <$> performRequestsNoBody method (constDyn $ Identity req') baseurl opts trigs
245− return $ toHeaders <$> resp
284+ wrap $ toHeaders <$> resp
246285 where req' = req {respHeaders =
247286 OnlyHeaders (Set.fromList (buildHeaderKeysTo (Proxy :: Proxy ls)))
248287 }
@@ -269,11 +308,11 @@ instance (KnownSymbol sym, ToHttpApiData a,
269308 type Client t m (Header sym a :> sublayout) tag =
270309 Dynamic t (Either Text a) -> Client t m sublayout tag
271310
272− clientWithRoute Proxy q t req baseurl opts eVal =
273− clientWithRoute (Proxy :: Proxy sublayout)
274− q t
275− (Servant.Common.Req.addHeader hname eVal req)
276− baseurl opts
311+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap eVal =
312+ clientWithRouteAndResultHandler
313+ (Proxy :: Proxy sublayout) q t
314+ (Servant.Common.Req.addHeader hname eVal req)
315+ baseurl opts wrap
277316 where hname = T.pack $ symbolVal (Proxy :: Proxy sym)
278317
279318
@@ -287,8 +326,21 @@ instance HasClient t m sublayout tag
287326 type Client t m (HttpVersion :> sublayout) tag =
288327 Client t m sublayout tag
289328
290− clientWithRoute Proxy q t =
291− clientWithRoute (Proxy :: Proxy sublayout) q t
329+ clientWithRouteAndResultHandler Proxy =
330+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout)
331+
332+-- | Using a 'Summary' combinator in your API doesn't affect the client
333+-- functions.
334+instance (HasClient t m sublayout tag, KnownSymbol sym)
335+ => HasClient t m (Summary sym :> sublayout) tag where
336+
337+ type Client t m (Summary sym :> sublayout) tag =
338+ Client t m sublayout tag
339+
340+ clientWithRouteAndResultHandler Proxy =
341+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout)
342+
343+
292344
293345
294346-- | If you use a 'QueryParam' in one of your endpoints in your API,
@@ -324,9 +376,9 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout tag, Reflex
324376 Dynamic t (QParam a) -> Client t m sublayout tag
325377
326378 -- if mparam = Nothing, we don't add it to the query string
327− clientWithRoute Proxy q t req baseurl opts mparam =
328− clientWithRoute (Proxy :: Proxy sublayout) q t
329− (req {qParams = paramPair : qParams req}) baseurl opts
379+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap mparam =
380+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t
381+ (req {qParams = paramPair : qParams req}) baseurl opts wrap
330382
331383 where pname = symbolVal (Proxy :: Proxy sym)
332384 --p prm = QueryPartParam $ (fmap . fmap) (toQueryParam) prm
@@ -370,8 +422,8 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout tag, Reflex
370422 type Client t m (QueryParams sym a :> sublayout) tag =
371423 Dynamic t [a] -> Client t m sublayout tag
372424
373− clientWithRoute Proxy q t req baseurl opts paramlist =
374− clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl opts
425+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap paramlist =
426+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t req' baseurl opts wrap
375427
376428 where req' = req { qParams = (T.pack pname, params') : qParams req }
377429 pname = symbolVal (Proxy :: Proxy sym)
@@ -410,8 +462,8 @@ instance (KnownSymbol sym, HasClient t m sublayout tag, Reflex t)
410462 type Client t m (QueryFlag sym :> sublayout) tag =
411463 Dynamic t Bool -> Client t m sublayout tag
412464
413− clientWithRoute Proxy q t req baseurl opts flag =
414− clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl opts
465+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap flag =
466+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t req' baseurl opts wrap
415467
416468 where req' = req { qParams = thisPair : qParams req }
417469 thisPair = (T.pack pName, QueryPartFlag flag) :: (Text, QueryPart t)
@@ -425,7 +477,7 @@ instance SupportsServantReflex t m => HasClient t m Raw tag where
425477 -> Event t tag
426478 -> m (Event t (ReqResult tag ()))
427479
428− clientWithRoute _ _ _ _ baseurl _ xhrs triggers = do
480+ clientWithRouteAndResultHandler _ _ _ _ baseurl _ wrap xhrs triggers = do
429481
430482 let xhrs' = liftA2 (\x path -> case x of
431483 Left e -> Left e
@@ -436,7 +488,7 @@ instance SupportsServantReflex t m => HasClient t m Raw tag where
436488 okReq = fmapMaybe (\(t,x) -> either (const Nothing) (Just . (t,)) x) xhrs'' :: Event t (tag, XhrRequest ())
437489
438490 resps <- performRequestsAsync okReq
439− return $ leftmost [ uncurry RequestFailure <$> badReq
491+ wrap $ leftmost [ uncurry RequestFailure <$> badReq
440492 , evalResponse (const $ Right ()) <$> resps
441493 ]
442494
@@ -467,8 +519,8 @@ instance (MimeRender ct a, HasClient t m sublayout tag, Reflex t)
467519 type Client t m (ReqBody (ct ': cts) a :> sublayout) tag =
468520 Dynamic t (Either Text a) -> Client t m sublayout tag
469521
470− clientWithRoute Proxy q t req baseurl opts body =
471− clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl opts
522+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap body =
523+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t req' baseurl opts wrap
472524 where req' = req { reqBody = bodyBytesCT }
473525 ctProxy = Proxy :: Proxy ct
474526 ctString = T.pack $ show $ contentType ctProxy
@@ -482,9 +534,9 @@ instance (MimeRender ct a, HasClient t m sublayout tag, Reflex t)
482534instance (KnownSymbol path, HasClient t m sublayout tag, Reflex t) => HasClient t m (path :> sublayout) tag where
483535 type Client t m (path :> sublayout) tag = Client t m sublayout tag
484536
485− clientWithRoute Proxy q t req baseurl opts =
486− clientWithRoute (Proxy :: Proxy sublayout) q t
487− (prependToPathParts (pure (Right $ T.pack p)) req) baseurl opts
537+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap =
538+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t
539+ (prependToPathParts (pure (Right $ T.pack p)) req) baseurl opts wrap
488540
489541 where p = symbolVal (Proxy :: Proxy path)
490542
@@ -492,23 +544,23 @@ instance (KnownSymbol path, HasClient t m sublayout tag, Reflex t) => HasClient
492544instance HasClient t m api tag => HasClient t m (Vault :> api) tag where
493545 type Client t m (Vault :> api) tag = Client t m api tag
494546
495− clientWithRoute Proxy q t req baseurl =
496− clientWithRoute (Proxy :: Proxy api) q t req baseurl
547+ clientWithRouteAndResultHandler Proxy =
548+ clientWithRouteAndResultHandler (Proxy :: Proxy api)
497549
498550
499551instance HasClient t m api tag => HasClient t m (RemoteHost :> api) tag where
500552 type Client t m (RemoteHost :> api) tag = Client t m api tag
501553
502− clientWithRoute Proxy q t req baseurl =
503− clientWithRoute (Proxy :: Proxy api) q t req baseurl
554+ clientWithRouteAndResultHandler Proxy =
555+ clientWithRouteAndResultHandler (Proxy :: Proxy api)
504556
505557
506558
507559instance HasClient t m api tag => HasClient t m (IsSecure :> api) tag where
508560 type Client t m (IsSecure :> api) tag = Client t m api tag
509561
510− clientWithRoute Proxy q t req baseurl =
511− clientWithRoute (Proxy :: Proxy api) q t req baseurl
562+ clientWithRouteAndResultHandler Proxy =
563+ clientWithRouteAndResultHandler (Proxy :: Proxy api)
512564
513565
514566instance (HasClient t m api tag, Reflex t)
@@ -517,8 +569,8 @@ instance (HasClient t m api tag, Reflex t)
517569 type Client t m (BasicAuth realm usr :> api) tag = Dynamic t (Maybe BasicAuthData)
518570 -> Client t m api tag
519571
520− clientWithRoute Proxy q t req baseurl opts authdata =
521− clientWithRoute (Proxy :: Proxy api) q t req' baseurl opts
572+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap authdata =
573+ clientWithRouteAndResultHandler (Proxy :: Proxy api) q t req' baseurl opts wrap
522574 where
523575 req' = req { authData = Just authdata }
524576
@@ -561,7 +613,7 @@ for empty and one for non-empty lists).
561613instance (HasCookieAuth auths, HasClient t m api tag) => HasClient t m (Auth.Auth auths a :> api) tag where
562614
563615 type Client t m (Auth.Auth auths a :> api) tag = Client t m api tag
564− clientWithRoute Proxy = clientWithRoute (Proxy :: Proxy api)
616+ clientWithRouteAndResultHandler Proxy = clientWithRouteAndResultHandler (Proxy :: Proxy api)
565617
566618
567619type family HasCookieAuth xs :: Constraint where