on master help sign in
Add (/:) operator
6718752Gaël Deest
servant-client-core/src/Servant/Client/Core/HasClient.hs+32
@@ -27,6 +27,7 @@ module Servant.Client.Core.HasClient (
27 HasClient (..),
27 HasClient (..),
28 EmptyClient (..),
28 EmptyClient (..),
29 AsClientT,
29 AsClientT,
30+ (/:),
30 foldMapUnion,
31 foldMapUnion,
31 matchUnion,
32 matchUnion,
32 ) where
33 ) where
@@ -872,6 +873,37 @@ instance
872
873
873#endif
874#endif
874
875
876+infixl 1 /:
877+
878+-- | Convenience function for working with nested record-clients.
879+--
880+-- Example:
881+--
882+-- @@
883+-- type Api = NamedAPI RootApi
884+--
885+-- data RootApi mode = RootApi
886+-- { subApi :: mode :- NamedAPI SubApi
887+-- , …
888+-- } deriving Generic
889+--
890+-- data SubAmi mode = SubApi
891+-- { endpoint :: mode :- Get '[JSON] Person
892+-- , …
893+-- } deriving Generic
894+--
895+-- api :: Proxy API
896+-- api = Proxy
897+--
898+-- rootClient :: RootApi (AsClientT ClientM)
899+-- rootClient = client api
900+--
901+-- endpointClient :: ClientM Person
902+-- endpointClient = client /: subApi /: endpoint
903+-- @@
904+(/:) :: a -> (a -> b) -> b
905+x /: f = f x
906+
875
907
876{- Note [Non-Empty Content Types]
908{- Note [Non-Empty Content Types]
877~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
909~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
servant-client-core/src/Servant/Client/Core/Reexport.hs+1
@@ -8,6 +8,7 @@ module Servant.Client.Core.Reexport
8 , foldMapUnion
8 , foldMapUnion
9 , matchUnion
9 , matchUnion
10 , AsClientT
10 , AsClientT
11+ , (/:)
11
12
12 -- * Response (for @Raw@)
13 -- * Response (for @Raw@)
13 , Response
14 , Response
servant-client/test/Servant/GenericSpec.hs+4−4
@@ -17,9 +17,9 @@
17
17
18module Servant.GenericSpec (spec) where
18module Servant.GenericSpec (spec) where
19
19
20−import Data.Function ((&))
21import Test.Hspec
20import Test.Hspec
22
21
22+import Servant.Client ((/:))
23import Servant.ClientTestUtils
23import Servant.ClientTestUtils
24
24
25spec :: Spec
25spec :: Spec
@@ -31,7 +31,7 @@ genericSpec = beforeAll (startWaiApp server) $ afterAll endWaiApp $ do
31 context "Record clients work as expected" $ do
31 context "Record clients work as expected" $ do
32
32
33 it "Client functions return expected values" $ \(_,baseUrl) -> do
33 it "Client functions return expected values" $ \(_,baseUrl) -> do
34− runClient (recordRoutes & version) baseUrl `shouldReturn` Right 42
34+ runClient (recordRoutes /: version) baseUrl `shouldReturn` Right 42
35− runClient (recordRoutes & echo $ "foo") baseUrl `shouldReturn` Right "foo"
35+ runClient (recordRoutes /: echo $ "foo") baseUrl `shouldReturn` Right "foo"
36 it "Clients can be nested" $ \(_,baseUrl) -> do
36 it "Clients can be nested" $ \(_,baseUrl) -> do
37− runClient (recordRoutes & otherRoutes & something) baseUrl `shouldReturn` Right ["foo", "bar", "pweet"]
37+ runClient (recordRoutes /: otherRoutes /: something) baseUrl `shouldReturn` Right ["foo", "bar", "pweet"]