@@ -15,12 +15,14 @@ need to have some language extensions and imports:
16module Client where
16module Client where
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
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
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
78marketing :: ClientInfo -- ^ value for the request body
80marketing :: ClientInfo -- ^ value for the request body
79− -> EitherT ServantError IO Email
81+ -> ExceptT ServantError IO Email
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
90+{-# NOINLINE __manager #-}
92+__manager = unsafePerformIO $ newManager defaultManagerSettings
94+position :<|> hello :<|> marketing =
88−position :<|> hello :<|> marketing = client api (BaseUrl Http "localhost" 8081)
95+ client api (BaseUrl Http "localhost" 8081 "") __manager
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.
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)
119run :: IO ()
126run :: IO ()
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