@@ -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,46 +26,49 @@
2526module Servant.Reflex
2627 ( client
2728 , clientWithOpts
29+ , clientWithOptsAndResultHandler
2830 , clientWithRoute
31+ , clientWithRouteAndResultHandler
2932 , BuildHeaderKeysTo(..)
3033 , toHeaders
3134 , HasClient
3235 , Client
33− , withCredentials
3436 , module Servant.Common.Req
3537 , module Servant.Common.BaseUrl
3638 ) where
3739
3840------------------------------------------------------------------------------
3941import Control.Applicative
40−import Data.Monoid ((<>))
41−import qualified Data.Set as Set
42−import qualified Data.Text.Encoding as E
43−import Data.CaseInsensitive (mk)
42+import qualified Data.CaseInsensitive as CI
4443import Data.Functor.Identity
45−import Data.Proxy (Proxy (..))
4644import qualified Data.Map as Map
45+import Data.Monoid ((<>))
46+import Data.Proxy (Proxy (..))
47+import qualified Data.Set as Set
4748import Data.Text (Text)
4849import qualified Data.Text as T
50+import qualified Data.Text.Encoding as E
4951import GHC.Exts (Constraint)
5052import GHC.TypeLits (KnownSymbol, symbolVal)
51−import Servant.API ((:<|>)(..),(:>), BasicAuth,
52− BasicAuthData, BuildHeadersTo(..),
53− Capture, contentType, Header,
54− Headers(..), HttpVersion, IsSecure,
55− MimeRender(..), MimeUnrender,
56− NoContent, QueryFlag, QueryParam,
57− QueryParams, Raw, ReflectMethod(..),
53+import Servant.API ((:<|>) (..), (:>), BasicAuth,
54+ BasicAuthData, BuildHeadersTo (..),
55+ Capture, Header, Headers (..),
56+ HttpVersion, IsSecure,
57+ MimeRender (..), MimeUnrender,
58+ NoContent, QueryFlag, QueryParam',
59+ QueryParams, Raw, ReflectMethod (..),
5860 RemoteHost, ReqBody,
59− ToHttpApiData(..), Vault, Verb)
61+ ToHttpApiData (..), Vault, Verb,
62+ contentType)
63+import Servant.API.Description (Summary)
6064import qualified Servant.Auth as Auth
6165
62−import Reflex.Dom (Dynamic, Event, Reflex,
63− XhrRequest(..),
64− XhrResponseHeaders(..),
65− XhrResponse(..), attachPromptlyDynWith, constDyn, ffor, fmapMaybe,
66− leftmost, performRequestsAsync,
67− )
66+import Reflex.Dom.Core (Dynamic, Event, Reflex,
67+ XhrRequest (..), XhrResponse (..),
68+ XhrResponseHeaders (..),
69+ attachPromptlyDynWith, constDyn, ffor,
70+ fmapMaybe, leftmost,
71+ performRequestsAsync)
6872------------------------------------------------------------------------------
6973import Servant.Common.BaseUrl (BaseUrl(..), Scheme(..), baseUrlWidget,
7074 showBaseUrl,
@@ -99,7 +103,7 @@ import Servant.Common.Req (ClientOptions(..),
99103-- >
100104-- > getAllBooks :: Event t l -> m (Event t (l, ReqResult [Book]))
101105-- > postNewBook :: Dynamic t (Maybe Book) -> Event t l
102−-- -> m (Event t (l, ReqResult Book)))
106+-- > -> m (Event t (l, ReqResult Book)))
103107-- > (getAllBooks :<|> postNewBook) = client myApi host
104108-- > where host = constDyn $ BaseUrl Http "localhost" 8080
105109client
@@ -121,21 +125,53 @@ clientWithOpts
121125 -> Client t m layout tag
122126clientWithOpts p q t baseurl = clientWithRoute p q t defReq baseurl
123127
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+
124142
125143-- | This class lets us define how each API combinator
126144-- influences the creation of an HTTP request. It's mostly
127145-- an internal class, you can just use 'client'.
128−class HasClient t m layout (tag :: *) where
146+class Monad m => HasClient t m layout (tag :: *) where
129147 type Client t m layout tag :: *
130− 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
131167
132168
133169instance (HasClient t m a tag, HasClient t m b tag) => HasClient t m (a :<|> b) tag where
134170 type Client t m (a :<|> b) tag = Client t m a tag :<|> Client t m b tag
135171
136− clientWithRoute Proxy q pTag req baseurl opts =
137− clientWithRoute (Proxy :: Proxy a) q pTag req baseurl opts :<|>
138− 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
139175
140176
141177-- Capture. Example:
@@ -156,11 +192,9 @@ instance (SupportsServantReflex t m, ToHttpApiData a, HasClient t m sublayout ta
156192 type Client t m (Capture capture a :> sublayout) tag =
157193 Dynamic t (Either Text a) -> Client t m sublayout tag
158194
159− clientWithRoute Proxy q t req baseurl opts val =
160− clientWithRoute (Proxy :: Proxy sublayout)
161− q t
162− (prependToPathParts p req)
163− 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
164198 where p = (fmap . fmap) (toUrlPiece) val
165199
166200
@@ -173,8 +207,8 @@ instance {-# OVERLAPPABLE #-}
173207 Event t tag -> m (Event t (ReqResult tag a))
174208 -- TODO how to access input types here?
175209 -- ExceptT ServantError IO a
176− clientWithRoute Proxy _ _ req baseurl opts trigs =
177− 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
178212 where method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
179213 req' = req { reqMethod = method }
180214
@@ -187,29 +221,33 @@ instance {-# OVERLAPPING #-}
187221 Event t tag -> m (Event t (ReqResult tag NoContent))
188222 -- TODO: how to access input types here?
189223 -- ExceptT ServantError IO NoContent
190− clientWithRoute Proxy _ _ req baseurl opts =
191− (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
192226 where method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
193227
194228
195229toHeaders :: BuildHeadersTo ls => ReqResult tag a -> ReqResult tag (Headers ls a)
196230toHeaders r =
197− let toBS = E.encodeUtf8
198− hdrs = maybe []
199− (\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))
200233 (Map.toList $ _xhrResponse_headers xhr))
201234 (response r)
202235 in ffor r $ \a -> Headers {getResponse = a ,getHeadersHList = buildHeadersTo hdrs}
203236
237+
204238class BuildHeaderKeysTo hs where
205− buildHeaderKeysTo :: Proxy hs -> [T.Text]
239+ buildHeaderKeysTo :: Proxy hs -> [CI.CI T.Text]
206240
207241instance {-# OVERLAPPABLE #-} BuildHeaderKeysTo '[]
208242 where buildHeaderKeysTo _ = []
209243
210244instance {-# OVERLAPPABLE #-} (BuildHeaderKeysTo xs, KnownSymbol h)
211245 => BuildHeaderKeysTo ((Header h v) ': xs) where
212− 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+
213251
214252-- HEADERS Verb (Content) --
215253-- Headers combinator not treated in fully general case,
@@ -224,10 +262,10 @@ instance {-# OVERLAPPABLE #-}
224262 ) => HasClient t m (Verb method status cts' (Headers ls a)) tag where
225263 type Client t m (Verb method status cts' (Headers ls a)) tag =
226264 Event t tag -> m (Event t (ReqResult tag (Headers ls a)))
227− clientWithRoute Proxy _ _ req baseurl opts trigs = do
265+ clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs = do
228266 let method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
229267 resp <- fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity req') baseurl opts trigs
230− return $ toHeaders <$> resp
268+ wrap $ toHeaders <$> resp
231269 where req' = req { respHeaders =
232270 OnlyHeaders (Set.fromList (buildHeaderKeysTo (Proxy :: Proxy ls)))
233271 }
@@ -240,10 +278,10 @@ instance {-# OVERLAPPABLE #-}
240278 ) => HasClient t m (Verb method status cts (Headers ls NoContent)) tag where
241279 type Client t m (Verb method status cts (Headers ls NoContent)) tag
242280 = Event t tag -> m (Event t (ReqResult tag (Headers ls NoContent)))
243− clientWithRoute Proxy _ _ req baseurl opts trigs = do
281+ clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs = do
244282 let method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
245283 resp <- fmap runIdentity <$> performRequestsNoBody method (constDyn $ Identity req') baseurl opts trigs
246− return $ toHeaders <$> resp
284+ wrap $ toHeaders <$> resp
247285 where req' = req {respHeaders =
248286 OnlyHeaders (Set.fromList (buildHeaderKeysTo (Proxy :: Proxy ls)))
249287 }
@@ -270,11 +308,11 @@ instance (KnownSymbol sym, ToHttpApiData a,
270308 type Client t m (Header sym a :> sublayout) tag =
271309 Dynamic t (Either Text a) -> Client t m sublayout tag
272310
273− clientWithRoute Proxy q t req baseurl opts eVal =
274− clientWithRoute (Proxy :: Proxy sublayout)
275− q t
276− (Servant.Common.Req.addHeader hname eVal req)
277− 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
278316 where hname = T.pack $ symbolVal (Proxy :: Proxy sym)
279317
280318
@@ -288,8 +326,21 @@ instance HasClient t m sublayout tag
288326 type Client t m (HttpVersion :> sublayout) tag =
289327 Client t m sublayout tag
290328
291− clientWithRoute Proxy q t =
292− 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+
293344
294345
295346-- | If you use a 'QueryParam' in one of your endpoints in your API,
@@ -319,15 +370,15 @@ instance HasClient t m sublayout tag
319370-- > -- 'getBooksBy Nothing' for all books
320371-- > -- 'getBooksBy (Just "Isaac Asimov")' to get all books by Isaac Asimov
321372instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout tag, Reflex t)
322− => HasClient t m (QueryParam sym a :> sublayout) tag where
373+ => HasClient t m (QueryParam' mods sym a :> sublayout) tag where
323374
324− type Client t m (QueryParam sym a :> sublayout) tag =
375+ type Client t m (QueryParam' mods sym a :> sublayout) tag =
325376 Dynamic t (QParam a) -> Client t m sublayout tag
326377
327378 -- if mparam = Nothing, we don't add it to the query string
328− clientWithRoute Proxy q t req baseurl opts mparam =
329− clientWithRoute (Proxy :: Proxy sublayout) q t
330− (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
331382
332383 where pname = symbolVal (Proxy :: Proxy sym)
333384 --p prm = QueryPartParam $ (fmap . fmap) (toQueryParam) prm
@@ -371,8 +422,8 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout tag, Reflex
371422 type Client t m (QueryParams sym a :> sublayout) tag =
372423 Dynamic t [a] -> Client t m sublayout tag
373424
374− clientWithRoute Proxy q t req baseurl opts paramlist =
375− 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
376427
377428 where req' = req { qParams = (T.pack pname, params') : qParams req }
378429 pname = symbolVal (Proxy :: Proxy sym)
@@ -411,8 +462,8 @@ instance (KnownSymbol sym, HasClient t m sublayout tag, Reflex t)
411462 type Client t m (QueryFlag sym :> sublayout) tag =
412463 Dynamic t Bool -> Client t m sublayout tag
413464
414− clientWithRoute Proxy q t req baseurl opts flag =
415− 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
416467
417468 where req' = req { qParams = thisPair : qParams req }
418469 thisPair = (T.pack pName, QueryPartFlag flag) :: (Text, QueryPart t)
@@ -426,7 +477,7 @@ instance SupportsServantReflex t m => HasClient t m Raw tag where
426477 -> Event t tag
427478 -> m (Event t (ReqResult tag ()))
428479
429− clientWithRoute _ _ _ _ baseurl _ xhrs triggers = do
480+ clientWithRouteAndResultHandler _ _ _ _ baseurl _ wrap xhrs triggers = do
430481
431482 let xhrs' = liftA2 (\x path -> case x of
432483 Left e -> Left e
@@ -437,7 +488,7 @@ instance SupportsServantReflex t m => HasClient t m Raw tag where
437488 okReq = fmapMaybe (\(t,x) -> either (const Nothing) (Just . (t,)) x) xhrs'' :: Event t (tag, XhrRequest ())
438489
439490 resps <- performRequestsAsync okReq
440− return $ leftmost [ uncurry RequestFailure <$> badReq
491+ wrap $ leftmost [ uncurry RequestFailure <$> badReq
441492 , evalResponse (const $ Right ()) <$> resps
442493 ]
443494
@@ -468,8 +519,8 @@ instance (MimeRender ct a, HasClient t m sublayout tag, Reflex t)
468519 type Client t m (ReqBody (ct ': cts) a :> sublayout) tag =
469520 Dynamic t (Either Text a) -> Client t m sublayout tag
470521
471− clientWithRoute Proxy q t req baseurl opts body =
472− 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
473524 where req' = req { reqBody = bodyBytesCT }
474525 ctProxy = Proxy :: Proxy ct
475526 ctString = T.pack $ show $ contentType ctProxy
@@ -483,9 +534,9 @@ instance (MimeRender ct a, HasClient t m sublayout tag, Reflex t)
483534instance (KnownSymbol path, HasClient t m sublayout tag, Reflex t) => HasClient t m (path :> sublayout) tag where
484535 type Client t m (path :> sublayout) tag = Client t m sublayout tag
485536
486− clientWithRoute Proxy q t req baseurl opts =
487− clientWithRoute (Proxy :: Proxy sublayout) q t
488− (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
489540
490541 where p = symbolVal (Proxy :: Proxy path)
491542
@@ -493,23 +544,23 @@ instance (KnownSymbol path, HasClient t m sublayout tag, Reflex t) => HasClient
493544instance HasClient t m api tag => HasClient t m (Vault :> api) tag where
494545 type Client t m (Vault :> api) tag = Client t m api tag
495546
496− clientWithRoute Proxy q t req baseurl =
497− clientWithRoute (Proxy :: Proxy api) q t req baseurl
547+ clientWithRouteAndResultHandler Proxy =
548+ clientWithRouteAndResultHandler (Proxy :: Proxy api)
498549
499550
500551instance HasClient t m api tag => HasClient t m (RemoteHost :> api) tag where
501552 type Client t m (RemoteHost :> api) tag = Client t m api tag
502553
503− clientWithRoute Proxy q t req baseurl =
504− clientWithRoute (Proxy :: Proxy api) q t req baseurl
554+ clientWithRouteAndResultHandler Proxy =
555+ clientWithRouteAndResultHandler (Proxy :: Proxy api)
505556
506557
507558
508559instance HasClient t m api tag => HasClient t m (IsSecure :> api) tag where
509560 type Client t m (IsSecure :> api) tag = Client t m api tag
510561
511− clientWithRoute Proxy q t req baseurl =
512− clientWithRoute (Proxy :: Proxy api) q t req baseurl
562+ clientWithRouteAndResultHandler Proxy =
563+ clientWithRouteAndResultHandler (Proxy :: Proxy api)
513564
514565
515566instance (HasClient t m api tag, Reflex t)
@@ -518,8 +569,8 @@ instance (HasClient t m api tag, Reflex t)
518569 type Client t m (BasicAuth realm usr :> api) tag = Dynamic t (Maybe BasicAuthData)
519570 -> Client t m api tag
520571
521− clientWithRoute Proxy q t req baseurl opts authdata =
522− 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
523574 where
524575 req' = req { authData = Just authdata }
525576
@@ -562,7 +613,7 @@ for empty and one for non-empty lists).
562613instance (HasCookieAuth auths, HasClient t m api tag) => HasClient t m (Auth.Auth auths a :> api) tag where
563614
564615 type Client t m (Auth.Auth auths a :> api) tag = Client t m api tag
565− clientWithRoute Proxy = clientWithRoute (Proxy :: Proxy api)
616+ clientWithRouteAndResultHandler Proxy = clientWithRouteAndResultHandler (Proxy :: Proxy api)
566617
567618
568619type family HasCookieAuth xs :: Constraint where
@@ -571,4 +622,3 @@ type family HasCookieAuth xs :: Constraint where
571622 HasCookieAuth '[] = CookieAuthNotEnabled
572623
573624class CookieAuthNotEnabled
574−