on master help sign in
Merge pull request #727 from declension/606-fix-client-documentation

Improve Client docs

commit7e5eca3d1c1628e3b76cea5f8912feaa13e4d4b3 authorOleg Grenrus <oleg.grenrus@iki.fi> dateFri 5 May 2017, 10:19 (9 years ago) parentd004805fa422bb
2 files changed+8−9
doc/tutorial/Client.lhs+7−8 doc/tutorial/index.rst+1−1
doc/tutorial/Client.lhs+7−8
@@ -1,12 +1,11 @@
11# Querying an API
22
3−While defining handlers that serve an API has a lot to it, querying an API is simpler: we do not care about what happens inside the webserver, we just need to know how to talk to it and get a response back. Except that we usually have to write the querying functions by hand because the structure of the API isn't a first class citizen and can't be inspected to generate a bunch of client-side functions.
3+While defining handlers that [serve an API](Server.lhs) has a lot to it, querying an API is simpler: we do not care about what happens inside the webserver, we just need to know how to talk to it and get a response back. That said, we usually have to write the querying functions by hand because the structure of the API isn't a first class citizen and can't be inspected to generate the client-side functions.
44
5−**servant** however has a way to inspect APIs, because APIs are just Haskell types and (GHC) Haskell lets us do quite a few things with types. In the same way that we look at an API type to deduce the types the handlers should have, we can inspect the structure of the API to *derive* Haskell functions that take one argument for each occurence of `Capture`, `ReqBody`, `QueryParam`
6−and friends. By *derive*, we mean that there's no code generation involved, the functions are defined just by the structure of the API type.
5+**servant** however has a way to inspect APIs, because APIs are just Haskell types and (GHC) Haskell lets us do quite a few things with types. In the same way that we look at an API type to deduce the types the handlers should have, we can inspect the structure of the API to *derive* Haskell functions that take one argument for each occurrence of `Capture`, `ReqBody`, `QueryParam`
6+and friends (see [the tutorial introduction](ApiType.lhs) for an overview). By *derive*, we mean that there's no code generation involved - the functions are defined just by the structure of the API type.
77
8−The source for this tutorial section is a literate haskell file, so first we
9−need to have some language extensions and imports:
8+The source for this tutorial section is a literate Haskell file, so first we need to have some language extensions and imports:
109
1110``` haskell
1211{-# LANGUAGE DataKinds #-}
@@ -65,7 +64,7 @@ type API = "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Posit
6564 :<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email
6665```
6766
68−What we are going to get with **servant-client** here is 3 functions, one to query each endpoint:
67+What we are going to get with **servant-client** here is three functions, one to query each endpoint:
6968
7069``` haskell
7170position :: Int -- ^ value for "x"
@@ -92,7 +91,7 @@ api = Proxy
9291position :<|> hello :<|> marketing = client api
9392```
9493
95−As you can see in the code above, we just "pattern match our way" to these functions. If we try to derive less or more functions than there are endpoints in the API, we obviously get an error. The `BaseUrl` value there is just:
94+`client api` returns client functions for our _entire_ API, combined with `:<|>`, which we can pattern match on as above. You could say `client` "calculates" the correct type and number of client functions for the API type it is given (via a `Proxy`), as well as their implementations.
9695
9796``` haskell ignore
9897-- | URI scheme to use
@@ -135,7 +134,7 @@ run = do
135134
136135Here's the output of the above code running against the appropriate server:
137136
138−``` bash
137+```
139138Position {xCoord = 10, yCoord = 10}
140139HelloMessage {msg = "Hello, servant"}
141140Email {from = "great@company.com", to = "alp@foo.com", subject = "Hey Alp, we miss you!", body = "Hi Alp,\n\nSince you've recently turned 26, have you checked out our latest haskell, mathematics products? Give us a visit!"}
doc/tutorial/index.rst+1−1
@@ -1,7 +1,7 @@
11Tutorial
22========
33
4−This is an introductory tutorial to **servant**.
4+This is an introductory tutorial to **servant**. Whilst browsing is fine, it makes more sense if you read the sections in order, or at least read the first section before anything else.
55
66(Any comments, issues or feedback about the tutorial can be submitted
77to `servant's issue tracker <http://github.com/haskell-servant/servant/issues>`_.)