servant/servant-examples/tutorial/T8.hs

50 lines
1.4 KiB
Haskell
Raw Normal View History

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
2015-05-07 17:48:21 +02:00
{-# LANGUAGE TypeOperators #-}
2015-05-10 13:39:18 +02:00
module T8 where
2015-05-07 17:48:21 +02:00
2015-09-12 14:11:24 +02:00
import Control.Monad.Trans.Except
2015-09-28 18:07:12 +02:00
import Network.HTTP.Client (Manager, defaultManagerSettings,
newManager)
import Servant
import Servant.Client
2015-09-28 18:07:12 +02:00
import System.IO.Unsafe (unsafePerformIO)
2015-05-07 17:48:21 +02:00
import T3
2015-05-07 17:48:21 +02:00
position :: Int -- ^ value for "x"
-> Int -- ^ value for "y"
2015-09-12 14:11:24 +02:00
-> ExceptT ServantError IO Position
2015-05-07 17:48:21 +02:00
hello :: Maybe String -- ^ an optional value for "name"
2015-09-12 14:11:24 +02:00
-> ExceptT ServantError IO HelloMessage
2015-05-07 17:48:21 +02:00
marketing :: ClientInfo -- ^ value for the request body
2015-09-12 14:11:24 +02:00
-> ExceptT ServantError IO Email
2015-05-07 17:48:21 +02:00
2015-09-28 18:07:12 +02:00
position :<|> hello :<|> marketing = client api baseUrl manager
2015-05-07 17:48:21 +02:00
baseUrl :: BaseUrl
2015-08-25 04:26:15 +02:00
baseUrl = BaseUrl Http "localhost" 8081 ""
2015-05-07 17:48:21 +02:00
2015-09-28 18:07:12 +02:00
{-# NOINLINE manager #-}
manager :: Manager
manager = unsafePerformIO $ newManager defaultManagerSettings
2015-09-12 14:11:24 +02:00
queries :: ExceptT ServantError IO (Position, HelloMessage, Email)
2015-05-07 17:48:21 +02:00
queries = do
2015-05-09 17:31:54 +02:00
pos <- position 10 10
msg <- hello (Just "servant")
em <- marketing (ClientInfo "Alp" "alp@foo.com" 26 ["haskell", "mathematics"])
2015-05-07 17:48:21 +02:00
return (pos, msg, em)
run :: IO ()
run = do
2015-09-12 14:11:24 +02:00
res <- runExceptT queries
2015-05-07 17:48:21 +02:00
case res of
Left err -> putStrLn $ "Error: " ++ show err
Right (pos, msg, em) -> do
print pos
print msg
print em