on master help sign in
Merge pull request #727 from declension/606-fix-client-documentation
7e5eca3Oleg Grenrus
doc/tutorial/Client.lhs+7−8
@@ -1,12 +1,11 @@
1# Querying an API
1# Querying an API
2
2
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.
4
4
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`
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. By *derive*, we mean that there's no code generation involved, the functions are defined just by the structure of the API type.
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.
7
7
8−The source for this tutorial section is a literate haskell file, so first we
8+The source for this tutorial section is a literate Haskell file, so first we need to have some language extensions and imports:
9−need to have some language extensions and imports:
10
9
11``` haskell
10``` haskell
12{-# LANGUAGE DataKinds #-}
11{-# LANGUAGE DataKinds #-}
@@ -65,7 +64,7 @@ type API = "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Posit
65 :<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email
64 :<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email
66```
65```
67
66
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:
69
68
70``` haskell
69``` haskell
71position :: Int -- ^ value for "x"
70position :: Int -- ^ value for "x"
@@ -92,7 +91,7 @@ api = Proxy
92position :<|> hello :<|> marketing = client api
91position :<|> hello :<|> marketing = client api
93```
92```
94
93
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.
96
95
97``` haskell ignore
96``` haskell ignore
98-- | URI scheme to use
97-- | URI scheme to use
@@ -135,7 +134,7 @@ run = do
135
134
136Here's the output of the above code running against the appropriate server:
135Here's the output of the above code running against the appropriate server:
137
136
138−``` bash
137+```
139Position {xCoord = 10, yCoord = 10}
138Position {xCoord = 10, yCoord = 10}
140HelloMessage {msg = "Hello, servant"}
139HelloMessage {msg = "Hello, servant"}
141Email {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!"}
140Email {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 @@
1Tutorial
1Tutorial
2========
2========
3
3
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.
5
5
6(Any comments, issues or feedback about the tutorial can be submitted
6(Any comments, issues or feedback about the tutorial can be submitted
7to `servant's issue tracker <http://github.com/haskell-servant/servant/issues>`_.)
7to `servant's issue tracker <http://github.com/haskell-servant/servant/issues>`_.)