525b74c6f4
- Update GHC-8.6.2 to GHC-8.6.3 - Build cookbook only with GHC-8.4.4 and GHC-8.6.3, thus making 8.0 and 8.2 jobs a little faster |
||
---|---|---|
.. | ||
src/Servant | ||
test | ||
CHANGELOG.md | ||
docs.sh | ||
LICENSE | ||
README.lhs | ||
README.md | ||
servant-client.cabal | ||
Setup.hs |
servant-client
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