servant/servant-client
Paolo Capriotti 19ec395e66
Avoid using SOP constructors directly (#1434)
This is a followup to #1420. It uses `respond` and `matchUnion`, with
the help of some type annotations, instead of the NS constructors from
SOP.
2021-07-13 10:10:30 -05:00
..
src/Servant union verbs (#1314) 2020-10-31 20:45:46 +01:00
test Avoid using SOP constructors directly (#1434) 2021-07-13 10:10:30 -05:00
CHANGELOG.md Prepare 0.18.3 release (#1430) 2021-06-24 00:38:46 +03:00
docs.sh prepare merge 2015-04-20 11:15:58 +02:00
LICENSE Changelog and cabal file edits 2018-11-13 09:58:42 +02:00
README.lhs servant-client: update README.md and test it 2018-04-18 14:18:41 +01:00
README.md servant-client: update README.md and test it 2018-04-18 14:18:41 +01:00
servant-client.cabal Prepare 0.18.3 release (#1430) 2021-06-24 00:38:46 +03:00
Setup.hs stylish haskell changes 2015-08-18 00:07:12 +02:00

servant-client

servant

This library lets you automatically derive Haskell functions that let you query each endpoint of a servant webservice.

Example

{-# 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 '[JSON] Book :> Post '[JSON] Book -- POST /books

myApi :: Proxy MyApi
myApi = Proxy

-- '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