on master help sign in
Merge branch 'master' into text-jsstring
commit89033dc42d741c7ca2736c06c14feb04e3e94888 authorKen Micklas <ken.micklas@obsidian.systems> dateTue 26 Jun 2018, 11:47 (8 years ago) parent6fb8c02ec87233
2 files changed+82−49
servant-reflex.cabal+1−1 src/Servant/Reflex.hs+81−48
servant-reflex.cabal+1−1
@@ -35,7 +35,7 @@ library
3535 exceptions >= 0.8 && < 0.9,
3636 ghcjs-dom >= 0.2 && < 0.10,
3737 http-api-data >= 0.3.6 && < 0.4,
38− http-media >= 0.6 && < 0.7,
38+ http-media >= 0.6 && < 0.8,
3939 jsaddle >= 0.8 && < 0.10,
4040 mtl >= 2.2.1 && < 2.3,
4141 network-uri >= 2.6 && < 2.7,
src/Servant/Reflex.hs+81−48
@@ -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
@@ -122,21 +125,53 @@ clientWithOpts
122125 -> Client t m layout tag
123126clientWithOpts p q t baseurl = clientWithRoute p q t defReq baseurl
124127
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+
125142
126143-- | This class lets us define how each API combinator
127144-- influences the creation of an HTTP request. It's mostly
128145-- an internal class, you can just use 'client'.
129−class HasClient t m layout (tag :: *) where
146+class Monad m => HasClient t m layout (tag :: *) where
130147 type Client t m layout tag :: *
131− 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
132167
133168
134169instance (HasClient t m a tag, HasClient t m b tag) => HasClient t m (a :<|> b) tag where
135170 type Client t m (a :<|> b) tag = Client t m a tag :<|> Client t m b tag
136171
137− clientWithRoute Proxy q pTag req baseurl opts =
138− clientWithRoute (Proxy :: Proxy a) q pTag req baseurl opts :<|>
139− 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
140175
141176
142177-- Capture. Example:
@@ -157,11 +192,9 @@ instance (SupportsServantReflex t m, ToHttpApiData a, HasClient t m sublayout ta
157192 type Client t m (Capture capture a :> sublayout) tag =
158193 Dynamic t (Either Text a) -> Client t m sublayout tag
159194
160− clientWithRoute Proxy q t req baseurl opts val =
161− clientWithRoute (Proxy :: Proxy sublayout)
162− q t
163− (prependToPathParts p req)
164− 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
165198 where p = (fmap . fmap) (toUrlPiece) val
166199
167200
@@ -174,8 +207,8 @@ instance {-# OVERLAPPABLE #-}
174207 Event t tag -> m (Event t (ReqResult tag a))
175208 -- TODO how to access input types here?
176209 -- ExceptT ServantError IO a
177− clientWithRoute Proxy _ _ req baseurl opts trigs =
178− 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
179212 where method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
180213 req' = req { reqMethod = method }
181214
@@ -188,8 +221,8 @@ instance {-# OVERLAPPING #-}
188221 Event t tag -> m (Event t (ReqResult tag NoContent))
189222 -- TODO: how to access input types here?
190223 -- ExceptT ServantError IO NoContent
191− clientWithRoute Proxy _ _ req baseurl opts =
192− (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
193226 where method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
194227
195228
@@ -225,10 +258,10 @@ instance {-# OVERLAPPABLE #-}
225258 ) => HasClient t m (Verb method status cts' (Headers ls a)) tag where
226259 type Client t m (Verb method status cts' (Headers ls a)) tag =
227260 Event t tag -> m (Event t (ReqResult tag (Headers ls a)))
228− clientWithRoute Proxy _ _ req baseurl opts trigs = do
261+ clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs = do
229262 let method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
230263 resp <- fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity req') baseurl opts trigs
231− return $ toHeaders <$> resp
264+ wrap $ toHeaders <$> resp
232265 where req' = req { respHeaders =
233266 OnlyHeaders (Set.fromList (buildHeaderKeysTo (Proxy :: Proxy ls)))
234267 }
@@ -241,10 +274,10 @@ instance {-# OVERLAPPABLE #-}
241274 ) => HasClient t m (Verb method status cts (Headers ls NoContent)) tag where
242275 type Client t m (Verb method status cts (Headers ls NoContent)) tag
243276 = Event t tag -> m (Event t (ReqResult tag (Headers ls NoContent)))
244− clientWithRoute Proxy _ _ req baseurl opts trigs = do
277+ clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs = do
245278 let method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
246279 resp <- fmap runIdentity <$> performRequestsNoBody method (constDyn $ Identity req') baseurl opts trigs
247− return $ toHeaders <$> resp
280+ wrap $ toHeaders <$> resp
248281 where req' = req {respHeaders =
249282 OnlyHeaders (Set.fromList (buildHeaderKeysTo (Proxy :: Proxy ls)))
250283 }
@@ -271,11 +304,11 @@ instance (KnownSymbol sym, ToHttpApiData a,
271304 type Client t m (Header sym a :> sublayout) tag =
272305 Dynamic t (Either Text a) -> Client t m sublayout tag
273306
274− clientWithRoute Proxy q t req baseurl opts eVal =
275− clientWithRoute (Proxy :: Proxy sublayout)
276− q t
277− (Servant.Common.Req.addHeader hname eVal req)
278− baseurl opts
307+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap eVal =
308+ clientWithRouteAndResultHandler
309+ (Proxy :: Proxy sublayout) q t
310+ (Servant.Common.Req.addHeader hname eVal req)
311+ baseurl opts wrap
279312 where hname = T.pack $ symbolVal (Proxy :: Proxy sym)
280313
281314
@@ -289,8 +322,8 @@ instance HasClient t m sublayout tag
289322 type Client t m (HttpVersion :> sublayout) tag =
290323 Client t m sublayout tag
291324
292− clientWithRoute Proxy q t =
293− clientWithRoute (Proxy :: Proxy sublayout) q t
325+ clientWithRouteAndResultHandler Proxy =
326+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout)
294327
295328
296329-- | If you use a 'QueryParam' in one of your endpoints in your API,
@@ -326,9 +359,9 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout tag, Reflex
326359 Dynamic t (QParam a) -> Client t m sublayout tag
327360
328361 -- if mparam = Nothing, we don't add it to the query string
329− clientWithRoute Proxy q t req baseurl opts mparam =
330− clientWithRoute (Proxy :: Proxy sublayout) q t
331− (req {qParams = paramPair : qParams req}) baseurl opts
362+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap mparam =
363+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t
364+ (req {qParams = paramPair : qParams req}) baseurl opts wrap
332365
333366 where pname = symbolVal (Proxy :: Proxy sym)
334367 --p prm = QueryPartParam $ (fmap . fmap) (toQueryParam) prm
@@ -372,8 +405,8 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout tag, Reflex
372405 type Client t m (QueryParams sym a :> sublayout) tag =
373406 Dynamic t [a] -> Client t m sublayout tag
374407
375− clientWithRoute Proxy q t req baseurl opts paramlist =
376− clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl opts
408+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap paramlist =
409+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t req' baseurl opts wrap
377410
378411 where req' = req { qParams = (T.pack pname, params') : qParams req }
379412 pname = symbolVal (Proxy :: Proxy sym)
@@ -412,8 +445,8 @@ instance (KnownSymbol sym, HasClient t m sublayout tag, Reflex t)
412445 type Client t m (QueryFlag sym :> sublayout) tag =
413446 Dynamic t Bool -> Client t m sublayout tag
414447
415− clientWithRoute Proxy q t req baseurl opts flag =
416− clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl opts
448+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap flag =
449+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t req' baseurl opts wrap
417450
418451 where req' = req { qParams = thisPair : qParams req }
419452 thisPair = (T.pack pName, QueryPartFlag flag) :: (Text, QueryPart t)
@@ -427,7 +460,7 @@ instance SupportsServantReflex t m => HasClient t m Raw tag where
427460 -> Event t tag
428461 -> m (Event t (ReqResult tag ()))
429462
430− clientWithRoute _ _ _ _ baseurl _ xhrs triggers = do
463+ clientWithRouteAndResultHandler _ _ _ _ baseurl _ wrap xhrs triggers = do
431464
432465 let xhrs' = liftA2 (\x path -> case x of
433466 Left e -> Left e
@@ -438,7 +471,7 @@ instance SupportsServantReflex t m => HasClient t m Raw tag where
438471 okReq = fmapMaybe (\(t,x) -> either (const Nothing) (Just . (t,)) x) xhrs'' :: Event t (tag, XhrRequest ())
439472
440473 resps <- performRequestsAsync okReq
441− return $ leftmost [ uncurry RequestFailure <$> badReq
474+ wrap $ leftmost [ uncurry RequestFailure <$> badReq
442475 , evalResponse (const $ Right ()) <$> resps
443476 ]
444477
@@ -469,8 +502,8 @@ instance (MimeRender ct a, HasClient t m sublayout tag, Reflex t)
469502 type Client t m (ReqBody (ct ': cts) a :> sublayout) tag =
470503 Dynamic t (Either Text a) -> Client t m sublayout tag
471504
472− clientWithRoute Proxy q t req baseurl opts body =
473− clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl opts
505+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap body =
506+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t req' baseurl opts wrap
474507 where req' = req { reqBody = bodyBytesCT }
475508 ctProxy = Proxy :: Proxy ct
476509 ctString = T.pack $ show $ contentType ctProxy
@@ -484,9 +517,9 @@ instance (MimeRender ct a, HasClient t m sublayout tag, Reflex t)
484517instance (KnownSymbol path, HasClient t m sublayout tag, Reflex t) => HasClient t m (path :> sublayout) tag where
485518 type Client t m (path :> sublayout) tag = Client t m sublayout tag
486519
487− clientWithRoute Proxy q t req baseurl opts =
488− clientWithRoute (Proxy :: Proxy sublayout) q t
489− (prependToPathParts (pure (Right $ T.pack p)) req) baseurl opts
520+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap =
521+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t
522+ (prependToPathParts (pure (Right $ T.pack p)) req) baseurl opts wrap
490523
491524 where p = symbolVal (Proxy :: Proxy path)
492525
@@ -494,23 +527,23 @@ instance (KnownSymbol path, HasClient t m sublayout tag, Reflex t) => HasClient
494527instance HasClient t m api tag => HasClient t m (Vault :> api) tag where
495528 type Client t m (Vault :> api) tag = Client t m api tag
496529
497− clientWithRoute Proxy q t req baseurl =
498− clientWithRoute (Proxy :: Proxy api) q t req baseurl
530+ clientWithRouteAndResultHandler Proxy =
531+ clientWithRouteAndResultHandler (Proxy :: Proxy api)
499532
500533
501534instance HasClient t m api tag => HasClient t m (RemoteHost :> api) tag where
502535 type Client t m (RemoteHost :> api) tag = Client t m api tag
503536
504− clientWithRoute Proxy q t req baseurl =
505− clientWithRoute (Proxy :: Proxy api) q t req baseurl
537+ clientWithRouteAndResultHandler Proxy =
538+ clientWithRouteAndResultHandler (Proxy :: Proxy api)
506539
507540
508541
509542instance HasClient t m api tag => HasClient t m (IsSecure :> api) tag where
510543 type Client t m (IsSecure :> api) tag = Client t m api tag
511544
512− clientWithRoute Proxy q t req baseurl =
513− clientWithRoute (Proxy :: Proxy api) q t req baseurl
545+ clientWithRouteAndResultHandler Proxy =
546+ clientWithRouteAndResultHandler (Proxy :: Proxy api)
514547
515548
516549instance (HasClient t m api tag, Reflex t)
@@ -519,8 +552,8 @@ instance (HasClient t m api tag, Reflex t)
519552 type Client t m (BasicAuth realm usr :> api) tag = Dynamic t (Maybe BasicAuthData)
520553 -> Client t m api tag
521554
522− clientWithRoute Proxy q t req baseurl opts authdata =
523− clientWithRoute (Proxy :: Proxy api) q t req' baseurl opts
555+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap authdata =
556+ clientWithRouteAndResultHandler (Proxy :: Proxy api) q t req' baseurl opts wrap
524557 where
525558 req' = req { authData = Just authdata }
526559
@@ -563,7 +596,7 @@ for empty and one for non-empty lists).
563596instance (HasCookieAuth auths, HasClient t m api tag) => HasClient t m (Auth.Auth auths a :> api) tag where
564597
565598 type Client t m (Auth.Auth auths a :> api) tag = Client t m api tag
566− clientWithRoute Proxy = clientWithRoute (Proxy :: Proxy api)
599+ clientWithRouteAndResultHandler Proxy = clientWithRouteAndResultHandler (Proxy :: Proxy api)
567600
568601
569602type family HasCookieAuth xs :: Constraint where