git.iko.soy/typeable/servant
on master help sign in
Modified the tutorial to reflect the changes to servant-client (explicit parameters)
commita9200cd0501e9359141c2f55536b3d1364a6d476 authormbg <michael.gale@cl.cam.ac.uk> dateWed 30 Mar 2016, 22:50 (11 years ago) parent9e1ba92
1 file changed+17−11
doc/tutorial/Client.lhs+17−11
doc/tutorial/Client.lhs+17−11
@@ -15,13 +15,13 @@ need to have some language extensions and imports:
1515
1616module Client where
1717
18+import Control.Monad.Trans.Except (ExceptT, runExceptT)
1819import Data.Aeson
1920import Data.Proxy
2021import GHC.Generics
21−import Network.HTTP.Client (newManager, defaultManagerSettings)
22+import Network.HTTP.Client (Manager, newManager, defaultManagerSettings)
2223import Servant.API
2324import Servant.Client
24−import Servant.Common.Req (ClientM, runClientM)
2525```
2626
2727Also, 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
7171``` haskell
7272position :: Int -- ^ value for "x"
7373 -> 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
7577
7678hello :: Maybe String -- ^ an optional value for "name"
77− -> ClientM HelloMessage
79+ -> Manager -- ^ the HTTP client to use
80+ -> BaseUrl -- ^ the URL at which the API can be found
81+ -> ExceptT ServantError IO HelloMessage
7882
7983marketing :: 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
8187```
8288
8389Each function makes available as an argument any value that the response may
@@ -114,17 +120,17 @@ data BaseUrl = BaseUrl
114120That's it. Let's now write some code that uses our client functions.
115121
116122``` haskell
117−queries :: ClientM (Position, HelloMessage, Email)
118−queries = do
119− pos <- position 10 10
120− message <- hello (Just "servant")
121− em <- marketing (ClientInfo "Alp" "alp@foo.com" 26 ["haskell", "mathematics"])
123+queries :: Manager -> BaseUrl -> ExceptT ServantError IO (Position, HelloMessage, Email)
124+queries manager baseurl = do
125+ pos <- position 10 10 manager baseurl
126+ message <- hello (Just "servant") manager baseurl
127+ em <- marketing (ClientInfo "Alp" "alp@foo.com" 26 ["haskell", "mathematics"]) manager baseurl
122128 return (pos, message, em)
123129
124130run :: IO ()
125131run = do
126132 manager <- newManager defaultManagerSettings
127− res <- runClientM queries (BaseUrl Http "localhost" 8081 "") manager
133+ res <- runExceptT (queries manager (BaseUrl Http "localhost" 8081 ""))
128134 case res of
129135 Left err -> putStrLn $ "Error: " ++ show err
130136 Right (pos, message, em) -> do