Add EmptyAPIClient and instance HasClient EmptyAPI
This commit is contained in:
parent
94483d586c
commit
4c64c13af0
2 changed files with 12 additions and 1 deletions
|
@ -62,6 +62,7 @@ Enough chitchat, let's see an example. Consider the following API type from the
|
|||
type API = "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Position
|
||||
:<|> "hello" :> QueryParam "name" String :> Get '[JSON] HelloMessage
|
||||
:<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email
|
||||
:<|> EmptyAPI
|
||||
```
|
||||
|
||||
What 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"
|
|||
|
||||
marketing :: ClientInfo -- ^ value for the request body
|
||||
-> ClientM Email
|
||||
|
||||
emptyClient :: EmptyAPIClient
|
||||
```
|
||||
|
||||
Each function makes available as an argument any value that the response may
|
||||
|
@ -88,7 +91,7 @@ the function `client`. It takes one argument:
|
|||
api :: Proxy API
|
||||
api = Proxy
|
||||
|
||||
position :<|> hello :<|> marketing = client api
|
||||
position :<|> hello :<|> marketing :<|> emptyClient = client api
|
||||
```
|
||||
|
||||
`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.
|
||||
|
|
|
@ -24,6 +24,7 @@ module Servant.Client
|
|||
, ClientEnv (ClientEnv)
|
||||
, mkAuthenticateReq
|
||||
, ServantError(..)
|
||||
, EmptyAPIClient(..)
|
||||
, module Servant.Common.BaseUrl
|
||||
) where
|
||||
|
||||
|
@ -88,6 +89,13 @@ instance (HasClient a, HasClient b) => HasClient (a :<|> b) where
|
|||
clientWithRoute (Proxy :: Proxy a) req :<|>
|
||||
clientWithRoute (Proxy :: Proxy b) req
|
||||
|
||||
-- | TODO docs
|
||||
data EmptyAPIClient = EmptyAPIClient
|
||||
|
||||
instance HasClient EmptyAPI where
|
||||
type Client EmptyAPI = EmptyAPIClient
|
||||
clientWithRoute Proxy _ = EmptyAPIClient
|
||||
|
||||
-- | If you use a 'Capture' in one of your endpoints in your API,
|
||||
-- the corresponding querying function will automatically take
|
||||
-- an additional argument of the type specified by your 'Capture'.
|
||||
|
|
Loading…
Reference in a new issue