2016-02-18 18:13:43 +01:00
# Querying an API
2016-01-25 14:11:40 +01:00
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.
2016-02-28 22:16:43 +01:00
**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`
2016-01-25 14:11:40 +01:00
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.
The source for this tutorial section is a literate haskell file, so first we
need to have some language extensions and imports:
2016-01-27 22:28:58 +01:00
``` haskell
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeOperators #-}
module Client where
2016-03-30 23:50:29 +02:00
import Control.Monad.Trans.Except (ExceptT, runExceptT)
2016-01-27 22:28:58 +01:00
import Data.Aeson
import Data.Proxy
import GHC.Generics
2016-03-30 23:50:29 +02:00
import Network.HTTP.Client (Manager, newManager, defaultManagerSettings)
2016-01-27 22:28:58 +01:00
import Servant.API
import Servant.Client
```
2016-01-25 14:11:40 +01:00
Also, we need examples for some domain specific data types:
2016-01-27 22:28:58 +01:00
``` haskell
data Position = Position
{ x :: Int
, y :: Int
} deriving (Show, Generic)
instance FromJSON Position
newtype HelloMessage = HelloMessage { msg :: String }
deriving (Show, Generic)
instance FromJSON HelloMessage
data ClientInfo = ClientInfo
{ clientName :: String
, clientEmail :: String
, clientAge :: Int
, clientInterestedIn :: [String]
} deriving Generic
instance ToJSON ClientInfo
data Email = Email
{ from :: String
, to :: String
, subject :: String
, body :: String
} deriving (Show, Generic)
instance FromJSON Email
```
2016-01-25 14:11:40 +01:00
Enough chitchat, let's see an example. Consider the following API type from the previous section:
2016-01-27 22:28:58 +01:00
``` haskell
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
```
2016-01-25 14:11:40 +01:00
2016-02-28 22:16:43 +01:00
What we are going to get with **servant-client** here is 3 functions, one to query each endpoint:
2016-01-25 14:11:40 +01:00
2016-01-27 22:28:58 +01:00
``` haskell
position :: Int -- ^ value for "x"
-> Int -- ^ value for "y"
2016-03-30 23:50:29 +02:00
-> Manager -- ^ the HTTP client to use
-> BaseUrl -- ^ the URL at which the API can be found
-> ExceptT ServantError IO Position
2016-01-27 22:28:58 +01:00
hello :: Maybe String -- ^ an optional value for "name"
2016-03-30 23:50:29 +02:00
-> Manager -- ^ the HTTP client to use
-> BaseUrl -- ^ the URL at which the API can be found
-> ExceptT ServantError IO HelloMessage
2016-01-27 22:28:58 +01:00
marketing :: ClientInfo -- ^ value for the request body
2016-03-30 23:50:29 +02:00
-> Manager -- ^ the HTTP client to use
-> BaseUrl -- ^ the URL at which the API can be found
-> ExceptT ServantError IO Email
2016-01-27 22:28:58 +01:00
```
2016-01-25 14:11:40 +01:00
2016-02-28 22:16:43 +01:00
Each function makes available as an argument any value that the response may
depend on, as evidenced in the API type. How do we get these functions? By calling
2016-03-28 16:27:51 +02:00
the function `client`. It takes one argument:
2016-02-28 22:16:43 +01:00
- a `Proxy` to your API,
2016-01-25 14:11:40 +01:00
2016-01-27 22:28:58 +01:00
``` haskell
api :: Proxy API
api = Proxy
2016-03-28 16:27:51 +02:00
position :<|> hello :<|> marketing = client api
2016-01-27 22:28:58 +01:00
```
2016-01-25 14:11:40 +01:00
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:
2016-01-27 22:26:59 +01:00
``` haskell ignore
2016-01-25 14:11:40 +01:00
-- | URI scheme to use
data Scheme =
Http -- ^ http://
| Https -- ^ https://
deriving
-- | Simple data type to represent the target of HTTP requests
-- for servant's automatically-generated clients.
data BaseUrl = BaseUrl
{ baseUrlScheme :: Scheme -- ^ URI scheme to use
, baseUrlHost :: String -- ^ host (eg "haskell.org")
, baseUrlPort :: Int -- ^ port (eg 80)
}
```
That's it. Let's now write some code that uses our client functions.
2016-01-27 22:28:58 +01:00
``` haskell
2016-03-30 23:50:29 +02:00
queries :: Manager -> BaseUrl -> ExceptT ServantError IO (Position, HelloMessage, Email)
queries manager baseurl = do
pos <- position 10 10 manager baseurl
message <- hello (Just "servant") manager baseurl
em <- marketing (ClientInfo "Alp" "alp@foo.com" 26 ["haskell", "mathematics"]) manager baseurl
2016-01-28 13:22:20 +01:00
return (pos, message, em)
2016-01-27 22:28:58 +01:00
run :: IO ()
run = do
2016-03-28 16:27:51 +02:00
manager <- newManager defaultManagerSettings
2016-03-30 23:50:29 +02:00
res <- runExceptT (queries manager (BaseUrl Http "localhost" 8081 ""))
2016-01-27 22:28:58 +01:00
case res of
Left err -> putStrLn $ "Error: " ++ show err
2016-01-28 13:22:20 +01:00
Right (pos, message, em) -> do
2016-01-27 22:28:58 +01:00
print pos
2016-01-28 13:22:20 +01:00
print message
2016-01-27 22:28:58 +01:00
print em
```
2016-01-25 14:11:40 +01:00
2016-02-28 22:16:43 +01:00
Here's the output of the above code running against the appropriate server:
2016-01-25 14:11:40 +01:00
``` bash
2016-02-28 22:16:43 +01:00
Position {x = 10, y = 10}
HelloMessage {msg = "Hello, servant"}
Email {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!"}
2016-01-25 14:11:40 +01:00
```
2016-02-28 22:16:43 +01:00
The types of the arguments for the functions are the same as for (server-side) request handlers. You now know how to use **servant-client**!