@@ -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,49 +25,46 @@
2625module Servant.Reflex
2726 ( client
2827 , clientWithOpts
29− , clientWithOptsAndResultHandler
3028 , clientWithRoute
31− , clientWithRouteAndResultHandler
3229 , BuildHeaderKeysTo(..)
3330 , toHeaders
3431 , HasClient
3532 , Client
33+ , withCredentials
3634 , module Servant.Common.Req
3735 , module Servant.Common.BaseUrl
3836 ) where
3937
4038------------------------------------------------------------------------------
4139import Control.Applicative
42−import qualified Data.CaseInsensitive as CI
43−import Data.Functor.Identity
44−import qualified Data.Map as Map
4540import Data.Monoid ((<>))
46−import Data.Proxy (Proxy (..))
4741import qualified Data.Set as Set
42+import qualified Data.Text.Encoding as E
43+import Data.CaseInsensitive (mk)
44+import Data.Functor.Identity
45+import Data.Proxy (Proxy (..))
46+import qualified Data.Map as Map
4847import Data.Text (Text)
4948import qualified Data.Text as T
50−import qualified Data.Text.Encoding as E
5149import GHC.Exts (Constraint)
5250import GHC.TypeLits (KnownSymbol, symbolVal)
53−import Servant.API ((:<|>) (..), (:>), BasicAuth,
54− BasicAuthData, BuildHeadersTo (..),
55− Capture, Header, Headers (..),
56− HttpVersion, IsSecure,
57− MimeRender (..), MimeUnrender,
51+import Servant.API ((:<|>)(..),(:>), BasicAuth,
52+ BasicAuthData, BuildHeadersTo(..),
53+ Capture, contentType, Header,
54+ Headers(..), HttpVersion, IsSecure,
55+ MimeRender(..), MimeUnrender,
5856 NoContent, QueryFlag, QueryParam,
59− QueryParams, Raw, ReflectMethod (..),
57+ QueryParams, Raw, ReflectMethod(..),
6058 RemoteHost, ReqBody,
61− ToHttpApiData (..), Vault, Verb,
62− contentType)
63−import Servant.API.Description (Summary)
59+ ToHttpApiData(..), Vault, Verb)
6460import qualified Servant.Auth as Auth
6561
66−import Reflex.Dom.Core (Dynamic, Event, Reflex,
67− XhrRequest (..), XhrResponse (..),
68− XhrResponseHeaders (..),
69− attachPromptlyDynWith, constDyn, ffor,
70− fmapMaybe, leftmost,
71− performRequestsAsync)
62+import Reflex.Dom (Dynamic, Event, Reflex,
63+ XhrRequest(..),
64+ XhrResponseHeaders(..),
65+ XhrResponse(..), attachPromptlyDynWith, constDyn, ffor, fmapMaybe,
66+ leftmost, performRequestsAsync,
67+ )
7268------------------------------------------------------------------------------
7369import Servant.Common.BaseUrl (BaseUrl(..), Scheme(..), baseUrlWidget,
7470 showBaseUrl,
@@ -103,7 +99,7 @@ import Servant.Common.Req (ClientOptions(..),
10399-- >
104100-- > getAllBooks :: Event t l -> m (Event t (l, ReqResult [Book]))
105101-- > postNewBook :: Dynamic t (Maybe Book) -> Event t l
106−-- > -> m (Event t (l, ReqResult Book)))
102+-- -> m (Event t (l, ReqResult Book)))
107103-- > (getAllBooks :<|> postNewBook) = client myApi host
108104-- > where host = constDyn $ BaseUrl Http "localhost" 8080
109105client
@@ -125,53 +121,21 @@ clientWithOpts
125121 -> Client t m layout tag
126122clientWithOpts p q t baseurl = clientWithRoute p q t defReq baseurl
127123
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−
142124
143125-- | This class lets us define how each API combinator
144126-- influences the creation of an HTTP request. It's mostly
145127-- an internal class, you can just use 'client'.
146−class Monad m => HasClient t m layout (tag :: *) where
128+class HasClient t m layout (tag :: *) where
147129 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
130+ clientWithRoute :: Proxy layout -> Proxy m -> Proxy tag -> Req t -> Dynamic t BaseUrl -> ClientOptions -> Client t m layout tag
167131
168132
169133instance (HasClient t m a tag, HasClient t m b tag) => HasClient t m (a :<|> b) tag where
170134 type Client t m (a :<|> b) tag = Client t m a tag :<|> Client t m b tag
171135
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
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
175139
176140
177141-- Capture. Example:
@@ -192,9 +156,11 @@ instance (SupportsServantReflex t m, ToHttpApiData a, HasClient t m sublayout ta
192156 type Client t m (Capture capture a :> sublayout) tag =
193157 Dynamic t (Either Text a) -> Client t m sublayout tag
194158
195− clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap val =
196− clientWithRouteAndResultHandler
197− (Proxy :: Proxy sublayout) q t (prependToPathParts p req) baseurl opts wrap
159+ clientWithRoute Proxy q t req baseurl opts val =
160+ clientWithRoute (Proxy :: Proxy sublayout)
161+ q t
162+ (prependToPathParts p req)
163+ baseurl opts
198164 where p = (fmap . fmap) (toUrlPiece) val
199165
200166
@@ -207,8 +173,8 @@ instance {-# OVERLAPPABLE #-}
207173 Event t tag -> m (Event t (ReqResult tag a))
208174 -- TODO how to access input types here?
209175 -- 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
176+ clientWithRoute Proxy _ _ req baseurl opts trigs =
177+ fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity $ req') baseurl opts trigs
212178 where method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
213179 req' = req { reqMethod = method }
214180
@@ -221,33 +187,29 @@ instance {-# OVERLAPPING #-}
221187 Event t tag -> m (Event t (ReqResult tag NoContent))
222188 -- TODO: how to access input types here?
223189 -- ExceptT ServantError IO NoContent
224− clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs =
225− wrap =<< fmap runIdentity <$> performRequestsNoBody method (constDyn $ Identity req) baseurl opts trigs
190+ clientWithRoute Proxy _ _ req baseurl opts =
191+ (fmap . fmap) runIdentity . performRequestsNoBody method (constDyn $ Identity req) baseurl opts
226192 where method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
227193
228194
229195toHeaders :: BuildHeadersTo ls => ReqResult tag a -> ReqResult tag (Headers ls a)
230196toHeaders r =
231− let hdrs = maybe []
232− (\xhr -> fmap (\(h,v) -> (CI.map E.encodeUtf8 h, E.encodeUtf8 v))
197+ let toBS = E.encodeUtf8
198+ hdrs = maybe []
199+ (\xhr -> fmap (\(h,v) -> (mk (toBS h), toBS v))
233200 (Map.toList $ _xhrResponse_headers xhr))
234201 (response r)
235202 in ffor r $ \a -> Headers {getResponse = a ,getHeadersHList = buildHeadersTo hdrs}
236203
237−
238204class BuildHeaderKeysTo hs where
239− buildHeaderKeysTo :: Proxy hs -> [CI.CI T.Text]
205+ buildHeaderKeysTo :: Proxy hs -> [T.Text]
240206
241207instance {-# OVERLAPPABLE #-} BuildHeaderKeysTo '[]
242208 where buildHeaderKeysTo _ = []
243209
244210instance {-# OVERLAPPABLE #-} (BuildHeaderKeysTo xs, KnownSymbol h)
245211 => 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−
212+ buildHeaderKeysTo _ = T.pack (symbolVal (Proxy :: Proxy h)) : buildHeaderKeysTo (Proxy :: Proxy xs)
251213
252214-- HEADERS Verb (Content) --
253215-- Headers combinator not treated in fully general case,
@@ -262,10 +224,10 @@ instance {-# OVERLAPPABLE #-}
262224 ) => HasClient t m (Verb method status cts' (Headers ls a)) tag where
263225 type Client t m (Verb method status cts' (Headers ls a)) tag =
264226 Event t tag -> m (Event t (ReqResult tag (Headers ls a)))
265− clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs = do
227+ clientWithRoute Proxy _ _ req baseurl opts trigs = do
266228 let method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
267229 resp <- fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity req') baseurl opts trigs
268− wrap $ toHeaders <$> resp
230+ return $ toHeaders <$> resp
269231 where req' = req { respHeaders =
270232 OnlyHeaders (Set.fromList (buildHeaderKeysTo (Proxy :: Proxy ls)))
271233 }
@@ -278,10 +240,10 @@ instance {-# OVERLAPPABLE #-}
278240 ) => HasClient t m (Verb method status cts (Headers ls NoContent)) tag where
279241 type Client t m (Verb method status cts (Headers ls NoContent)) tag
280242 = Event t tag -> m (Event t (ReqResult tag (Headers ls NoContent)))
281− clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs = do
243+ clientWithRoute Proxy _ _ req baseurl opts trigs = do
282244 let method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
283245 resp <- fmap runIdentity <$> performRequestsNoBody method (constDyn $ Identity req') baseurl opts trigs
284− wrap $ toHeaders <$> resp
246+ return $ toHeaders <$> resp
285247 where req' = req {respHeaders =
286248 OnlyHeaders (Set.fromList (buildHeaderKeysTo (Proxy :: Proxy ls)))
287249 }
@@ -308,11 +270,11 @@ instance (KnownSymbol sym, ToHttpApiData a,
308270 type Client t m (Header sym a :> sublayout) tag =
309271 Dynamic t (Either Text a) -> Client t m sublayout tag
310272
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
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
316278 where hname = T.pack $ symbolVal (Proxy :: Proxy sym)
317279
318280
@@ -326,21 +288,8 @@ instance HasClient t m sublayout tag
326288 type Client t m (HttpVersion :> sublayout) tag =
327289 Client t m sublayout tag
328290
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−
291+ clientWithRoute Proxy q t =
292+ clientWithRoute (Proxy :: Proxy sublayout) q t
344293
345294
346295-- | If you use a 'QueryParam' in one of your endpoints in your API,
@@ -376,9 +325,9 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout tag, Reflex
376325 Dynamic t (QParam a) -> Client t m sublayout tag
377326
378327 -- 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
328+ clientWithRoute Proxy q t req baseurl opts mparam =
329+ clientWithRoute (Proxy :: Proxy sublayout) q t
330+ (req {qParams = paramPair : qParams req}) baseurl opts
382331
383332 where pname = symbolVal (Proxy :: Proxy sym)
384333 --p prm = QueryPartParam $ (fmap . fmap) (toQueryParam) prm
@@ -422,8 +371,8 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout tag, Reflex
422371 type Client t m (QueryParams sym a :> sublayout) tag =
423372 Dynamic t [a] -> Client t m sublayout tag
424373
425− clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap paramlist =
426− clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t req' baseurl opts wrap
374+ clientWithRoute Proxy q t req baseurl opts paramlist =
375+ clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl opts
427376
428377 where req' = req { qParams = (T.pack pname, params') : qParams req }
429378 pname = symbolVal (Proxy :: Proxy sym)
@@ -462,8 +411,8 @@ instance (KnownSymbol sym, HasClient t m sublayout tag, Reflex t)
462411 type Client t m (QueryFlag sym :> sublayout) tag =
463412 Dynamic t Bool -> Client t m sublayout tag
464413
465− clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap flag =
466− clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t req' baseurl opts wrap
414+ clientWithRoute Proxy q t req baseurl opts flag =
415+ clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl opts
467416
468417 where req' = req { qParams = thisPair : qParams req }
469418 thisPair = (T.pack pName, QueryPartFlag flag) :: (Text, QueryPart t)
@@ -477,7 +426,7 @@ instance SupportsServantReflex t m => HasClient t m Raw tag where
477426 -> Event t tag
478427 -> m (Event t (ReqResult tag ()))
479428
480− clientWithRouteAndResultHandler _ _ _ _ baseurl _ wrap xhrs triggers = do
429+ clientWithRoute _ _ _ _ baseurl _ xhrs triggers = do
481430
482431 let xhrs' = liftA2 (\x path -> case x of
483432 Left e -> Left e
@@ -488,7 +437,7 @@ instance SupportsServantReflex t m => HasClient t m Raw tag where
488437 okReq = fmapMaybe (\(t,x) -> either (const Nothing) (Just . (t,)) x) xhrs'' :: Event t (tag, XhrRequest ())
489438
490439 resps <- performRequestsAsync okReq
491− wrap $ leftmost [ uncurry RequestFailure <$> badReq
440+ return $ leftmost [ uncurry RequestFailure <$> badReq
492441 , evalResponse (const $ Right ()) <$> resps
493442 ]
494443
@@ -519,8 +468,8 @@ instance (MimeRender ct a, HasClient t m sublayout tag, Reflex t)
519468 type Client t m (ReqBody (ct ': cts) a :> sublayout) tag =
520469 Dynamic t (Either Text a) -> Client t m sublayout tag
521470
522− clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap body =
523− clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t req' baseurl opts wrap
471+ clientWithRoute Proxy q t req baseurl opts body =
472+ clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl opts
524473 where req' = req { reqBody = bodyBytesCT }
525474 ctProxy = Proxy :: Proxy ct
526475 ctString = T.pack $ show $ contentType ctProxy
@@ -534,9 +483,9 @@ instance (MimeRender ct a, HasClient t m sublayout tag, Reflex t)
534483instance (KnownSymbol path, HasClient t m sublayout tag, Reflex t) => HasClient t m (path :> sublayout) tag where
535484 type Client t m (path :> sublayout) tag = Client t m sublayout tag
536485
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
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
540489
541490 where p = symbolVal (Proxy :: Proxy path)
542491
@@ -544,23 +493,23 @@ instance (KnownSymbol path, HasClient t m sublayout tag, Reflex t) => HasClient
544493instance HasClient t m api tag => HasClient t m (Vault :> api) tag where
545494 type Client t m (Vault :> api) tag = Client t m api tag
546495
547− clientWithRouteAndResultHandler Proxy =
548− clientWithRouteAndResultHandler (Proxy :: Proxy api)
496+ clientWithRoute Proxy q t req baseurl =
497+ clientWithRoute (Proxy :: Proxy api) q t req baseurl
549498
550499
551500instance HasClient t m api tag => HasClient t m (RemoteHost :> api) tag where
552501 type Client t m (RemoteHost :> api) tag = Client t m api tag
553502
554− clientWithRouteAndResultHandler Proxy =
555− clientWithRouteAndResultHandler (Proxy :: Proxy api)
503+ clientWithRoute Proxy q t req baseurl =
504+ clientWithRoute (Proxy :: Proxy api) q t req baseurl
556505
557506
558507
559508instance HasClient t m api tag => HasClient t m (IsSecure :> api) tag where
560509 type Client t m (IsSecure :> api) tag = Client t m api tag
561510
562− clientWithRouteAndResultHandler Proxy =
563− clientWithRouteAndResultHandler (Proxy :: Proxy api)
511+ clientWithRoute Proxy q t req baseurl =
512+ clientWithRoute (Proxy :: Proxy api) q t req baseurl
564513
565514
566515instance (HasClient t m api tag, Reflex t)
@@ -569,8 +518,8 @@ instance (HasClient t m api tag, Reflex t)
569518 type Client t m (BasicAuth realm usr :> api) tag = Dynamic t (Maybe BasicAuthData)
570519 -> Client t m api tag
571520
572− clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap authdata =
573− clientWithRouteAndResultHandler (Proxy :: Proxy api) q t req' baseurl opts wrap
521+ clientWithRoute Proxy q t req baseurl opts authdata =
522+ clientWithRoute (Proxy :: Proxy api) q t req' baseurl opts
574523 where
575524 req' = req { authData = Just authdata }
576525
@@ -613,7 +562,7 @@ for empty and one for non-empty lists).
613562instance (HasCookieAuth auths, HasClient t m api tag) => HasClient t m (Auth.Auth auths a :> api) tag where
614563
615564 type Client t m (Auth.Auth auths a :> api) tag = Client t m api tag
616− clientWithRouteAndResultHandler Proxy = clientWithRouteAndResultHandler (Proxy :: Proxy api)
565+ clientWithRoute Proxy = clientWithRoute (Proxy :: Proxy api)
617566
618567
619568type family HasCookieAuth xs :: Constraint where