Skip to content

Commit 4996969

Browse files
committed
servant-client: update README.md and test it
1 parent 6af3835 commit 4996969

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

servant-client/README.lhs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
README.md

servant-client/README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,35 @@ This library lets you automatically derive Haskell functions that let you query
77
## Example
88

99
``` haskell
10+
{-# LANGUAGE DataKinds #-}
11+
{-# LANGUAGE TypeOperators #-}
12+
13+
import Data.Proxy
14+
import Data.Text
15+
import Network.HTTP.Client (newManager, defaultManagerSettings)
16+
import Servant.API
17+
import Servant.Client
18+
19+
20+
type Book = Text
21+
1022
type MyApi = "books" :> Get '[JSON] [Book] -- GET /books
11-
:<|> "books" :> ReqBody Book :> Post '[JSON] Book -- POST /books
23+
:<|> "books" :> ReqBody '[JSON] Book :> Post '[JSON] Book -- POST /books
1224

1325
myApi :: Proxy MyApi
1426
myApi = Proxy
1527

16-
getAllBooks :: Manager -> BaseUrl -> ExceptT String IO [Book]
17-
postNewBook :: Book -> Manager -> BaseUrl -> ExceptT String IO Book
1828
-- 'client' allows you to produce operations to query an API from a client.
29+
postNewBook :: Book -> ClientM Book
30+
getAllBooks :: ClientM [Book]
1931
(getAllBooks :<|> postNewBook) = client myApi
32+
33+
34+
main :: IO ()
35+
main = do
36+
manager' <- newManager defaultManagerSettings
37+
res <- runClientM getAllBooks (mkClientEnv manager' (BaseUrl Http "localhost" 8081 ""))
38+
case res of
39+
Left err -> putStrLn $ "Error: " ++ show err
40+
Right books -> print books
2041
```

servant-client/servant-client.cabal

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,10 @@ test-suite spec
128128

129129
build-tool-depends:
130130
hspec-discover:hspec-discover >= 2.4.4 && < 2.6
131+
132+
test-suite readme
133+
type: exitcode-stdio-1.0
134+
main-is: README.lhs
135+
build-depends: base, servant, http-client, text, servant-client, markdown-unlit
136+
build-tool-depends: markdown-unlit:markdown-unlit
137+
ghc-options: -pgmL markdown-unlit

0 commit comments

Comments
 (0)