on master help sign in
Sample program more sensible and update README
bdf61e4Thomas Sutton
README.md+10−2
@@ -31,6 +31,14 @@ data Greet = Greet { _msg :: Text }
31instance FromJSON Greet
31instance FromJSON Greet
32instance ToJSON Greet
32instance ToJSON Greet
33
33
34+-- we can render a Greeting into JSON using this ToJSON instance
35+instance MimeRender JSON Greet where
36+ toByteString Proxy = encodePretty
37+
38+-- or we can render it to HTML
39+instance MimeRender HTML Greet where
40+ toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
41+
34-- we provide a sample value for the 'Greet' type
42-- we provide a sample value for the 'Greet' type
35instance ToSample Greet where
43instance ToSample Greet where
36 toSample = Just g
44 toSample = Just g
@@ -51,8 +59,8 @@ instance ToCapture (Capture "greetid" Text) where
51
59
52-- API specification
60-- API specification
53type TestApi =
61type TestApi =
54− "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get Greet
62+ "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON,HTML] Greet
55− :<|> "greet" :> RQBody Greet :> Post Greet
63+ :<|> "greet" :> RQBody '[JSON] Greet :> Post '[JSON] Greet
56 :<|> "delete" :> Capture "greetid" Text :> Delete
64 :<|> "delete" :> Capture "greetid" Text :> Delete
57
65
58testApi :: Proxy TestApi
66testApi :: Proxy TestApi
example/greet.hs+4−2
@@ -23,9 +23,11 @@ newtype Greet = Greet Text
23instance FromJSON Greet
23instance FromJSON Greet
24instance ToJSON Greet
24instance ToJSON Greet
25
25
26+-- | A 'Greet' value can be rendered to 'JSON'.
26instance MimeRender JSON Greet where
27instance MimeRender JSON Greet where
27 toByteString Proxy = encodePretty
28 toByteString Proxy = encodePretty
28
29
30+-- | A 'Greet' value can be rendered to 'HTML'.
29instance MimeRender HTML Greet where
31instance MimeRender HTML Greet where
30 toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
32 toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
31
33
@@ -74,12 +76,12 @@ intro2 = DocIntro "This title is below the last"
74
76
75-- API specification
77-- API specification
76type TestApi =
78type TestApi =
77− -- GET /hello/:name?capital={true, false} returns a Greet as JSON
79+ -- GET /hello/:name?capital={true, false} returns a Greet as JSON or HTML
78 "hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON, HTML] Greet
80 "hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON, HTML] Greet
79
81
80 -- POST /greet with a Greet as JSON in the request body,
82 -- POST /greet with a Greet as JSON in the request body,
81 -- returns a Greet as JSON
83 -- returns a Greet as JSON
82− :<|> "greet" :> ReqBody '[JSON,HTML] Greet :> Post '[JSON] Greet
84+ :<|> "greet" :> ReqBody '[JSON] Greet :> Post '[JSON] Greet
83
85
84 -- DELETE /greet/:greetid
86 -- DELETE /greet/:greetid
85 :<|> "greet" :> Capture "greetid" Text :> Delete
87 :<|> "greet" :> Capture "greetid" Text :> Delete