on master help sign in
Add EmptyAPIClient and `instance HasClient EmptyAPI`
4c64c13David Turner
doc/tutorial/Client.lhs+4−1
@@ -62,6 +62,7 @@ Enough chitchat, let's see an example. Consider the following API type from the
62type API = "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Position
62type API = "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Position
63 :<|> "hello" :> QueryParam "name" String :> Get '[JSON] HelloMessage
63 :<|> "hello" :> QueryParam "name" String :> Get '[JSON] HelloMessage
64 :<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email
64 :<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email
65+ :<|> EmptyAPI
65```
66```
66
67
67What we are going to get with **servant-client** here is three functions, one to query each endpoint:
68What 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"
76
77
77marketing :: ClientInfo -- ^ value for the request body
78marketing :: ClientInfo -- ^ value for the request body
78 -> ClientM Email
79 -> ClientM Email
80+
81+emptyClient :: EmptyAPIClient
79```
82```
80
83
81Each function makes available as an argument any value that the response may
84Each function makes available as an argument any value that the response may
@@ -88,7 +91,7 @@ the function `client`. It takes one argument:
88api :: Proxy API
91api :: Proxy API
89api = Proxy
92api = Proxy
90
93
91−position :<|> hello :<|> marketing = client api
94+position :<|> hello :<|> marketing :<|> emptyClient = client api
92```
95```
93
96
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.
97`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
24 , ClientEnv (ClientEnv)
24 , ClientEnv (ClientEnv)
25 , mkAuthenticateReq
25 , mkAuthenticateReq
26 , ServantError(..)
26 , ServantError(..)
27+ , EmptyAPIClient(..)
27 , module Servant.Common.BaseUrl
28 , module Servant.Common.BaseUrl
28 ) where
29 ) where
29
30
@@ -88,6 +89,13 @@ instance (HasClient a, HasClient b) => HasClient (a :<|> b) where
88 clientWithRoute (Proxy :: Proxy a) req :<|>
89 clientWithRoute (Proxy :: Proxy a) req :<|>
89 clientWithRoute (Proxy :: Proxy b) req
90 clientWithRoute (Proxy :: Proxy b) req
90
91
92+-- | TODO docs
93+data EmptyAPIClient = EmptyAPIClient
94+
95+instance HasClient EmptyAPI where
96+ type Client EmptyAPI = EmptyAPIClient
97+ clientWithRoute Proxy _ = EmptyAPIClient
98+
91-- | If you use a 'Capture' in one of your endpoints in your API,
99-- | If you use a 'Capture' in one of your endpoints in your API,
92-- the corresponding querying function will automatically take
100-- the corresponding querying function will automatically take
93-- an additional argument of the type specified by your 'Capture'.
101-- an additional argument of the type specified by your 'Capture'.