git.iko.soy/typeable/servant
on master help sign in
Add EmptyAPIClient and `instance HasClient EmptyAPI`
commit4c64c13af0a2300d721a8e4e55d9652fc4d97e11 authorDavid Turner <dave.c.turner@gmail.com> dateTue 16 May 2017, 09:42 (9 years ago) parent94483d5
2 files changed+12−1
doc/tutorial/Client.lhs+4−1 servant-client/src/Servant/Client.hs+8
doc/tutorial/Client.lhs+4−1
@@ -62,6 +62,7 @@ Enough chitchat, let's see an example. Consider the following API type from the
6262type API = "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Position
6363 :<|> "hello" :> QueryParam "name" String :> Get '[JSON] HelloMessage
6464 :<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email
65+ :<|> EmptyAPI
6566```
6667
6768What we are going to get with **servant-client** here is three functions, one to query each endpoint:
@@ -76,6 +77,8 @@ hello :: Maybe String -- ^ an optional value for "name"
7677
7778marketing :: ClientInfo -- ^ value for the request body
7879 -> ClientM Email
80+
81+emptyClient :: EmptyAPIClient
7982```
8083
8184Each function makes available as an argument any value that the response may
@@ -88,7 +91,7 @@ the function `client`. It takes one argument:
8891api :: Proxy API
8992api = Proxy
9093
91−position :<|> hello :<|> marketing = client api
94+position :<|> hello :<|> marketing :<|> emptyClient = client api
9295```
9396
9497`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.
servant-client/src/Servant/Client.hs+8
@@ -24,6 +24,7 @@ module Servant.Client
2424 , ClientEnv (ClientEnv)
2525 , mkAuthenticateReq
2626 , ServantError(..)
27+ , EmptyAPIClient(..)
2728 , module Servant.Common.BaseUrl
2829 ) where
2930
@@ -88,6 +89,13 @@ instance (HasClient a, HasClient b) => HasClient (a :<|> b) where
8889 clientWithRoute (Proxy :: Proxy a) req :<|>
8990 clientWithRoute (Proxy :: Proxy b) req
9091
92+-- | TODO docs
93+data EmptyAPIClient = EmptyAPIClient
94+
95+instance HasClient EmptyAPI where
96+ type Client EmptyAPI = EmptyAPIClient
97+ clientWithRoute Proxy _ = EmptyAPIClient
98+
9199-- | If you use a 'Capture' in one of your endpoints in your API,
92100-- the corresponding querying function will automatically take
93101-- an additional argument of the type specified by your 'Capture'.