@@ -27,6 +27,7 @@ module Servant.Client.Core.HasClient (
27 HasClient (..),
27 HasClient (..),
28 EmptyClient (..),
28 EmptyClient (..),
29 AsClientT,
29 AsClientT,
31 foldMapUnion,
32 foldMapUnion,
32 matchUnion,
33 matchUnion,
@@ -54,7 +55,8 @@ import Data.Sequence
54 (fromList)
55 (fromList)
55import qualified Data.Text as T
56import qualified Data.Text as T
56import Network.HTTP.Media
57import Network.HTTP.Media
57− (MediaType, matches, parseAccept, (//))
58+ (MediaType, matches, parseAccept)
59+import qualified Network.HTTP.Media as Media
58import qualified Data.Sequence as Seq
60import qualified Data.Sequence as Seq
59import Data.SOP.BasicFunctors
61import Data.SOP.BasicFunctors
60 (I (I), (:.:) (Comp))
62 (I (I), (:.:) (Comp))
@@ -873,9 +875,12 @@ instance
876−infixl 1 /:
878+infixl 1 //
878−-- | Convenience function for working with nested record-clients.
881+-- | Helper to make code using records of clients more readable.
883+-- Can be mixed with (/:) for supplying arguments.
880-- Example:
885-- Example:
@@ -899,10 +904,46 @@ infixl 1 /:
899-- rootClient = client api
904-- rootClient = client api
901-- endpointClient :: ClientM Person
906-- endpointClient :: ClientM Person
902−-- endpointClient = client /: subApi /: endpoint
907+-- endpointClient = client // subApi // endpoint
909+(//) :: a -> (a -> b) -> b
912+-- | Convenience function for supplying arguments to client functions when
913+-- working with records of clients.
915+-- Intended to be use in conjunction with '(//)'.
920+-- type Api = NamedAPI RootApi
922+-- data RootApi mode = RootApi
923+-- { subApi :: mode :- Capture "token" String :> NamedAPI SubApi
924+-- , hello :: mode :- Capture "name" String :> Get '[JSON] String
926+-- } deriving Generic
928+-- data SubAmi mode = SubApi
929+-- { endpoint :: mode :- Get '[JSON] Person
931+-- } deriving Generic
936+-- rootClient :: RootApi (AsClientT ClientM)
937+-- rootClient = client api
939+-- hello :: String -> ClientM String
940+-- hello name = rootClient // hello /: name
942+-- endpointClient :: ClientM Person
943+-- endpointClient = client // subApi /: "foobar123" // endpoint
904−(/:) :: a -> (a -> b) -> b
945+(/:) :: (a -> b -> c) -> b -> a -> c
905−x /: f = f x
946+(/:) = flip
908{- Note [Non-Empty Content Types]
949{- Note [Non-Empty Content Types]
@@ -928,7 +969,7 @@ for empty and one for non-empty lists).
928checkContentTypeHeader :: RunClient m => Response -> m MediaType
969checkContentTypeHeader :: RunClient m => Response -> m MediaType
929checkContentTypeHeader response =
970checkContentTypeHeader response =
930 case lookup "Content-Type" $ toList $ responseHeaders response of
971 case lookup "Content-Type" $ toList $ responseHeaders response of
931− Nothing -> return $ "application"//"octet-stream"
972+ Nothing -> return $ "application" Media.// "octet-stream"
932 Just t -> case parseAccept t of
973 Just t -> case parseAccept t of
933 Nothing -> throwClientError $ InvalidContentTypeHeader response
974 Nothing -> throwClientError $ InvalidContentTypeHeader response
934 Just t' -> return t'
975 Just t' -> return t'