@@ -15,13 +15,13 @@ need to have some language extensions and imports:
16module Client where
16module Client where
18+import Control.Monad.Trans.Except (ExceptT, runExceptT)
18import Data.Aeson
19import Data.Aeson
19import Data.Proxy
20import Data.Proxy
20import GHC.Generics
21import GHC.Generics
21−import Network.HTTP.Client (newManager, defaultManagerSettings)
22+import Network.HTTP.Client (Manager, newManager, defaultManagerSettings)
22import Servant.API
23import Servant.API
23import Servant.Client
24import Servant.Client
24−import Servant.Common.Req (ClientM, runClientM)
27Also, we need examples for some domain specific data types:
27Also, we need examples for some domain specific data types:
@@ -71,13 +71,19 @@ What we are going to get with **servant-client** here is 3 functions, one to que
71``` haskell
71``` haskell
72position :: Int -- ^ value for "x"
72position :: Int -- ^ value for "x"
73 -> Int -- ^ value for "y"
73 -> Int -- ^ value for "y"
74− -> ClientM Position
74+ -> Manager -- ^ the HTTP client to use
75+ -> BaseUrl -- ^ the URL at which the API can be found
76+ -> ExceptT ServantError IO Position
76hello :: Maybe String -- ^ an optional value for "name"
78hello :: Maybe String -- ^ an optional value for "name"
79+ -> Manager -- ^ the HTTP client to use
80+ -> BaseUrl -- ^ the URL at which the API can be found
77− -> ClientM HelloMessage
81+ -> ExceptT ServantError IO HelloMessage
79marketing :: ClientInfo -- ^ value for the request body
83marketing :: ClientInfo -- ^ value for the request body
80− -> ClientM Email
84+ -> Manager -- ^ the HTTP client to use
85+ -> BaseUrl -- ^ the URL at which the API can be found
86+ -> ExceptT ServantError IO Email
83Each function makes available as an argument any value that the response may
89Each function makes available as an argument any value that the response may
@@ -114,17 +120,17 @@ data BaseUrl = BaseUrl
114That's it. Let's now write some code that uses our client functions.
120That's it. Let's now write some code that uses our client functions.
116``` haskell
122``` haskell
117−queries :: ClientM (Position, HelloMessage, Email)
123+queries :: Manager -> BaseUrl -> ExceptT ServantError IO (Position, HelloMessage, Email)
118−queries = do
124+queries manager baseurl = do
119− pos <- position 10 10
125+ pos <- position 10 10 manager baseurl
120− message <- hello (Just "servant")
126+ message <- hello (Just "servant") manager baseurl
121− em <- marketing (ClientInfo "Alp" "alp@foo.com" 26 ["haskell", "mathematics"])
127+ em <- marketing (ClientInfo "Alp" "alp@foo.com" 26 ["haskell", "mathematics"]) manager baseurl
122 return (pos, message, em)
128 return (pos, message, em)
124run :: IO ()
130run :: IO ()
126 manager <- newManager defaultManagerSettings
132 manager <- newManager defaultManagerSettings
127− res <- runClientM queries (BaseUrl Http "localhost" 8081 "") manager
133+ res <- runExceptT (queries manager (BaseUrl Http "localhost" 8081 ""))
128 case res of
134 case res of
129 Left err -> putStrLn $ "Error: " ++ show err
135 Left err -> putStrLn $ "Error: " ++ show err
130 Right (pos, message, em) -> do
136 Right (pos, message, em) -> do