@@ -24,9 +24,12 @@
2424-- API.
2525module Servant.Reflex
2626 ( client
27+ , clientWithOpts
28+ , clientWithRoute
2729 , BuildHeaderKeysTo(..)
2830 , toHeaders
29− , HasClient(..)
31+ , HasClient
32+ , Client
3033 , module Servant.Common.Req
3134 , module Servant.Common.BaseUrl
3235 ) where
@@ -65,7 +68,9 @@ import Reflex.Dom (Dynamic, Event, Reflex,
6568import Servant.Common.BaseUrl (BaseUrl(..), Scheme(..), baseUrlWidget,
6669 showBaseUrl,
6770 SupportsServantReflex)
68−import Servant.Common.Req (Req, ReqResult(..), QParam(..),
71+import Servant.Common.Req (ClientOptions(..),
72+ defaultClientOptions,
73+ Req, ReqResult(..), QParam(..),
6974 QueryPart(..), addHeader, authData,
7075 defReq, evalResponse, prependToPathParts,
7176 -- performRequestCT,
@@ -75,7 +80,8 @@ import Servant.Common.Req (Req, ReqResult(..), QParam(..),
7580 performSomeRequestsAsync,
7681 qParamToQueryPart, reqBody,
7782 reqSuccess, reqFailure,
78− reqMethod, respHeaders, response,
83+ reqMethod, respHeaders,
84+ response,
7985 reqTag,
8086 qParams)
8187
@@ -95,9 +101,24 @@ import Servant.Common.Req (Req, ReqResult(..), QParam(..),
95101-- -> m (Event t (l, ReqResult Book)))
96102-- > (getAllBooks :<|> postNewBook) = client myApi host
97103-- > where host = constDyn $ BaseUrl Http "localhost" 8080
98−client :: (HasClient t m layout tag)
99− => Proxy layout -> Proxy m -> Proxy tag -> Dynamic t BaseUrl -> Client t m layout tag
100−client p q t baseurl = clientWithRoute p q t defReq baseurl
104+client
105+ :: (HasClient t m layout tag)
106+ => Proxy layout
107+ -> Proxy m
108+ -> Proxy tag
109+ -> Dynamic t BaseUrl
110+ -> Client t m layout tag
111+client p q t baseurl = clientWithRoute p q t defReq baseurl defaultClientOptions
112+
113+clientWithOpts
114+ :: (HasClient t m layout tag)
115+ => Proxy layout
116+ -> Proxy m
117+ -> Proxy tag
118+ -> Dynamic t BaseUrl
119+ -> ClientOptions
120+ -> Client t m layout tag
121+clientWithOpts p q t baseurl = clientWithRoute p q t defReq baseurl
101122
102123
103124-- | This class lets us define how each API combinator
@@ -105,15 +126,15 @@ client p q t baseurl = clientWithRoute p q t defReq baseurl
105126-- an internal class, you can just use 'client'.
106127class HasClient t m layout (tag :: *) where
107128 type Client t m layout tag :: *
108− clientWithRoute :: Proxy layout -> Proxy m -> Proxy tag -> Req t -> Dynamic t BaseUrl -> Client t m layout tag
129+ clientWithRoute :: Proxy layout -> Proxy m -> Proxy tag -> Req t -> Dynamic t BaseUrl -> ClientOptions -> Client t m layout tag
109130
110131
111132instance (HasClient t m a tag, HasClient t m b tag) => HasClient t m (a :<|> b) tag where
112133 type Client t m (a :<|> b) tag = Client t m a tag :<|> Client t m b tag
113134
114− clientWithRoute Proxy q pTag req baseurl =
115− clientWithRoute (Proxy :: Proxy a) q pTag req baseurl :<|>
116− clientWithRoute (Proxy :: Proxy b) q pTag req baseurl
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
117138
118139
119140-- Capture. Example:
@@ -134,11 +155,11 @@ instance (SupportsServantReflex t m, ToHttpApiData a, HasClient t m sublayout ta
134155 type Client t m (Capture capture a :> sublayout) tag =
135156 Dynamic t (Either Text a) -> Client t m sublayout tag
136157
137− clientWithRoute Proxy q t req baseurl val =
158+ clientWithRoute Proxy q t req baseurl opts val =
138159 clientWithRoute (Proxy :: Proxy sublayout)
139160 q t
140161 (prependToPathParts p req)
141− baseurl
162+ baseurl opts
142163 where p = (fmap . fmap) (toUrlPiece) val
143164
144165
@@ -151,9 +172,8 @@ instance {-# OVERLAPPABLE #-}
151172 Event t tag -> m (Event t (ReqResult tag a))
152173 -- TODO how to access input types here?
153174 -- ExceptT ServantError IO a
154− clientWithRoute Proxy _ _ req baseurl trigs =
155− -- performRequestCT (Proxy :: Proxy ct) method req' baseurl trigs
156− fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity $ req') baseurl trigs
175+ clientWithRoute Proxy _ _ req baseurl opts trigs =
176+ fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity $ req') baseurl opts trigs
157177 where method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
158178 req' = req { reqMethod = method }
159179
@@ -166,8 +186,8 @@ instance {-# OVERLAPPING #-}
166186 Event t tag -> m (Event t (ReqResult tag NoContent))
167187 -- TODO: how to access input types here?
168188 -- ExceptT ServantError IO NoContent
169− clientWithRoute Proxy _ _ req baseurl =
170− (fmap . fmap) runIdentity . performRequestsNoBody method (constDyn $ Identity req) baseurl
189+ clientWithRoute Proxy _ _ req baseurl opts =
190+ (fmap . fmap) runIdentity . performRequestsNoBody method (constDyn $ Identity req) baseurl opts
171191 where method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
172192
173193
@@ -203,9 +223,9 @@ instance {-# OVERLAPPABLE #-}
203223 ) => HasClient t m (Verb method status cts' (Headers ls a)) tag where
204224 type Client t m (Verb method status cts' (Headers ls a)) tag =
205225 Event t tag -> m (Event t (ReqResult tag (Headers ls a)))
206− clientWithRoute Proxy _ _ req baseurl trigs = do
226+ clientWithRoute Proxy _ _ req baseurl opts trigs = do
207227 let method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
208− resp <- fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity req') baseurl trigs
228+ resp <- fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity req') baseurl opts trigs
209229 return $ toHeaders <$> resp
210230 where req' = req { respHeaders =
211231 OnlyHeaders (Set.fromList (buildHeaderKeysTo (Proxy :: Proxy ls)))
@@ -219,9 +239,9 @@ instance {-# OVERLAPPABLE #-}
219239 ) => HasClient t m (Verb method status cts (Headers ls NoContent)) tag where
220240 type Client t m (Verb method status cts (Headers ls NoContent)) tag
221241 = Event t tag -> m (Event t (ReqResult tag (Headers ls NoContent)))
222− clientWithRoute Proxy _ _ req baseurl trigs = do
242+ clientWithRoute Proxy _ _ req baseurl opts trigs = do
223243 let method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
224− resp <- fmap runIdentity <$> performRequestsNoBody method (constDyn $ Identity req') baseurl trigs
244+ resp <- fmap runIdentity <$> performRequestsNoBody method (constDyn $ Identity req') baseurl opts trigs
225245 return $ toHeaders <$> resp
226246 where req' = req {respHeaders =
227247 OnlyHeaders (Set.fromList (buildHeaderKeysTo (Proxy :: Proxy ls)))
@@ -249,11 +269,11 @@ instance (KnownSymbol sym, ToHttpApiData a,
249269 type Client t m (Header sym a :> sublayout) tag =
250270 Dynamic t (Either Text a) -> Client t m sublayout tag
251271
252− clientWithRoute Proxy q t req baseurl eVal =
272+ clientWithRoute Proxy q t req baseurl opts eVal =
253273 clientWithRoute (Proxy :: Proxy sublayout)
254274 q t
255275 (Servant.Common.Req.addHeader hname eVal req)
256− baseurl
276+ baseurl opts
257277 where hname = T.pack $ symbolVal (Proxy :: Proxy sym)
258278
259279
@@ -304,9 +324,9 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout tag, Reflex
304324 Dynamic t (QParam a) -> Client t m sublayout tag
305325
306326 -- if mparam = Nothing, we don't add it to the query string
307− clientWithRoute Proxy q t req baseurl mparam =
327+ clientWithRoute Proxy q t req baseurl opts mparam =
308328 clientWithRoute (Proxy :: Proxy sublayout) q t
309− (req {qParams = paramPair : qParams req}) baseurl
329+ (req {qParams = paramPair : qParams req}) baseurl opts
310330
311331 where pname = symbolVal (Proxy :: Proxy sym)
312332 --p prm = QueryPartParam $ (fmap . fmap) (toQueryParam) prm
@@ -350,8 +370,8 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout tag, Reflex
350370 type Client t m (QueryParams sym a :> sublayout) tag =
351371 Dynamic t [a] -> Client t m sublayout tag
352372
353− clientWithRoute Proxy q t req baseurl paramlist =
354− clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl
373+ clientWithRoute Proxy q t req baseurl opts paramlist =
374+ clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl opts
355375
356376 where req' = req { qParams = (T.pack pname, params') : qParams req }
357377 pname = symbolVal (Proxy :: Proxy sym)
@@ -390,8 +410,8 @@ instance (KnownSymbol sym, HasClient t m sublayout tag, Reflex t)
390410 type Client t m (QueryFlag sym :> sublayout) tag =
391411 Dynamic t Bool -> Client t m sublayout tag
392412
393− clientWithRoute Proxy q t req baseurl flag =
394− clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl
413+ clientWithRoute Proxy q t req baseurl opts flag =
414+ clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl opts
395415
396416 where req' = req { qParams = thisPair : qParams req }
397417 thisPair = (T.pack pName, QueryPartFlag flag) :: (Text, QueryPart t)
@@ -405,7 +425,7 @@ instance SupportsServantReflex t m => HasClient t m Raw tag where
405425 -> Event t tag
406426 -> m (Event t (ReqResult tag ()))
407427
408− clientWithRoute _ _ _ _ baseurl xhrs triggers = do
428+ clientWithRoute _ _ _ _ baseurl _ xhrs triggers = do
409429
410430 let xhrs' = liftA2 (\x path -> case x of
411431 Left e -> Left e
@@ -447,8 +467,8 @@ instance (MimeRender ct a, HasClient t m sublayout tag, Reflex t)
447467 type Client t m (ReqBody (ct ': cts) a :> sublayout) tag =
448468 Dynamic t (Either Text a) -> Client t m sublayout tag
449469
450− clientWithRoute Proxy q t req baseurl body =
451− clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl
470+ clientWithRoute Proxy q t req baseurl opts body =
471+ clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl opts
452472 where req' = req { reqBody = bodyBytesCT }
453473 ctProxy = Proxy :: Proxy ct
454474 ctString = T.pack $ show $ contentType ctProxy
@@ -462,9 +482,9 @@ instance (MimeRender ct a, HasClient t m sublayout tag, Reflex t)
462482instance (KnownSymbol path, HasClient t m sublayout tag, Reflex t) => HasClient t m (path :> sublayout) tag where
463483 type Client t m (path :> sublayout) tag = Client t m sublayout tag
464484
465− clientWithRoute Proxy q t req baseurl =
485+ clientWithRoute Proxy q t req baseurl opts =
466486 clientWithRoute (Proxy :: Proxy sublayout) q t
467− (prependToPathParts (pure (Right $ T.pack p)) req) baseurl
487+ (prependToPathParts (pure (Right $ T.pack p)) req) baseurl opts
468488
469489 where p = symbolVal (Proxy :: Proxy path)
470490
@@ -497,8 +517,8 @@ instance (HasClient t m api tag, Reflex t)
497517 type Client t m (BasicAuth realm usr :> api) tag = Dynamic t (Maybe BasicAuthData)
498518 -> Client t m api tag
499519
500− clientWithRoute Proxy q t req baseurl authdata =
501− clientWithRoute (Proxy :: Proxy api) q t req' baseurl
520+ clientWithRoute Proxy q t req baseurl opts authdata =
521+ clientWithRoute (Proxy :: Proxy api) q t req' baseurl opts
502522 where
503523 req' = req { authData = Just authdata }
504524