diff --git a/servant-client-core/src/Servant/Client/Core/HasClient.hs b/servant-client-core/src/Servant/Client/Core/HasClient.hs index 11e7407e..a7573e5c 100644 --- a/servant-client-core/src/Servant/Client/Core/HasClient.hs +++ b/servant-client-core/src/Servant/Client/Core/HasClient.hs @@ -27,6 +27,7 @@ module Servant.Client.Core.HasClient ( HasClient (..), EmptyClient (..), AsClientT, + (/:), foldMapUnion, matchUnion, ) where @@ -872,6 +873,37 @@ instance #endif +infixl 1 /: + +-- | Convenience function for working with nested record-clients. +-- +-- Example: +-- +-- @@ +-- type Api = NamedAPI RootApi +-- +-- data RootApi mode = RootApi +-- { subApi :: mode :- NamedAPI SubApi +-- , … +-- } deriving Generic +-- +-- data SubAmi mode = SubApi +-- { endpoint :: mode :- Get '[JSON] Person +-- , … +-- } deriving Generic +-- +-- api :: Proxy API +-- api = Proxy +-- +-- rootClient :: RootApi (AsClientT ClientM) +-- rootClient = client api +-- +-- endpointClient :: ClientM Person +-- endpointClient = client /: subApi /: endpoint +-- @@ +(/:) :: a -> (a -> b) -> b +x /: f = f x + {- Note [Non-Empty Content Types] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/servant-client-core/src/Servant/Client/Core/Reexport.hs b/servant-client-core/src/Servant/Client/Core/Reexport.hs index 32c03eca..788e44b4 100644 --- a/servant-client-core/src/Servant/Client/Core/Reexport.hs +++ b/servant-client-core/src/Servant/Client/Core/Reexport.hs @@ -8,6 +8,7 @@ module Servant.Client.Core.Reexport , foldMapUnion , matchUnion , AsClientT + , (/:) -- * Response (for @Raw@) , Response diff --git a/servant-client/test/Servant/GenericSpec.hs b/servant-client/test/Servant/GenericSpec.hs index 9ce4f4a4..b347cf57 100644 --- a/servant-client/test/Servant/GenericSpec.hs +++ b/servant-client/test/Servant/GenericSpec.hs @@ -17,9 +17,9 @@ module Servant.GenericSpec (spec) where -import Data.Function ((&)) import Test.Hspec +import Servant.Client ((/:)) import Servant.ClientTestUtils spec :: Spec @@ -31,7 +31,7 @@ genericSpec = beforeAll (startWaiApp server) $ afterAll endWaiApp $ do context "Record clients work as expected" $ do it "Client functions return expected values" $ \(_,baseUrl) -> do - runClient (recordRoutes & version) baseUrl `shouldReturn` Right 42 - runClient (recordRoutes & echo $ "foo") baseUrl `shouldReturn` Right "foo" + runClient (recordRoutes /: version) baseUrl `shouldReturn` Right 42 + runClient (recordRoutes /: echo $ "foo") baseUrl `shouldReturn` Right "foo" it "Clients can be nested" $ \(_,baseUrl) -> do - runClient (recordRoutes & otherRoutes & something) baseUrl `shouldReturn` Right ["foo", "bar", "pweet"] + runClient (recordRoutes /: otherRoutes /: something) baseUrl `shouldReturn` Right ["foo", "bar", "pweet"]