Sample program more sensible and update README

This commit is contained in:
Thomas Sutton 2015-02-22 17:18:07 +11:00
parent 02c4adfd18
commit bdf61e4df9
2 changed files with 14 additions and 4 deletions

View file

@ -31,6 +31,14 @@ data Greet = Greet { _msg :: Text }
instance FromJSON Greet instance FromJSON Greet
instance ToJSON Greet instance ToJSON Greet
-- we can render a Greeting into JSON using this ToJSON instance
instance MimeRender JSON Greet where
toByteString Proxy = encodePretty
-- or we can render it to HTML
instance MimeRender HTML Greet where
toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
-- we provide a sample value for the 'Greet' type -- we provide a sample value for the 'Greet' type
instance ToSample Greet where instance ToSample Greet where
toSample = Just g toSample = Just g
@ -51,8 +59,8 @@ instance ToCapture (Capture "greetid" Text) where
-- API specification -- API specification
type TestApi = type TestApi =
"hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get Greet "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON,HTML] Greet
:<|> "greet" :> RQBody Greet :> Post Greet :<|> "greet" :> RQBody '[JSON] Greet :> Post '[JSON] Greet
:<|> "delete" :> Capture "greetid" Text :> Delete :<|> "delete" :> Capture "greetid" Text :> Delete
testApi :: Proxy TestApi testApi :: Proxy TestApi

View file

@ -23,9 +23,11 @@ newtype Greet = Greet Text
instance FromJSON Greet instance FromJSON Greet
instance ToJSON Greet instance ToJSON Greet
-- | A 'Greet' value can be rendered to 'JSON'.
instance MimeRender JSON Greet where instance MimeRender JSON Greet where
toByteString Proxy = encodePretty toByteString Proxy = encodePretty
-- | A 'Greet' value can be rendered to 'HTML'.
instance MimeRender HTML Greet where instance MimeRender HTML Greet where
toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>" toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
@ -74,12 +76,12 @@ intro2 = DocIntro "This title is below the last"
-- API specification -- API specification
type TestApi = type TestApi =
-- GET /hello/:name?capital={true, false} returns a Greet as JSON -- GET /hello/:name?capital={true, false} returns a Greet as JSON or HTML
"hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON, HTML] Greet "hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON, HTML] Greet
-- POST /greet with a Greet as JSON in the request body, -- POST /greet with a Greet as JSON in the request body,
-- returns a Greet as JSON -- returns a Greet as JSON
:<|> "greet" :> ReqBody '[JSON,HTML] Greet :> Post '[JSON] Greet :<|> "greet" :> ReqBody '[JSON] Greet :> Post '[JSON] Greet
-- DELETE /greet/:greetid -- DELETE /greet/:greetid
:<|> "greet" :> Capture "greetid" Text :> Delete :<|> "greet" :> Capture "greetid" Text :> Delete