git.iko.soy/typeable/servant
on master help sign in
examples: add GS8
commit0000189760cb5dcfd0e16537dc7b495b0a651c7d authorAlp Mestanogullari <alpmestan@gmail.com> dateThu 7 May 2015, 17:48 (11 years ago) parentc892aae
5 files changed+72−4
servant-examples/getting-started/GS3.hs+8−4 servant-examples/getting-started/GS8.hs+46 servant-examples/getting-started/getting-started.hs+1 servant-examples/getting-started/gs8-main.hs+4 servant-examples/servant-examples.cabal+13
servant-examples/getting-started/GS3.hs+8−4
@@ -14,13 +14,15 @@ import Servant
1414data Position = Position
1515 { x :: Int
1616 , y :: Int
17− } deriving Generic
17+ } deriving (Show, Generic)
1818
19+instance FromJSON Position
1920instance ToJSON Position
2021
2122newtype HelloMessage = HelloMessage { msg :: String }
22− deriving Generic
23+ deriving (Show, Generic)
2324
25+instance FromJSON HelloMessage
2426instance ToJSON HelloMessage
2527
2628data ClientInfo = ClientInfo
@@ -28,17 +30,19 @@ data ClientInfo = ClientInfo
2830 , email :: String
2931 , age :: Int
3032 , interested_in :: [String]
31− } deriving Generic
33+ } deriving (Show, Generic)
3234
3335instance FromJSON ClientInfo
36+instance ToJSON ClientInfo
3437
3538data Email = Email
3639 { from :: String
3740 , to :: String
3841 , subject :: String
3942 , body :: String
40− } deriving Generic
43+ } deriving (Show, Generic)
4144
45+instance FromJSON Email
4246instance ToJSON Email
4347
4448emailForClient :: ClientInfo -> Email
servant-examples/getting-started/GS8.hsnew file+46
@@ -0,0 +1,46 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE TypeFamilies #-}
3+{-# LANGUAGE TypeOperators #-}
4+module GS8 where
5+
6+import Control.Monad.Trans.Either
7+import Data.Aeson
8+import Servant
9+import Servant.Client
10+
11+import GS3
12+
13+position :: Int -- ^ value for "x"
14+ -> Int -- ^ value for "y"
15+ -> BaseUrl
16+ -> EitherT ServantError IO Position
17+
18+hello :: Maybe String -- ^ an optional value for "name"
19+ -> BaseUrl
20+ -> EitherT ServantError IO HelloMessage
21+
22+marketing :: ClientInfo -- ^ value for the request body
23+ -> BaseUrl
24+ -> EitherT ServantError IO Email
25+
26+position :<|> hello :<|> marketing = client api
27+
28+baseUrl :: BaseUrl
29+baseUrl = BaseUrl Http "localhost" 8081
30+
31+queries :: EitherT ServantError IO (Position, HelloMessage, Email)
32+queries = do
33+ pos <- position 10 10 baseUrl
34+ msg <- hello (Just "servant") baseUrl
35+ em <- marketing (ClientInfo "Alp" "alp@foo.com" 26 ["haskell", "mathematics"]) baseUrl
36+ return (pos, msg, em)
37+
38+run :: IO ()
39+run = do
40+ res <- runEitherT queries
41+ case res of
42+ Left err -> putStrLn $ "Error: " ++ show err
43+ Right (pos, msg, em) -> do
44+ print pos
45+ print msg
46+ print em
servant-examples/getting-started/getting-started.hs+1
@@ -19,6 +19,7 @@ app n = case n of
1919 "5" -> Just GS5.app
2020 "6" -> Just GS6.app
2121 "7" -> Just GS7.app
22+ "8" -> Just GS3.app
2223 _ -> Nothing
2324
2425main :: IO ()
servant-examples/getting-started/gs8-main.hsnew file+4
@@ -0,0 +1,4 @@
1+import GS8
2+
3+main :: IO ()
4+main = run
servant-examples/servant-examples.cabal+13
@@ -32,6 +32,19 @@ executable getting-started
3232 hs-source-dirs: getting-started
3333 default-language: Haskell2010
3434
35+executable gs8-main
36+ main-is: gs8-main.hs
37+ hs-source-dirs: getting-started
38+ default-language: Haskell2010
39+ build-depends:
40+ aeson
41+ , base
42+ , either
43+ , servant
44+ , servant-client
45+ , servant-server
46+ , wai
47+
3548executable hackage
3649 main-is: hackage.hs
3750 build-depends: