servant-client: update README.md and test it

This commit is contained in:
Domen Kožar 2018-04-18 14:01:42 +01:00
parent 6af38354d0
commit 49969695df
No known key found for this signature in database
GPG Key ID: C2FFBCAFD2C24246
3 changed files with 32 additions and 3 deletions

1
servant-client/README.lhs Symbolic link
View File

@ -0,0 +1 @@
README.md

View File

@ -7,14 +7,35 @@ This library lets you automatically derive Haskell functions that let you query
## Example
``` haskell
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
import Data.Proxy
import Data.Text
import Network.HTTP.Client (newManager, defaultManagerSettings)
import Servant.API
import Servant.Client
type Book = Text
type MyApi = "books" :> Get '[JSON] [Book] -- GET /books
:<|> "books" :> ReqBody Book :> Post '[JSON] Book -- POST /books
:<|> "books" :> ReqBody '[JSON] Book :> Post '[JSON] Book -- POST /books
myApi :: Proxy MyApi
myApi = Proxy
getAllBooks :: Manager -> BaseUrl -> ExceptT String IO [Book]
postNewBook :: Book -> Manager -> BaseUrl -> ExceptT String IO Book
-- 'client' allows you to produce operations to query an API from a client.
postNewBook :: Book -> ClientM Book
getAllBooks :: ClientM [Book]
(getAllBooks :<|> postNewBook) = client myApi
main :: IO ()
main = do
manager' <- newManager defaultManagerSettings
res <- runClientM getAllBooks (mkClientEnv manager' (BaseUrl Http "localhost" 8081 ""))
case res of
Left err -> putStrLn $ "Error: " ++ show err
Right books -> print books
```

View File

@ -128,3 +128,10 @@ test-suite spec
build-tool-depends:
hspec-discover:hspec-discover >= 2.4.4 && < 2.6
test-suite readme
type: exitcode-stdio-1.0
main-is: README.lhs
build-depends: base, servant, http-client, text, servant-client, markdown-unlit
build-tool-depends: markdown-unlit:markdown-unlit
ghc-options: -pgmL markdown-unlit