@@ -8,7 +8,6 @@
88{-# LANGUAGE MultiParamTypeClasses #-}
99{-# LANGUAGE OverloadedStrings #-}
1010{-# LANGUAGE PolyKinds #-}
11−{-# LANGUAGE Rank2Types #-}
1211{-# LANGUAGE ScopedTypeVariables #-}
1312{-# LANGUAGE TupleSections #-}
1413{-# LANGUAGE TypeFamilies #-}
@@ -26,9 +25,7 @@
2625module Servant.Reflex
2726 ( client
2827 , clientWithOpts
29− , clientWithOptsAndResultHandler
3028 , clientWithRoute
31− , clientWithRouteAndResultHandler
3229 , BuildHeaderKeysTo(..)
3330 , toHeaders
3431 , HasClient
@@ -39,36 +36,34 @@ module Servant.Reflex
3936
4037------------------------------------------------------------------------------
4138import Control.Applicative
42−import qualified Data.CaseInsensitive as CI
43−import Data.Functor.Identity
44−import qualified Data.Map as Map
4539import Data.Monoid ((<>))
46−import Data.Proxy (Proxy (..))
4740import qualified Data.Set as Set
41+import qualified Data.Text.Encoding as E
42+import Data.CaseInsensitive (mk)
43+import Data.Functor.Identity
44+import Data.Proxy (Proxy (..))
45+import qualified Data.Map as Map
4846import Data.Text (Text)
4947import qualified Data.Text as T
50−import qualified Data.Text.Encoding as E
5148import GHC.Exts (Constraint)
5249import GHC.TypeLits (KnownSymbol, symbolVal)
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 (..),
50+import Servant.API ((:<|>)(..),(:>), BasicAuth,
51+ BasicAuthData, BuildHeadersTo(..),
52+ Capture, contentType, Header,
53+ Headers(..), HttpVersion, IsSecure,
54+ MimeRender(..), MimeUnrender,
55+ NoContent, QueryFlag, QueryParam,
56+ QueryParams, Raw, ReflectMethod(..),
6057 RemoteHost, ReqBody,
61− ToHttpApiData (..), Vault, Verb,
62− contentType)
63−import Servant.API.Description (Summary)
58+ ToHttpApiData(..), Vault, Verb)
6459import qualified Servant.Auth as Auth
6560
6661import Reflex.Dom.Core (Dynamic, Event, Reflex,
67− XhrRequest (..), XhrResponse (..),
68− XhrResponseHeaders (..),
69− attachPromptlyDynWith, constDyn, ffor,
70− fmapMaybe, leftmost,
71− performRequestsAsync)
62+ XhrRequest(..),
63+ XhrResponseHeaders(..),
64+ XhrResponse(..), attachPromptlyDynWith, constDyn, ffor, fmapMaybe,
65+ leftmost, performRequestsAsync,
66+ )
7267------------------------------------------------------------------------------
7368import Servant.Common.BaseUrl (BaseUrl(..), Scheme(..), baseUrlWidget,
7469 showBaseUrl,
@@ -103,7 +98,7 @@ import Servant.Common.Req (ClientOptions(..),
10398-- >
10499-- > getAllBooks :: Event t l -> m (Event t (l, ReqResult [Book]))
105100-- > postNewBook :: Dynamic t (Maybe Book) -> Event t l
106−-- > -> m (Event t (l, ReqResult Book)))
101+-- -> m (Event t (l, ReqResult Book)))
107102-- > (getAllBooks :<|> postNewBook) = client myApi host
108103-- > where host = constDyn $ BaseUrl Http "localhost" 8080
109104client
@@ -125,53 +120,21 @@ clientWithOpts
125120 -> Client t m layout tag
126121clientWithOpts p q t baseurl = clientWithRoute p q t defReq baseurl
127122
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−
142123
143124-- | This class lets us define how each API combinator
144125-- influences the creation of an HTTP request. It's mostly
145126-- an internal class, you can just use 'client'.
146−class Monad m => HasClient t m layout (tag :: *) where
127+class HasClient t m layout (tag :: *) where
147128 type 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
129+ clientWithRoute :: Proxy layout -> Proxy m -> Proxy tag -> Req t -> Dynamic t BaseUrl -> ClientOptions -> Client t m layout tag
167130
168131
169132instance (HasClient t m a tag, HasClient t m b tag) => HasClient t m (a :<|> b) tag where
170133 type Client t m (a :<|> b) tag = Client t m a tag :<|> Client t m b tag
171134
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
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
175138
176139
177140-- Capture. Example:
@@ -192,9 +155,11 @@ instance (SupportsServantReflex t m, ToHttpApiData a, HasClient t m sublayout ta
192155 type Client t m (Capture capture a :> sublayout) tag =
193156 Dynamic t (Either Text a) -> Client t m sublayout tag
194157
195− clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap val =
196− clientWithRouteAndResultHandler
197− (Proxy :: Proxy sublayout) q t (prependToPathParts p req) baseurl opts wrap
158+ clientWithRoute Proxy q t req baseurl opts val =
159+ clientWithRoute (Proxy :: Proxy sublayout)
160+ q t
161+ (prependToPathParts p req)
162+ baseurl opts
198163 where p = (fmap . fmap) (toUrlPiece) val
199164
200165
@@ -207,8 +172,8 @@ instance {-# OVERLAPPABLE #-}
207172 Event t tag -> m (Event t (ReqResult tag a))
208173 -- TODO how to access input types here?
209174 -- ExceptT ServantError IO a
210− clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs =
211− wrap =<< fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity $ req') baseurl opts trigs
175+ clientWithRoute Proxy _ _ req baseurl opts trigs =
176+ fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity $ req') baseurl opts trigs
212177 where method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
213178 req' = req { reqMethod = method }
214179
@@ -221,33 +186,29 @@ instance {-# OVERLAPPING #-}
221186 Event t tag -> m (Event t (ReqResult tag NoContent))
222187 -- TODO: how to access input types here?
223188 -- ExceptT ServantError IO NoContent
224− clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs =
225− wrap =<< fmap runIdentity <$> performRequestsNoBody method (constDyn $ Identity req) baseurl opts trigs
189+ clientWithRoute Proxy _ _ req baseurl opts =
190+ (fmap . fmap) runIdentity . performRequestsNoBody method (constDyn $ Identity req) baseurl opts
226191 where method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
227192
228193
229194toHeaders :: BuildHeadersTo ls => ReqResult tag a -> ReqResult tag (Headers ls a)
230195toHeaders r =
231− let hdrs = maybe []
232− (\xhr -> fmap (\(h,v) -> (CI.map E.encodeUtf8 h, E.encodeUtf8 v))
196+ let toBS = E.encodeUtf8
197+ hdrs = maybe []
198+ (\xhr -> fmap (\(h,v) -> (mk (toBS h), toBS v))
233199 (Map.toList $ _xhrResponse_headers xhr))
234200 (response r)
235201 in ffor r $ \a -> Headers {getResponse = a ,getHeadersHList = buildHeadersTo hdrs}
236202
237−
238203class BuildHeaderKeysTo hs where
239− buildHeaderKeysTo :: Proxy hs -> [CI.CI T.Text]
204+ buildHeaderKeysTo :: Proxy hs -> [T.Text]
240205
241206instance {-# OVERLAPPABLE #-} BuildHeaderKeysTo '[]
242207 where buildHeaderKeysTo _ = []
243208
244209instance {-# OVERLAPPABLE #-} (BuildHeaderKeysTo xs, KnownSymbol h)
245210 => BuildHeaderKeysTo ((Header h v) ': xs) where
246− buildHeaderKeysTo _ =
247− let
248− thisKey = CI.mk $ T.pack (symbolVal (Proxy :: Proxy h))
249− in thisKey : buildHeaderKeysTo (Proxy :: Proxy xs)
250−
211+ buildHeaderKeysTo _ = T.pack (symbolVal (Proxy :: Proxy h)) : buildHeaderKeysTo (Proxy :: Proxy xs)
251212
252213-- HEADERS Verb (Content) --
253214-- Headers combinator not treated in fully general case,
@@ -262,10 +223,10 @@ instance {-# OVERLAPPABLE #-}
262223 ) => HasClient t m (Verb method status cts' (Headers ls a)) tag where
263224 type Client t m (Verb method status cts' (Headers ls a)) tag =
264225 Event t tag -> m (Event t (ReqResult tag (Headers ls a)))
265− clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs = do
226+ clientWithRoute Proxy _ _ req baseurl opts trigs = do
266227 let method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
267228 resp <- fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity req') baseurl opts trigs
268− wrap $ toHeaders <$> resp
229+ return $ toHeaders <$> resp
269230 where req' = req { respHeaders =
270231 OnlyHeaders (Set.fromList (buildHeaderKeysTo (Proxy :: Proxy ls)))
271232 }
@@ -278,10 +239,10 @@ instance {-# OVERLAPPABLE #-}
278239 ) => HasClient t m (Verb method status cts (Headers ls NoContent)) tag where
279240 type Client t m (Verb method status cts (Headers ls NoContent)) tag
280241 = Event t tag -> m (Event t (ReqResult tag (Headers ls NoContent)))
281− clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs = do
242+ clientWithRoute Proxy _ _ req baseurl opts trigs = do
282243 let method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
283244 resp <- fmap runIdentity <$> performRequestsNoBody method (constDyn $ Identity req') baseurl opts trigs
284− wrap $ toHeaders <$> resp
245+ return $ toHeaders <$> resp
285246 where req' = req {respHeaders =
286247 OnlyHeaders (Set.fromList (buildHeaderKeysTo (Proxy :: Proxy ls)))
287248 }
@@ -308,11 +269,11 @@ instance (KnownSymbol sym, ToHttpApiData a,
308269 type Client t m (Header sym a :> sublayout) tag =
309270 Dynamic t (Either Text a) -> Client t m sublayout tag
310271
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
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
316277 where hname = T.pack $ symbolVal (Proxy :: Proxy sym)
317278
318279
@@ -326,21 +287,8 @@ instance HasClient t m sublayout tag
326287 type Client t m (HttpVersion :> sublayout) tag =
327288 Client t m sublayout tag
328289
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−
290+ clientWithRoute Proxy q t =
291+ clientWithRoute (Proxy :: Proxy sublayout) q t
344292
345293
346294-- | If you use a 'QueryParam' in one of your endpoints in your API,
@@ -370,15 +318,15 @@ instance (HasClient t m sublayout tag, KnownSymbol sym)
370318-- > -- 'getBooksBy Nothing' for all books
371319-- > -- 'getBooksBy (Just "Isaac Asimov")' to get all books by Isaac Asimov
372320instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout tag, Reflex t)
373− => HasClient t m (QueryParam' mods sym a :> sublayout) tag where
321+ => HasClient t m (QueryParam sym a :> sublayout) tag where
374322
375− type Client t m (QueryParam' mods sym a :> sublayout) tag =
323+ type Client t m (QueryParam sym a :> sublayout) tag =
376324 Dynamic t (QParam a) -> Client t m sublayout tag
377325
378326 -- if mparam = Nothing, we don't add it to the query string
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
327+ clientWithRoute Proxy q t req baseurl opts mparam =
328+ clientWithRoute (Proxy :: Proxy sublayout) q t
329+ (req {qParams = paramPair : qParams req}) baseurl opts
382330
383331 where pname = symbolVal (Proxy :: Proxy sym)
384332 --p prm = QueryPartParam $ (fmap . fmap) (toQueryParam) prm
@@ -422,8 +370,8 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout tag, Reflex
422370 type Client t m (QueryParams sym a :> sublayout) tag =
423371 Dynamic t [a] -> Client t m sublayout tag
424372
425− clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap paramlist =
426− clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t req' baseurl opts wrap
373+ clientWithRoute Proxy q t req baseurl opts paramlist =
374+ clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl opts
427375
428376 where req' = req { qParams = (T.pack pname, params') : qParams req }
429377 pname = symbolVal (Proxy :: Proxy sym)
@@ -462,8 +410,8 @@ instance (KnownSymbol sym, HasClient t m sublayout tag, Reflex t)
462410 type Client t m (QueryFlag sym :> sublayout) tag =
463411 Dynamic t Bool -> Client t m sublayout tag
464412
465− clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap flag =
466− clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t req' baseurl opts wrap
413+ clientWithRoute Proxy q t req baseurl opts flag =
414+ clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl opts
467415
468416 where req' = req { qParams = thisPair : qParams req }
469417 thisPair = (T.pack pName, QueryPartFlag flag) :: (Text, QueryPart t)
@@ -477,7 +425,7 @@ instance SupportsServantReflex t m => HasClient t m Raw tag where
477425 -> Event t tag
478426 -> m (Event t (ReqResult tag ()))
479427
480− clientWithRouteAndResultHandler _ _ _ _ baseurl _ wrap xhrs triggers = do
428+ clientWithRoute _ _ _ _ baseurl _ xhrs triggers = do
481429
482430 let xhrs' = liftA2 (\x path -> case x of
483431 Left e -> Left e
@@ -488,7 +436,7 @@ instance SupportsServantReflex t m => HasClient t m Raw tag where
488436 okReq = fmapMaybe (\(t,x) -> either (const Nothing) (Just . (t,)) x) xhrs'' :: Event t (tag, XhrRequest ())
489437
490438 resps <- performRequestsAsync okReq
491− wrap $ leftmost [ uncurry RequestFailure <$> badReq
439+ return $ leftmost [ uncurry RequestFailure <$> badReq
492440 , evalResponse (const $ Right ()) <$> resps
493441 ]
494442
@@ -519,8 +467,8 @@ instance (MimeRender ct a, HasClient t m sublayout tag, Reflex t)
519467 type Client t m (ReqBody (ct ': cts) a :> sublayout) tag =
520468 Dynamic t (Either Text a) -> Client t m sublayout tag
521469
522− clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap body =
523− clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t req' baseurl opts wrap
470+ clientWithRoute Proxy q t req baseurl opts body =
471+ clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl opts
524472 where req' = req { reqBody = bodyBytesCT }
525473 ctProxy = Proxy :: Proxy ct
526474 ctString = T.pack $ show $ contentType ctProxy
@@ -534,9 +482,9 @@ instance (MimeRender ct a, HasClient t m sublayout tag, Reflex t)
534482instance (KnownSymbol path, HasClient t m sublayout tag, Reflex t) => HasClient t m (path :> sublayout) tag where
535483 type Client t m (path :> sublayout) tag = Client t m sublayout tag
536484
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
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
540488
541489 where p = symbolVal (Proxy :: Proxy path)
542490
@@ -544,23 +492,23 @@ instance (KnownSymbol path, HasClient t m sublayout tag, Reflex t) => HasClient
544492instance HasClient t m api tag => HasClient t m (Vault :> api) tag where
545493 type Client t m (Vault :> api) tag = Client t m api tag
546494
547− clientWithRouteAndResultHandler Proxy =
548− clientWithRouteAndResultHandler (Proxy :: Proxy api)
495+ clientWithRoute Proxy q t req baseurl =
496+ clientWithRoute (Proxy :: Proxy api) q t req baseurl
549497
550498
551499instance HasClient t m api tag => HasClient t m (RemoteHost :> api) tag where
552500 type Client t m (RemoteHost :> api) tag = Client t m api tag
553501
554− clientWithRouteAndResultHandler Proxy =
555− clientWithRouteAndResultHandler (Proxy :: Proxy api)
502+ clientWithRoute Proxy q t req baseurl =
503+ clientWithRoute (Proxy :: Proxy api) q t req baseurl
556504
557505
558506
559507instance HasClient t m api tag => HasClient t m (IsSecure :> api) tag where
560508 type Client t m (IsSecure :> api) tag = Client t m api tag
561509
562− clientWithRouteAndResultHandler Proxy =
563− clientWithRouteAndResultHandler (Proxy :: Proxy api)
510+ clientWithRoute Proxy q t req baseurl =
511+ clientWithRoute (Proxy :: Proxy api) q t req baseurl
564512
565513
566514instance (HasClient t m api tag, Reflex t)
@@ -569,8 +517,8 @@ instance (HasClient t m api tag, Reflex t)
569517 type Client t m (BasicAuth realm usr :> api) tag = Dynamic t (Maybe BasicAuthData)
570518 -> Client t m api tag
571519
572− clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap authdata =
573− clientWithRouteAndResultHandler (Proxy :: Proxy api) q t req' baseurl opts wrap
520+ clientWithRoute Proxy q t req baseurl opts authdata =
521+ clientWithRoute (Proxy :: Proxy api) q t req' baseurl opts
574522 where
575523 req' = req { authData = Just authdata }
576524
@@ -613,7 +561,7 @@ for empty and one for non-empty lists).
613561instance (HasCookieAuth auths, HasClient t m api tag) => HasClient t m (Auth.Auth auths a :> api) tag where
614562
615563 type Client t m (Auth.Auth auths a :> api) tag = Client t m api tag
616− clientWithRouteAndResultHandler Proxy = clientWithRouteAndResultHandler (Proxy :: Proxy api)
564+ clientWithRoute Proxy = clientWithRoute (Proxy :: Proxy api)
617565
618566
619567type family HasCookieAuth xs :: Constraint where
@@ -622,3 +570,4 @@ type family HasCookieAuth xs :: Constraint where
622570 HasCookieAuth '[] = CookieAuthNotEnabled
623571
624572class CookieAuthNotEnabled
573+