git.iko.soy/typeable/servant
on master help sign in
examples: add GS8
0000189Alp Mestanogullari
servant-examples/getting-started/GS3.hs+8−4
@@ -14,13 +14,15 @@ import Servant
14data Position = Position
14data Position = Position
15 { x :: Int
15 { x :: Int
16 , y :: Int
16 , y :: Int
17− } deriving Generic
17+ } deriving (Show, Generic)
18
18
19+instance FromJSON Position
19instance ToJSON Position
20instance ToJSON Position
20
21
21newtype HelloMessage = HelloMessage { msg :: String }
22newtype HelloMessage = HelloMessage { msg :: String }
22− deriving Generic
23+ deriving (Show, Generic)
23
24
25+instance FromJSON HelloMessage
24instance ToJSON HelloMessage
26instance ToJSON HelloMessage
25
27
26data ClientInfo = ClientInfo
28data ClientInfo = ClientInfo
@@ -28,17 +30,19 @@ data ClientInfo = ClientInfo
28 , email :: String
30 , email :: String
29 , age :: Int
31 , age :: Int
30 , interested_in :: [String]
32 , interested_in :: [String]
31− } deriving Generic
33+ } deriving (Show, Generic)
32
34
33instance FromJSON ClientInfo
35instance FromJSON ClientInfo
36+instance ToJSON ClientInfo
34
37
35data Email = Email
38data Email = Email
36 { from :: String
39 { from :: String
37 , to :: String
40 , to :: String
38 , subject :: String
41 , subject :: String
39 , body :: String
42 , body :: String
40− } deriving Generic
43+ } deriving (Show, Generic)
41
44
45+instance FromJSON Email
42instance ToJSON Email
46instance ToJSON Email
43
47
44emailForClient :: ClientInfo -> Email
48emailForClient :: 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
19 "5" -> Just GS5.app
19 "5" -> Just GS5.app
20 "6" -> Just GS6.app
20 "6" -> Just GS6.app
21 "7" -> Just GS7.app
21 "7" -> Just GS7.app
22+ "8" -> Just GS3.app
22 _ -> Nothing
23 _ -> Nothing
23
24
24main :: IO ()
25main :: 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
32 hs-source-dirs: getting-started
32 hs-source-dirs: getting-started
33 default-language: Haskell2010
33 default-language: Haskell2010
34
34
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+
35executable hackage
48executable hackage
36 main-is: hackage.hs
49 main-is: hackage.hs
37 build-depends:
50 build-depends: