on master help sign in
Merge pull request #948 from domenkozar/servant-client-readme
63253f0Oleg Grenrus
servant-client/README.lhsnew file+1
@@ -0,0 +1 @@
1+README.md
servant-client/README.md+24−3
@@ -7,14 +7,35 @@ This library lets you automatically derive Haskell functions that let you query
7## Example
7## Example
8
8
9``` haskell
9``` 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+
10type MyApi = "books" :> Get '[JSON] [Book] -- GET /books
22type 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
12
24
13myApi :: Proxy MyApi
25myApi :: Proxy MyApi
14myApi = Proxy
26myApi = Proxy
15
27
16−getAllBooks :: Manager -> BaseUrl -> ExceptT String IO [Book]
17−postNewBook :: Book -> Manager -> BaseUrl -> ExceptT String IO Book
18-- 'client' allows you to produce operations to query an API from a client.
28-- 'client' allows you to produce operations to query an API from a client.
29+postNewBook :: Book -> ClientM Book
30+getAllBooks :: ClientM [Book]
19(getAllBooks :<|> postNewBook) = client myApi
31(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
20```
41```
servant-client/servant-client.cabal+7
@@ -128,3 +128,10 @@ test-suite spec
128
128
129 build-tool-depends:
129 build-tool-depends:
130 hspec-discover:hspec-discover >= 2.4.4 && < 2.6
130 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