git.iko.soy/typeable/servant
on master help sign in
tutorial: make Client.lhs compile
df363ceSönke Hahn
doc/tutorial/Client.lhs+18−11
@@ -15,12 +15,14 @@ need to have some language extensions and imports:
15
15
16module Client where
16module Client where
17
17
18−import Control.Monad.Trans.Either
18+import Control.Monad.Trans.Except
19import Data.Aeson
19import Data.Aeson
20import Data.Proxy
20import Data.Proxy
21import GHC.Generics
21import GHC.Generics
22+import Network.HTTP.Client (Manager, newManager, defaultManagerSettings)
22import Servant.API
23import Servant.API
23import Servant.Client
24import Servant.Client
25+import System.IO.Unsafe
24```
26```
25
27
26Also, we need examples for some domain specific data types:
28Also, 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
70``` haskell
72``` haskell
71position :: Int -- ^ value for "x"
73position :: Int -- ^ value for "x"
72 -> Int -- ^ value for "y"
74 -> Int -- ^ value for "y"
73− -> EitherT ServantError IO Position
75+ -> ExceptT ServantError IO Position
74
76
75hello :: Maybe String -- ^ an optional value for "name"
77hello :: Maybe String -- ^ an optional value for "name"
76− -> EitherT ServantError IO HelloMessage
78+ -> ExceptT ServantError IO HelloMessage
77
79
78marketing :: ClientInfo -- ^ value for the request body
80marketing :: ClientInfo -- ^ value for the request body
79− -> EitherT ServantError IO Email
81+ -> ExceptT ServantError IO Email
80```
82```
81
83
82Each 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:
84Each 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
85api :: Proxy API
87api :: Proxy API
86api = Proxy
88api = Proxy
87
89
90+{-# NOINLINE __manager #-}
91+__manager :: Manager
92+__manager = unsafePerformIO $ newManager defaultManagerSettings
93+
94+position :<|> hello :<|> marketing =
88−position :<|> hello :<|> marketing = client api (BaseUrl Http "localhost" 8081)
95+ client api (BaseUrl Http "localhost" 8081 "") __manager
89```
96```
90
97
91As 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:
98As 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
109That's it. Let's now write some code that uses our client functions.
116That's it. Let's now write some code that uses our client functions.
110
117
111``` haskell
118``` haskell
112−queries :: EitherT ServantError IO (Position, HelloMessage, Email)
119+queries :: ExceptT ServantError IO (Position, HelloMessage, Email)
113queries = do
120queries = do
114 pos <- position 10 10
121 pos <- position 10 10
115− msg <- hello (Just "servant")
122+ message <- hello (Just "servant")
116 em <- marketing (ClientInfo "Alp" "alp@foo.com" 26 ["haskell", "mathematics"])
123 em <- marketing (ClientInfo "Alp" "alp@foo.com" 26 ["haskell", "mathematics"])
117− return (pos, msg, em)
124+ return (pos, message, em)
118
125
119run :: IO ()
126run :: IO ()
120run = do
127run = do
121− res <- runEitherT queries
128+ res <- runExceptT queries
122 case res of
129 case res of
123 Left err -> putStrLn $ "Error: " ++ show err
130 Left err -> putStrLn $ "Error: " ++ show err
124− Right (pos, msg, em) -> do
131+ Right (pos, message, em) -> do
125 print pos
132 print pos
126− print msg
133+ print message
127 print em
134 print em
128```
135```
129
136
doc/tutorial/tutorial.cabal+4−1
@@ -15,7 +15,7 @@ cabal-version: >=1.10
15
15
16library
16library
17 exposed-modules: ApiType
17 exposed-modules: ApiType
18− -- , Client
18+ , Client
19 -- , Docs
19 -- , Docs
20 -- , Javascript
20 -- , Javascript
21 -- , Server
21 -- , Server
@@ -45,6 +45,9 @@ library
45 , http-types
45 , http-types
46 , transformers
46 , transformers
47 , markdown-unlit >= 0.4
47 , markdown-unlit >= 0.4
48+ , http-client
48 -- hs-source-dirs:
49 -- hs-source-dirs:
49 default-language: Haskell2010
50 default-language: Haskell2010
50 ghc-options: -Wall -Werror -pgmL markdown-unlit
51 ghc-options: -Wall -Werror -pgmL markdown-unlit
52+ -- to silence aeson-0.10 warnings:
53+ ghc-options: -fno-warn-missing-methods