git.iko.soy/typeable/servant
on master help sign in
tutorial: make Client.lhs compile
commitdf363cecb095410a7a5d87f5f85a3f62f33af964 authorSönke Hahn <soenkehahn@gmail.com> dateThu 28 Jan 2016, 13:22 (11 years ago) parent52b1a23
2 files changed+22−12
doc/tutorial/Client.lhs+18−11 doc/tutorial/tutorial.cabal+4−1
doc/tutorial/Client.lhs+18−11
@@ -15,12 +15,14 @@ need to have some language extensions and imports:
1515
1616module Client where
1717
18−import Control.Monad.Trans.Either
18+import Control.Monad.Trans.Except
1919import Data.Aeson
2020import Data.Proxy
2121import GHC.Generics
22+import Network.HTTP.Client (Manager, newManager, defaultManagerSettings)
2223import Servant.API
2324import Servant.Client
25+import System.IO.Unsafe
2426```
2527
2628Also, we need examples for some domain specific data types:
@@ -70,13 +72,13 @@ What we are going to get with *servant-client* here is 3 functions, one to query
7072``` haskell
7173position :: Int -- ^ value for "x"
7274 -> Int -- ^ value for "y"
73− -> EitherT ServantError IO Position
75+ -> ExceptT ServantError IO Position
7476
7577hello :: Maybe String -- ^ an optional value for "name"
76− -> EitherT ServantError IO HelloMessage
78+ -> ExceptT ServantError IO HelloMessage
7779
7880marketing :: ClientInfo -- ^ value for the request body
79− -> EitherT ServantError IO Email
81+ -> ExceptT ServantError IO Email
8082```
8183
8284Each function makes available as an argument any value that the response may depend on, as evidenced in the API type. How do we get these functions? Just give a `Proxy` to your API and a host to make the requests to:
@@ -85,7 +87,12 @@ Each function makes available as an argument any value that the response may dep
8587api :: Proxy API
8688api = Proxy
8789
88−position :<|> hello :<|> marketing = client api (BaseUrl Http "localhost" 8081)
90+{-# NOINLINE __manager #-}
91+__manager :: Manager
92+__manager = unsafePerformIO $ newManager defaultManagerSettings
93+
94+position :<|> hello :<|> marketing =
95+ client api (BaseUrl Http "localhost" 8081 "") __manager
8996```
9097
9198As 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:
@@ -109,21 +116,21 @@ data BaseUrl = BaseUrl
109116That's it. Let's now write some code that uses our client functions.
110117
111118``` haskell
112−queries :: EitherT ServantError IO (Position, HelloMessage, Email)
119+queries :: ExceptT ServantError IO (Position, HelloMessage, Email)
113120queries = do
114121 pos <- position 10 10
115− msg <- hello (Just "servant")
122+ message <- hello (Just "servant")
116123 em <- marketing (ClientInfo "Alp" "alp@foo.com" 26 ["haskell", "mathematics"])
117− return (pos, msg, em)
124+ return (pos, message, em)
118125
119126run :: IO ()
120127run = do
121− res <- runEitherT queries
128+ res <- runExceptT queries
122129 case res of
123130 Left err -> putStrLn $ "Error: " ++ show err
124− Right (pos, msg, em) -> do
131+ Right (pos, message, em) -> do
125132 print pos
126− print msg
133+ print message
127134 print em
128135```
129136
doc/tutorial/tutorial.cabal+4−1
@@ -15,7 +15,7 @@ cabal-version: >=1.10
1515
1616library
1717 exposed-modules: ApiType
18− -- , Client
18+ , Client
1919 -- , Docs
2020 -- , Javascript
2121 -- , Server
@@ -45,6 +45,9 @@ library
4545 , http-types
4646 , transformers
4747 , markdown-unlit >= 0.4
48+ , http-client
4849 -- hs-source-dirs:
4950 default-language: Haskell2010
5051 ghc-options: -Wall -Werror -pgmL markdown-unlit
52+ -- to silence aeson-0.10 warnings:
53+ ghc-options: -fno-warn-missing-methods