Merge pull request #948 from domenkozar/servant-client-readme

servant-client: update README.md and test it
This commit is contained in:
Oleg Grenrus 2018-04-20 22:56:15 +03:00 committed by GitHub
commit 63253f09df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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 ## Example
``` haskell ``` 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 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 MyApi
myApi = Proxy 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. -- 'client' allows you to produce operations to query an API from a client.
postNewBook :: Book -> ClientM Book
getAllBooks :: ClientM [Book]
(getAllBooks :<|> postNewBook) = client myApi (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: build-tool-depends:
hspec-discover:hspec-discover >= 2.4.4 && < 2.6 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