From 0000189760cb5dcfd0e16537dc7b495b0a651c7d Mon Sep 17 00:00:00 2001 From: Alp Mestanogullari Date: Thu, 7 May 2015 17:48:21 +0200 Subject: [PATCH] examples: add GS8 --- servant-examples/getting-started/GS3.hs | 12 +++-- servant-examples/getting-started/GS8.hs | 46 +++++++++++++++++++ .../getting-started/getting-started.hs | 1 + servant-examples/getting-started/gs8-main.hs | 4 ++ servant-examples/servant-examples.cabal | 13 ++++++ 5 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 servant-examples/getting-started/GS8.hs create mode 100644 servant-examples/getting-started/gs8-main.hs diff --git a/servant-examples/getting-started/GS3.hs b/servant-examples/getting-started/GS3.hs index 26c2a7fe..505982cf 100644 --- a/servant-examples/getting-started/GS3.hs +++ b/servant-examples/getting-started/GS3.hs @@ -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 diff --git a/servant-examples/getting-started/GS8.hs b/servant-examples/getting-started/GS8.hs new file mode 100644 index 00000000..56039e5a --- /dev/null +++ b/servant-examples/getting-started/GS8.hs @@ -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 diff --git a/servant-examples/getting-started/getting-started.hs b/servant-examples/getting-started/getting-started.hs index 7f6f8c90..bf7103d2 100644 --- a/servant-examples/getting-started/getting-started.hs +++ b/servant-examples/getting-started/getting-started.hs @@ -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 () diff --git a/servant-examples/getting-started/gs8-main.hs b/servant-examples/getting-started/gs8-main.hs new file mode 100644 index 00000000..b6a9e30e --- /dev/null +++ b/servant-examples/getting-started/gs8-main.hs @@ -0,0 +1,4 @@ +import GS8 + +main :: IO () +main = run diff --git a/servant-examples/servant-examples.cabal b/servant-examples/servant-examples.cabal index ef2f8cc3..287e26bd 100644 --- a/servant-examples/servant-examples.cabal +++ b/servant-examples/servant-examples.cabal @@ -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: