git.iko.soy/typeable/servant
on master help sign in
Updated tutorial to reflect the updated `client` function in servant-client
commit19a4e037d8c262da47c5be735392fdd7210499d8 authormbg <michael.gale@cl.cam.ac.uk> dateMon 28 Mar 2016, 15:27 (11 years ago) parent316737c
1 file changed+10−22
doc/tutorial/Client.lhs+10−22
doc/tutorial/Client.lhs+10−22
@@ -15,14 +15,13 @@ need to have some language extensions and imports:
1515
1616module Client where
1717
18−import Control.Monad.Trans.Except
1918import Data.Aeson
2019import Data.Proxy
2120import GHC.Generics
22−import Network.HTTP.Client (Manager, newManager, defaultManagerSettings)
21+import Network.HTTP.Client (newManager, defaultManagerSettings)
2322import Servant.API
2423import Servant.Client
25−import System.IO.Unsafe
24+import Servant.Common.Req (ClientM, runClientM)
2625```
2726
2827Also, we need examples for some domain specific data types:
@@ -72,40 +71,28 @@ What we are going to get with **servant-client** here is 3 functions, one to que
7271``` haskell
7372position :: Int -- ^ value for "x"
7473 -> Int -- ^ value for "y"
75− -> ExceptT ServantError IO Position
74+ -> ClientM Position
7675
7776hello :: Maybe String -- ^ an optional value for "name"
78− -> ExceptT ServantError IO HelloMessage
77+ -> ClientM HelloMessage
7978
8079marketing :: ClientInfo -- ^ value for the request body
81− -> ExceptT ServantError IO Email
80+ -> ClientM Email
8281```
8382
8483Each function makes available as an argument any value that the response may
8584depend on, as evidenced in the API type. How do we get these functions? By calling
86−the function `client`. It takes three arguments:
85+the function `client`. It takes one argument:
8786
8887- a `Proxy` to your API,
89−- a `BaseUrl`, consisting of the protocol, the host, the port and an optional subpath --
90− this basically tells `client` where the service that you want to query is hosted,
91−- a `Manager`, (from [http-client](http://hackage.haskell.org/package/http-client))
92−which manages http connections.
9388
9489``` haskell
9590api :: Proxy API
9691api = Proxy
9792
98−{-# NOINLINE __manager #-}
99−__manager :: Manager
100−__manager = unsafePerformIO $ newManager defaultManagerSettings
101−
102−position :<|> hello :<|> marketing =
103− client api (BaseUrl Http "localhost" 8081 "") __manager
93+position :<|> hello :<|> marketing = client api
10494```
10595
106−(Yes, the usage of `unsafePerformIO` is very ugly, we know. Hopefully soon it'll
107−be possible to do without.)
108−
10996As you can see in the code above, we just "pattern match our way" to these functions. If we try to derive less or more functions than there are endpoints in the API, we obviously get an error. The `BaseUrl` value there is just:
11097
11198``` haskell ignore
@@ -127,7 +114,7 @@ data BaseUrl = BaseUrl
127114That's it. Let's now write some code that uses our client functions.
128115
129116``` haskell
130−queries :: ExceptT ServantError IO (Position, HelloMessage, Email)
117+queries :: ClientM (Position, HelloMessage, Email)
131118queries = do
132119 pos <- position 10 10
133120 message <- hello (Just "servant")
@@ -136,7 +123,8 @@ queries = do
136123
137124run :: IO ()
138125run = do
139− res <- runExceptT queries
126+ manager <- newManager defaultManagerSettings
127+ res <- runClientM queries (BaseUrl Http "localhost" 8081 "") manager
140128 case res of
141129 Left err -> putStrLn $ "Error: " ++ show err
142130 Right (pos, message, em) -> do