examples: add GS8

This commit is contained in:
Alp Mestanogullari 2015-05-07 17:48:21 +02:00
parent c892aae429
commit 0000189760
5 changed files with 72 additions and 4 deletions

View file

@ -14,13 +14,15 @@ import Servant
data Position = Position
{ x :: Int
, y :: Int
} deriving Generic
} deriving (Show, Generic)
instance FromJSON Position
instance ToJSON Position
newtype HelloMessage = HelloMessage { msg :: String }
deriving Generic
deriving (Show, Generic)
instance FromJSON HelloMessage
instance ToJSON HelloMessage
data ClientInfo = ClientInfo
@ -28,17 +30,19 @@ data ClientInfo = ClientInfo
, email :: String
, age :: Int
, interested_in :: [String]
} deriving Generic
} deriving (Show, Generic)
instance FromJSON ClientInfo
instance ToJSON ClientInfo
data Email = Email
{ from :: String
, to :: String
, subject :: String
, body :: String
} deriving Generic
} deriving (Show, Generic)
instance FromJSON Email
instance ToJSON Email
emailForClient :: ClientInfo -> Email

View file

@ -0,0 +1,46 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module GS8 where
import Control.Monad.Trans.Either
import Data.Aeson
import Servant
import Servant.Client
import GS3
position :: Int -- ^ value for "x"
-> Int -- ^ value for "y"
-> BaseUrl
-> EitherT ServantError IO Position
hello :: Maybe String -- ^ an optional value for "name"
-> BaseUrl
-> EitherT ServantError IO HelloMessage
marketing :: ClientInfo -- ^ value for the request body
-> BaseUrl
-> EitherT ServantError IO Email
position :<|> hello :<|> marketing = client api
baseUrl :: BaseUrl
baseUrl = BaseUrl Http "localhost" 8081
queries :: EitherT ServantError IO (Position, HelloMessage, Email)
queries = do
pos <- position 10 10 baseUrl
msg <- hello (Just "servant") baseUrl
em <- marketing (ClientInfo "Alp" "alp@foo.com" 26 ["haskell", "mathematics"]) baseUrl
return (pos, msg, em)
run :: IO ()
run = do
res <- runEitherT queries
case res of
Left err -> putStrLn $ "Error: " ++ show err
Right (pos, msg, em) -> do
print pos
print msg
print em

View file

@ -19,6 +19,7 @@ app n = case n of
"5" -> Just GS5.app
"6" -> Just GS6.app
"7" -> Just GS7.app
"8" -> Just GS3.app
_ -> Nothing
main :: IO ()

View file

@ -0,0 +1,4 @@
import GS8
main :: IO ()
main = run

View file

@ -32,6 +32,19 @@ executable getting-started
hs-source-dirs: getting-started
default-language: Haskell2010
executable gs8-main
main-is: gs8-main.hs
hs-source-dirs: getting-started
default-language: Haskell2010
build-depends:
aeson
, base
, either
, servant
, servant-client
, servant-server
, wai
executable hackage
main-is: hackage.hs
build-depends: