2015-04-20 19:52:29 +02:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE InstanceSigs #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2015-11-27 02:05:34 +01:00
|
|
|
{-# LANGUAGE PolyKinds #-}
|
2015-04-20 19:52:29 +02:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2015-05-17 15:51:49 +02:00
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
2015-12-27 17:54:29 +01:00
|
|
|
|
|
|
|
#include "overlapping-compat.h"
|
2014-11-27 18:28:01 +01:00
|
|
|
-- | This module provides 'client' which can automatically generate
|
|
|
|
-- querying functions for each endpoint just from the type representing your
|
|
|
|
-- API.
|
|
|
|
module Servant.Client
|
2016-02-17 21:45:08 +01:00
|
|
|
( AuthClientData
|
|
|
|
, AuthenticateReq(..)
|
|
|
|
, client
|
2014-11-27 18:28:01 +01:00
|
|
|
, HasClient(..)
|
2016-02-17 21:45:08 +01:00
|
|
|
, mkAuthenticateReq
|
2015-03-05 02:46:35 +01:00
|
|
|
, ServantError(..)
|
2014-11-27 18:28:01 +01:00
|
|
|
, module Servant.Common.BaseUrl
|
|
|
|
) where
|
|
|
|
|
2015-05-02 03:21:03 +02:00
|
|
|
#if !MIN_VERSION_base(4,8,0)
|
|
|
|
import Control.Applicative ((<$>))
|
|
|
|
#endif
|
2015-09-12 14:11:24 +02:00
|
|
|
import Control.Monad.Trans.Except
|
2015-04-20 19:52:29 +02:00
|
|
|
import Data.ByteString.Lazy (ByteString)
|
|
|
|
import Data.List
|
|
|
|
import Data.Proxy
|
|
|
|
import Data.String.Conversions
|
|
|
|
import Data.Text (unpack)
|
|
|
|
import GHC.TypeLits
|
2015-09-28 16:34:53 +02:00
|
|
|
import Network.HTTP.Client (Response, Manager)
|
2015-04-20 19:52:29 +02:00
|
|
|
import Network.HTTP.Media
|
|
|
|
import qualified Network.HTTP.Types as H
|
2015-05-02 03:21:03 +02:00
|
|
|
import qualified Network.HTTP.Types.Header as HTTP
|
2015-04-20 19:52:29 +02:00
|
|
|
import Servant.API
|
2016-02-17 21:45:08 +01:00
|
|
|
import Servant.Common.Auth
|
2015-04-20 19:52:29 +02:00
|
|
|
import Servant.Common.BaseUrl
|
2016-02-17 20:12:47 +01:00
|
|
|
import Servant.Common.BasicAuth
|
2015-04-20 19:52:29 +02:00
|
|
|
import Servant.Common.Req
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- * Accessing APIs as a Client
|
|
|
|
|
|
|
|
-- | 'client' allows you to produce operations to query an API from a client.
|
|
|
|
--
|
2015-05-14 00:52:37 +02:00
|
|
|
-- > type MyApi = "books" :> Get '[JSON] [Book] -- GET /books
|
2015-11-27 02:05:34 +01:00
|
|
|
-- > :<|> "books" :> ReqBody '[JSON] Book :> Post '[JSON] Book -- POST /books
|
2014-11-27 18:28:01 +01:00
|
|
|
-- >
|
|
|
|
-- > myApi :: Proxy MyApi
|
|
|
|
-- > myApi = Proxy
|
|
|
|
-- >
|
2015-09-12 14:11:24 +02:00
|
|
|
-- > getAllBooks :: ExceptT String IO [Book]
|
|
|
|
-- > postNewBook :: Book -> ExceptT String IO Book
|
2015-09-28 16:34:53 +02:00
|
|
|
-- > (getAllBooks :<|> postNewBook) = client myApi host manager
|
2015-05-14 00:52:37 +02:00
|
|
|
-- > where host = BaseUrl Http "localhost" 8080
|
2015-09-28 16:34:53 +02:00
|
|
|
client :: HasClient layout => Proxy layout -> BaseUrl -> Manager -> Client layout
|
2015-05-08 20:47:19 +02:00
|
|
|
client p baseurl = clientWithRoute p defReq baseurl
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- | This class lets us define how each API combinator
|
2014-12-01 13:41:12 +01:00
|
|
|
-- influences the creation of an HTTP request. It's mostly
|
|
|
|
-- an internal class, you can just use 'client'.
|
2014-11-27 18:28:01 +01:00
|
|
|
class HasClient layout where
|
2015-05-09 01:51:23 +02:00
|
|
|
type Client layout :: *
|
2015-09-28 16:34:53 +02:00
|
|
|
clientWithRoute :: Proxy layout -> Req -> BaseUrl -> Manager -> Client layout
|
2015-03-09 21:50:30 +01:00
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- | A client querying function for @a ':<|>' b@ will actually hand you
|
|
|
|
-- one function for querying @a@ and another one for querying @b@,
|
|
|
|
-- stitching them together with ':<|>', which really is just like a pair.
|
|
|
|
--
|
2015-05-14 00:52:37 +02:00
|
|
|
-- > type MyApi = "books" :> Get '[JSON] [Book] -- GET /books
|
|
|
|
-- > :<|> "books" :> ReqBody '[JSON] Book :> Post Book -- POST /books
|
2014-11-27 18:28:01 +01:00
|
|
|
-- >
|
|
|
|
-- > myApi :: Proxy MyApi
|
|
|
|
-- > myApi = Proxy
|
|
|
|
-- >
|
2015-09-12 14:11:24 +02:00
|
|
|
-- > getAllBooks :: ExceptT String IO [Book]
|
|
|
|
-- > postNewBook :: Book -> ExceptT String IO Book
|
2015-09-28 16:34:53 +02:00
|
|
|
-- > (getAllBooks :<|> postNewBook) = client myApi host manager
|
2015-05-14 00:52:37 +02:00
|
|
|
-- > where host = BaseUrl Http "localhost" 8080
|
2014-11-27 18:28:01 +01:00
|
|
|
instance (HasClient a, HasClient b) => HasClient (a :<|> b) where
|
2015-05-09 01:51:23 +02:00
|
|
|
type Client (a :<|> b) = Client a :<|> Client b
|
2015-09-28 16:34:53 +02:00
|
|
|
clientWithRoute Proxy req baseurl manager =
|
|
|
|
clientWithRoute (Proxy :: Proxy a) req baseurl manager :<|>
|
|
|
|
clientWithRoute (Proxy :: Proxy b) req baseurl manager
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- | 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'.
|
|
|
|
-- That function will take care of inserting a textual representation
|
|
|
|
-- of this value at the right place in the request path.
|
|
|
|
--
|
|
|
|
-- You can control how values for this type are turned into
|
2015-10-07 23:38:47 +02:00
|
|
|
-- text by specifying a 'ToHttpApiData' instance for your type.
|
2014-11-27 18:28:01 +01:00
|
|
|
--
|
|
|
|
-- Example:
|
|
|
|
--
|
2015-05-14 00:52:37 +02:00
|
|
|
-- > type MyApi = "books" :> Capture "isbn" Text :> Get '[JSON] Book
|
2014-11-27 18:28:01 +01:00
|
|
|
-- >
|
|
|
|
-- > myApi :: Proxy MyApi
|
|
|
|
-- > myApi = Proxy
|
|
|
|
-- >
|
2015-09-12 14:11:24 +02:00
|
|
|
-- > getBook :: Text -> ExceptT String IO Book
|
2015-09-28 16:34:53 +02:00
|
|
|
-- > getBook = client myApi host manager
|
2015-05-14 00:52:37 +02:00
|
|
|
-- > where host = BaseUrl Http "localhost" 8080
|
2014-11-27 18:28:01 +01:00
|
|
|
-- > -- then you can just use "getBook" to query that endpoint
|
2015-10-07 23:38:47 +02:00
|
|
|
instance (KnownSymbol capture, ToHttpApiData a, HasClient sublayout)
|
2014-11-27 18:28:01 +01:00
|
|
|
=> HasClient (Capture capture a :> sublayout) where
|
|
|
|
|
2015-05-09 01:51:23 +02:00
|
|
|
type Client (Capture capture a :> sublayout) =
|
2015-05-03 01:45:17 +02:00
|
|
|
a -> Client sublayout
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2015-09-28 16:34:53 +02:00
|
|
|
clientWithRoute Proxy req baseurl manager val =
|
2015-04-24 17:51:30 +02:00
|
|
|
clientWithRoute (Proxy :: Proxy sublayout)
|
|
|
|
(appendToPath p req)
|
|
|
|
baseurl
|
2015-09-28 16:34:53 +02:00
|
|
|
manager
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2015-10-07 23:38:47 +02:00
|
|
|
where p = unpack (toUrlPiece val)
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2015-12-27 17:54:29 +01:00
|
|
|
instance OVERLAPPABLE_
|
2015-11-27 02:05:34 +01:00
|
|
|
-- Note [Non-Empty Content Types]
|
|
|
|
(MimeUnrender ct a, ReflectMethod method, cts' ~ (ct ': cts)
|
|
|
|
) => HasClient (Verb method status cts' a) where
|
|
|
|
type Client (Verb method status cts' a) = ExceptT ServantError IO a
|
2015-09-28 16:34:53 +02:00
|
|
|
clientWithRoute Proxy req baseurl manager =
|
2015-11-27 02:05:34 +01:00
|
|
|
snd <$> performRequestCT (Proxy :: Proxy ct) method req baseurl manager
|
|
|
|
where method = reflectMethod (Proxy :: Proxy method)
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2015-12-27 17:54:29 +01:00
|
|
|
instance OVERLAPPING_
|
2016-01-07 14:30:08 +01:00
|
|
|
(ReflectMethod method) => HasClient (Verb method status cts NoContent) where
|
|
|
|
type Client (Verb method status cts NoContent) = ExceptT ServantError IO NoContent
|
2015-09-28 16:34:53 +02:00
|
|
|
clientWithRoute Proxy req baseurl manager =
|
2016-01-07 14:30:08 +01:00
|
|
|
performRequestNoBody method req baseurl manager >> return NoContent
|
2015-11-27 02:05:34 +01:00
|
|
|
where method = reflectMethod (Proxy :: Proxy method)
|
2015-05-06 21:21:35 +02:00
|
|
|
|
2015-12-27 17:54:29 +01:00
|
|
|
instance OVERLAPPING_
|
2015-11-27 02:05:34 +01:00
|
|
|
-- Note [Non-Empty Content Types]
|
|
|
|
( MimeUnrender ct a, BuildHeadersTo ls, ReflectMethod method, cts' ~ (ct ': cts)
|
|
|
|
) => HasClient (Verb method status cts' (Headers ls a)) where
|
|
|
|
type Client (Verb method status cts' (Headers ls a))
|
|
|
|
= ExceptT ServantError IO (Headers ls a)
|
2015-09-28 16:34:53 +02:00
|
|
|
clientWithRoute Proxy req baseurl manager = do
|
2015-11-27 02:05:34 +01:00
|
|
|
let method = reflectMethod (Proxy :: Proxy method)
|
|
|
|
(hdrs, resp) <- performRequestCT (Proxy :: Proxy ct) method req baseurl manager
|
2015-05-06 21:21:35 +02:00
|
|
|
return $ Headers { getResponse = resp
|
|
|
|
, getHeadersHList = buildHeadersTo hdrs
|
|
|
|
}
|
|
|
|
|
2015-12-27 17:54:29 +01:00
|
|
|
instance OVERLAPPING_
|
2015-11-27 02:05:34 +01:00
|
|
|
( BuildHeadersTo ls, ReflectMethod method
|
2016-01-07 14:30:08 +01:00
|
|
|
) => HasClient (Verb method status cts (Headers ls NoContent)) where
|
|
|
|
type Client (Verb method status cts (Headers ls NoContent))
|
|
|
|
= ExceptT ServantError IO (Headers ls NoContent)
|
2015-09-28 16:34:53 +02:00
|
|
|
clientWithRoute Proxy req baseurl manager = do
|
2015-11-27 02:05:34 +01:00
|
|
|
let method = reflectMethod (Proxy :: Proxy method)
|
|
|
|
hdrs <- performRequestNoBody method req baseurl manager
|
2016-01-07 14:30:08 +01:00
|
|
|
return $ Headers { getResponse = NoContent
|
2015-05-02 03:21:03 +02:00
|
|
|
, getHeadersHList = buildHeadersTo hdrs
|
|
|
|
}
|
|
|
|
|
2015-11-27 02:05:34 +01:00
|
|
|
|
2014-12-08 12:52:30 +01:00
|
|
|
-- | If you use a 'Header' in one of your endpoints in your API,
|
|
|
|
-- the corresponding querying function will automatically take
|
|
|
|
-- an additional argument of the type specified by your 'Header',
|
|
|
|
-- wrapped in Maybe.
|
|
|
|
--
|
|
|
|
-- That function will take care of encoding this argument as Text
|
|
|
|
-- in the request headers.
|
|
|
|
--
|
2015-10-07 23:38:47 +02:00
|
|
|
-- All you need is for your type to have a 'ToHttpApiData' instance.
|
2014-12-08 12:52:30 +01:00
|
|
|
--
|
|
|
|
-- Example:
|
|
|
|
--
|
2015-05-14 00:52:37 +02:00
|
|
|
-- > newtype Referer = Referer { referrer :: Text }
|
2015-10-07 23:38:47 +02:00
|
|
|
-- > deriving (Eq, Show, Generic, FromText, ToHttpApiData)
|
2014-12-08 12:52:30 +01:00
|
|
|
-- >
|
|
|
|
-- > -- GET /view-my-referer
|
2015-05-14 00:52:37 +02:00
|
|
|
-- > type MyApi = "view-my-referer" :> Header "Referer" Referer :> Get '[JSON] Referer
|
2014-12-08 12:52:30 +01:00
|
|
|
-- >
|
|
|
|
-- > myApi :: Proxy MyApi
|
|
|
|
-- > myApi = Proxy
|
|
|
|
-- >
|
2015-09-12 14:11:24 +02:00
|
|
|
-- > viewReferer :: Maybe Referer -> ExceptT String IO Book
|
2015-05-14 00:52:37 +02:00
|
|
|
-- > viewReferer = client myApi host
|
|
|
|
-- > where host = BaseUrl Http "localhost" 8080
|
2014-12-08 12:52:30 +01:00
|
|
|
-- > -- then you can just use "viewRefer" to query that endpoint
|
2015-05-14 00:52:37 +02:00
|
|
|
-- > -- specifying Nothing or e.g Just "http://haskell.org/" as arguments
|
2015-10-07 23:38:47 +02:00
|
|
|
instance (KnownSymbol sym, ToHttpApiData a, HasClient sublayout)
|
2014-12-08 12:52:30 +01:00
|
|
|
=> HasClient (Header sym a :> sublayout) where
|
|
|
|
|
2015-05-09 01:51:23 +02:00
|
|
|
type Client (Header sym a :> sublayout) =
|
2015-05-03 01:45:17 +02:00
|
|
|
Maybe a -> Client sublayout
|
2014-12-08 12:52:30 +01:00
|
|
|
|
2015-09-28 16:34:53 +02:00
|
|
|
clientWithRoute Proxy req baseurl manager mval =
|
2015-04-24 17:51:30 +02:00
|
|
|
clientWithRoute (Proxy :: Proxy sublayout)
|
|
|
|
(maybe req
|
|
|
|
(\value -> Servant.Common.Req.addHeader hname value req)
|
|
|
|
mval
|
|
|
|
)
|
|
|
|
baseurl
|
2015-09-28 16:34:53 +02:00
|
|
|
manager
|
2014-12-08 12:52:30 +01:00
|
|
|
|
|
|
|
where hname = symbolVal (Proxy :: Proxy sym)
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2016-01-16 19:34:44 +01:00
|
|
|
-- | Using a 'HttpVersion' combinator in your API doesn't affect the client
|
|
|
|
-- functions.
|
|
|
|
instance HasClient sublayout
|
|
|
|
=> HasClient (HttpVersion :> sublayout) where
|
|
|
|
|
|
|
|
type Client (HttpVersion :> sublayout) =
|
|
|
|
Client sublayout
|
|
|
|
|
|
|
|
clientWithRoute Proxy =
|
|
|
|
clientWithRoute (Proxy :: Proxy sublayout)
|
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
-- | If you use a 'QueryParam' in one of your endpoints in your API,
|
|
|
|
-- the corresponding querying function will automatically take
|
|
|
|
-- an additional argument of the type specified by your 'QueryParam',
|
|
|
|
-- enclosed in Maybe.
|
|
|
|
--
|
|
|
|
-- If you give Nothing, nothing will be added to the query string.
|
|
|
|
--
|
|
|
|
-- If you give a non-'Nothing' value, this function will take care
|
|
|
|
-- of inserting a textual representation of this value in the query string.
|
|
|
|
--
|
|
|
|
-- You can control how values for your type are turned into
|
2015-10-07 23:38:47 +02:00
|
|
|
-- text by specifying a 'ToHttpApiData' instance for your type.
|
2014-11-27 18:28:01 +01:00
|
|
|
--
|
|
|
|
-- Example:
|
|
|
|
--
|
2015-05-14 00:52:37 +02:00
|
|
|
-- > type MyApi = "books" :> QueryParam "author" Text :> Get '[JSON] [Book]
|
2014-11-27 18:28:01 +01:00
|
|
|
-- >
|
|
|
|
-- > myApi :: Proxy MyApi
|
|
|
|
-- > myApi = Proxy
|
|
|
|
-- >
|
2015-09-12 14:11:24 +02:00
|
|
|
-- > getBooksBy :: Maybe Text -> ExceptT String IO [Book]
|
2015-05-14 00:52:37 +02:00
|
|
|
-- > getBooksBy = client myApi host
|
|
|
|
-- > where host = BaseUrl Http "localhost" 8080
|
2014-11-27 18:28:01 +01:00
|
|
|
-- > -- then you can just use "getBooksBy" to query that endpoint.
|
|
|
|
-- > -- 'getBooksBy Nothing' for all books
|
|
|
|
-- > -- 'getBooksBy (Just "Isaac Asimov")' to get all books by Isaac Asimov
|
2015-10-07 23:38:47 +02:00
|
|
|
instance (KnownSymbol sym, ToHttpApiData a, HasClient sublayout)
|
2014-11-27 18:28:01 +01:00
|
|
|
=> HasClient (QueryParam sym a :> sublayout) where
|
|
|
|
|
2015-05-09 01:51:23 +02:00
|
|
|
type Client (QueryParam sym a :> sublayout) =
|
2015-05-03 01:45:17 +02:00
|
|
|
Maybe a -> Client sublayout
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- if mparam = Nothing, we don't add it to the query string
|
2015-09-28 16:34:53 +02:00
|
|
|
clientWithRoute Proxy req baseurl manager mparam =
|
2015-04-24 17:51:30 +02:00
|
|
|
clientWithRoute (Proxy :: Proxy sublayout)
|
|
|
|
(maybe req
|
|
|
|
(flip (appendToQueryString pname) req . Just)
|
|
|
|
mparamText
|
|
|
|
)
|
|
|
|
baseurl
|
2015-09-28 16:34:53 +02:00
|
|
|
manager
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
where pname = cs pname'
|
|
|
|
pname' = symbolVal (Proxy :: Proxy sym)
|
2015-10-07 23:38:47 +02:00
|
|
|
mparamText = fmap toQueryParam mparam
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- | If you use a 'QueryParams' in one of your endpoints in your API,
|
|
|
|
-- the corresponding querying function will automatically take
|
2015-03-26 14:34:38 +01:00
|
|
|
-- an additional argument, a list of values of the type specified
|
2014-11-27 18:28:01 +01:00
|
|
|
-- by your 'QueryParams'.
|
|
|
|
--
|
|
|
|
-- If you give an empty list, nothing will be added to the query string.
|
|
|
|
--
|
|
|
|
-- Otherwise, this function will take care
|
|
|
|
-- of inserting a textual representation of your values in the query string,
|
|
|
|
-- under the same query string parameter name.
|
|
|
|
--
|
|
|
|
-- You can control how values for your type are turned into
|
2015-10-07 23:38:47 +02:00
|
|
|
-- text by specifying a 'ToHttpApiData' instance for your type.
|
2014-11-27 18:28:01 +01:00
|
|
|
--
|
|
|
|
-- Example:
|
|
|
|
--
|
2015-05-14 00:52:37 +02:00
|
|
|
-- > type MyApi = "books" :> QueryParams "authors" Text :> Get '[JSON] [Book]
|
2014-11-27 18:28:01 +01:00
|
|
|
-- >
|
|
|
|
-- > myApi :: Proxy MyApi
|
|
|
|
-- > myApi = Proxy
|
|
|
|
-- >
|
2015-09-12 14:11:24 +02:00
|
|
|
-- > getBooksBy :: [Text] -> ExceptT String IO [Book]
|
2015-05-14 00:52:37 +02:00
|
|
|
-- > getBooksBy = client myApi host
|
|
|
|
-- > where host = BaseUrl Http "localhost" 8080
|
2014-11-27 18:28:01 +01:00
|
|
|
-- > -- then you can just use "getBooksBy" to query that endpoint.
|
|
|
|
-- > -- 'getBooksBy []' for all books
|
|
|
|
-- > -- 'getBooksBy ["Isaac Asimov", "Robert A. Heinlein"]'
|
|
|
|
-- > -- to get all books by Asimov and Heinlein
|
2015-10-07 23:38:47 +02:00
|
|
|
instance (KnownSymbol sym, ToHttpApiData a, HasClient sublayout)
|
2014-11-27 18:28:01 +01:00
|
|
|
=> HasClient (QueryParams sym a :> sublayout) where
|
|
|
|
|
2015-05-09 01:51:23 +02:00
|
|
|
type Client (QueryParams sym a :> sublayout) =
|
2015-05-03 01:45:17 +02:00
|
|
|
[a] -> Client sublayout
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2015-09-28 16:34:53 +02:00
|
|
|
clientWithRoute Proxy req baseurl manager paramlist =
|
2015-04-24 17:51:30 +02:00
|
|
|
clientWithRoute (Proxy :: Proxy sublayout)
|
|
|
|
(foldl' (\ req' -> maybe req' (flip (appendToQueryString pname) req' . Just))
|
|
|
|
req
|
|
|
|
paramlist'
|
|
|
|
)
|
2015-09-28 16:34:53 +02:00
|
|
|
baseurl manager
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
where pname = cs pname'
|
|
|
|
pname' = symbolVal (Proxy :: Proxy sym)
|
2015-10-07 23:38:47 +02:00
|
|
|
paramlist' = map (Just . toQueryParam) paramlist
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- | If you use a 'QueryFlag' in one of your endpoints in your API,
|
|
|
|
-- the corresponding querying function will automatically take
|
|
|
|
-- an additional 'Bool' argument.
|
|
|
|
--
|
|
|
|
-- If you give 'False', nothing will be added to the query string.
|
|
|
|
--
|
|
|
|
-- Otherwise, this function will insert a value-less query string
|
|
|
|
-- parameter under the name associated to your 'QueryFlag'.
|
|
|
|
--
|
|
|
|
-- Example:
|
|
|
|
--
|
2015-05-14 00:52:37 +02:00
|
|
|
-- > type MyApi = "books" :> QueryFlag "published" :> Get '[JSON] [Book]
|
2014-11-27 18:28:01 +01:00
|
|
|
-- >
|
|
|
|
-- > myApi :: Proxy MyApi
|
|
|
|
-- > myApi = Proxy
|
|
|
|
-- >
|
2015-09-12 14:11:24 +02:00
|
|
|
-- > getBooks :: Bool -> ExceptT String IO [Book]
|
2015-05-14 00:52:37 +02:00
|
|
|
-- > getBooks = client myApi host
|
|
|
|
-- > where host = BaseUrl Http "localhost" 8080
|
2014-11-27 18:28:01 +01:00
|
|
|
-- > -- then you can just use "getBooks" to query that endpoint.
|
|
|
|
-- > -- 'getBooksBy False' for all books
|
|
|
|
-- > -- 'getBooksBy True' to only get _already published_ books
|
|
|
|
instance (KnownSymbol sym, HasClient sublayout)
|
|
|
|
=> HasClient (QueryFlag sym :> sublayout) where
|
|
|
|
|
2015-05-09 01:51:23 +02:00
|
|
|
type Client (QueryFlag sym :> sublayout) =
|
2015-05-03 01:45:17 +02:00
|
|
|
Bool -> Client sublayout
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2015-09-28 16:34:53 +02:00
|
|
|
clientWithRoute Proxy req baseurl manager flag =
|
2015-04-24 17:51:30 +02:00
|
|
|
clientWithRoute (Proxy :: Proxy sublayout)
|
|
|
|
(if flag
|
|
|
|
then appendToQueryString paramname Nothing req
|
|
|
|
else req
|
|
|
|
)
|
2015-09-28 16:34:53 +02:00
|
|
|
baseurl manager
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
where paramname = cs $ symbolVal (Proxy :: Proxy sym)
|
|
|
|
|
2015-01-01 23:43:29 +01:00
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
-- | Pick a 'Method' and specify where the server you want to query is. You get
|
2015-03-16 01:12:11 +01:00
|
|
|
-- back the full `Response`.
|
2014-11-27 18:28:01 +01:00
|
|
|
instance HasClient Raw where
|
2015-09-12 14:11:24 +02:00
|
|
|
type Client Raw = H.Method -> ExceptT ServantError IO (Int, ByteString, MediaType, [HTTP.Header], Response ByteString)
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2015-09-28 16:34:53 +02:00
|
|
|
clientWithRoute :: Proxy Raw -> Req -> BaseUrl -> Manager -> Client Raw
|
|
|
|
clientWithRoute Proxy req baseurl manager httpMethod = do
|
2015-10-07 15:24:52 +02:00
|
|
|
performRequest httpMethod req baseurl manager
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- | If you use a 'ReqBody' in one of your endpoints in your API,
|
|
|
|
-- the corresponding querying function will automatically take
|
|
|
|
-- an additional argument of the type specified by your 'ReqBody'.
|
|
|
|
-- That function will take care of encoding this argument as JSON and
|
|
|
|
-- of using it as the request body.
|
|
|
|
--
|
|
|
|
-- All you need is for your type to have a 'ToJSON' instance.
|
|
|
|
--
|
|
|
|
-- Example:
|
|
|
|
--
|
2015-05-14 00:52:37 +02:00
|
|
|
-- > type MyApi = "books" :> ReqBody '[JSON] Book :> Post '[JSON] Book
|
2014-11-27 18:28:01 +01:00
|
|
|
-- >
|
|
|
|
-- > myApi :: Proxy MyApi
|
|
|
|
-- > myApi = Proxy
|
|
|
|
-- >
|
2015-09-12 14:11:24 +02:00
|
|
|
-- > addBook :: Book -> ExceptT String IO Book
|
2015-09-28 16:34:53 +02:00
|
|
|
-- > addBook = client myApi host manager
|
2015-05-14 00:52:37 +02:00
|
|
|
-- > where host = BaseUrl Http "localhost" 8080
|
2014-11-27 18:28:01 +01:00
|
|
|
-- > -- then you can just use "addBook" to query that endpoint
|
2015-02-24 23:30:31 +01:00
|
|
|
instance (MimeRender ct a, HasClient sublayout)
|
2015-02-17 07:17:10 +01:00
|
|
|
=> HasClient (ReqBody (ct ': cts) a :> sublayout) where
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2015-05-09 01:51:23 +02:00
|
|
|
type Client (ReqBody (ct ': cts) a :> sublayout) =
|
2015-05-03 01:45:17 +02:00
|
|
|
a -> Client sublayout
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2015-09-28 16:34:53 +02:00
|
|
|
clientWithRoute Proxy req baseurl manager body =
|
2015-04-24 17:51:30 +02:00
|
|
|
clientWithRoute (Proxy :: Proxy sublayout)
|
|
|
|
(let ctProxy = Proxy :: Proxy ct
|
|
|
|
in setRQBody (mimeRender ctProxy body)
|
|
|
|
(contentType ctProxy)
|
|
|
|
req
|
|
|
|
)
|
2015-09-28 16:34:53 +02:00
|
|
|
baseurl manager
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- | Make the querying function append @path@ to the request path.
|
|
|
|
instance (KnownSymbol path, HasClient sublayout) => HasClient (path :> sublayout) where
|
2015-05-09 01:51:23 +02:00
|
|
|
type Client (path :> sublayout) = Client sublayout
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2015-09-28 16:34:53 +02:00
|
|
|
clientWithRoute Proxy req baseurl manager =
|
2015-04-24 17:51:30 +02:00
|
|
|
clientWithRoute (Proxy :: Proxy sublayout)
|
|
|
|
(appendToPath p req)
|
2015-09-28 16:34:53 +02:00
|
|
|
baseurl manager
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
where p = symbolVal (Proxy :: Proxy path)
|
|
|
|
|
2015-06-23 10:34:20 +02:00
|
|
|
instance HasClient api => HasClient (Vault :> api) where
|
|
|
|
type Client (Vault :> api) = Client api
|
|
|
|
|
2015-09-28 16:34:53 +02:00
|
|
|
clientWithRoute Proxy req baseurl manager =
|
|
|
|
clientWithRoute (Proxy :: Proxy api) req baseurl manager
|
2015-06-23 10:34:20 +02:00
|
|
|
|
|
|
|
instance HasClient api => HasClient (RemoteHost :> api) where
|
|
|
|
type Client (RemoteHost :> api) = Client api
|
|
|
|
|
2015-09-28 16:34:53 +02:00
|
|
|
clientWithRoute Proxy req baseurl manager =
|
|
|
|
clientWithRoute (Proxy :: Proxy api) req baseurl manager
|
2015-06-23 10:34:20 +02:00
|
|
|
|
|
|
|
instance HasClient api => HasClient (IsSecure :> api) where
|
|
|
|
type Client (IsSecure :> api) = Client api
|
|
|
|
|
2015-09-28 16:34:53 +02:00
|
|
|
clientWithRoute Proxy req baseurl manager =
|
|
|
|
clientWithRoute (Proxy :: Proxy api) req baseurl manager
|
2015-11-27 02:05:34 +01:00
|
|
|
|
2016-01-18 21:27:19 +01:00
|
|
|
instance HasClient subapi =>
|
2016-02-28 23:23:32 +01:00
|
|
|
HasClient (WithNamedContext name context subapi) where
|
2016-01-18 21:27:19 +01:00
|
|
|
|
2016-02-28 23:23:32 +01:00
|
|
|
type Client (WithNamedContext name context subapi) = Client subapi
|
2016-01-18 21:27:19 +01:00
|
|
|
clientWithRoute Proxy = clientWithRoute (Proxy :: Proxy subapi)
|
|
|
|
|
2016-02-17 21:45:08 +01:00
|
|
|
instance ( HasClient api
|
|
|
|
) => HasClient (AuthProtect tag :> api) where
|
|
|
|
type Client (AuthProtect tag :> api)
|
|
|
|
= AuthenticateReq (AuthProtect tag) -> Client api
|
|
|
|
|
|
|
|
clientWithRoute Proxy req baseurl manager (AuthenticateReq (val,func)) =
|
|
|
|
clientWithRoute (Proxy :: Proxy api) (func val req) baseurl manager
|
2015-11-27 02:05:34 +01:00
|
|
|
|
2016-02-17 20:12:47 +01:00
|
|
|
-- * Basic Authentication
|
|
|
|
|
|
|
|
instance HasClient api => HasClient (BasicAuth realm usr :> api) where
|
|
|
|
type Client (BasicAuth realm usr :> api) = BasicAuthData -> Client api
|
|
|
|
|
|
|
|
clientWithRoute Proxy req baseurl manager val =
|
|
|
|
clientWithRoute (Proxy :: Proxy api) (basicAuthReq val req) baseurl manager
|
|
|
|
|
|
|
|
|
2015-11-27 02:05:34 +01:00
|
|
|
{- Note [Non-Empty Content Types]
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Rather than have
|
|
|
|
|
|
|
|
instance (..., cts' ~ (ct ': cts)) => ... cts' ...
|
|
|
|
|
|
|
|
It may seem to make more sense to have:
|
|
|
|
|
|
|
|
instance (...) => ... (ct ': cts) ...
|
|
|
|
|
|
|
|
But this means that if another instance exists that does *not* require
|
|
|
|
non-empty lists, but is otherwise more specific, no instance will be overall
|
2016-01-08 17:43:10 +01:00
|
|
|
more specific. This in turn generally means adding yet another instance (one
|
2015-11-27 02:05:34 +01:00
|
|
|
for empty and one for non-empty lists).
|
|
|
|
-}
|