Export encoding function for a query parameter value (#1549)
This commit is contained in:
parent
cedab6572d
commit
d05da71f09
3 changed files with 18 additions and 10 deletions
|
@ -59,6 +59,7 @@ module Servant.Client.Core
|
||||||
, appendToPath
|
, appendToPath
|
||||||
, setRequestBodyLBS
|
, setRequestBodyLBS
|
||||||
, setRequestBody
|
, setRequestBody
|
||||||
|
, encodeQueryParamValue
|
||||||
) where
|
) where
|
||||||
import Servant.Client.Core.Auth
|
import Servant.Client.Core.Auth
|
||||||
import Servant.Client.Core.BaseUrl
|
import Servant.Client.Core.BaseUrl
|
||||||
|
|
|
@ -33,8 +33,6 @@ import Control.Arrow
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
(unless)
|
(unless)
|
||||||
import qualified Data.ByteString as BS
|
import qualified Data.ByteString as BS
|
||||||
import Data.ByteString.Builder
|
|
||||||
(toLazyByteString)
|
|
||||||
import qualified Data.ByteString.Lazy as BL
|
import qualified Data.ByteString.Lazy as BL
|
||||||
import Data.Either
|
import Data.Either
|
||||||
(partitionEithers)
|
(partitionEithers)
|
||||||
|
@ -571,7 +569,7 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient m api, SBoolI (FoldRequire
|
||||||
(Proxy :: Proxy mods) add (maybe req add) mparam
|
(Proxy :: Proxy mods) add (maybe req add) mparam
|
||||||
where
|
where
|
||||||
add :: a -> Request
|
add :: a -> Request
|
||||||
add param = appendToQueryString pname (Just $ encodeQueryParam param) req
|
add param = appendToQueryString pname (Just $ encodeQueryParamValue param) req
|
||||||
|
|
||||||
pname :: Text
|
pname :: Text
|
||||||
pname = pack $ symbolVal (Proxy :: Proxy sym)
|
pname = pack $ symbolVal (Proxy :: Proxy sym)
|
||||||
|
@ -579,9 +577,6 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient m api, SBoolI (FoldRequire
|
||||||
hoistClientMonad pm _ f cl = \arg ->
|
hoistClientMonad pm _ f cl = \arg ->
|
||||||
hoistClientMonad pm (Proxy :: Proxy api) f (cl arg)
|
hoistClientMonad pm (Proxy :: Proxy api) f (cl arg)
|
||||||
|
|
||||||
encodeQueryParam :: ToHttpApiData a => a -> BS.ByteString
|
|
||||||
encodeQueryParam = BL.toStrict . toLazyByteString . toEncodedUrlPiece
|
|
||||||
|
|
||||||
-- | If you use a 'QueryParams' in one of your endpoints in your API,
|
-- | If you use a 'QueryParams' in one of your endpoints in your API,
|
||||||
-- the corresponding querying function will automatically take
|
-- the corresponding querying function will automatically take
|
||||||
-- an additional argument, a list of values of the type specified
|
-- an additional argument, a list of values of the type specified
|
||||||
|
@ -623,7 +618,7 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient m api)
|
||||||
)
|
)
|
||||||
|
|
||||||
where pname = pack $ symbolVal (Proxy :: Proxy sym)
|
where pname = pack $ symbolVal (Proxy :: Proxy sym)
|
||||||
paramlist' = map (Just . encodeQueryParam) paramlist
|
paramlist' = map (Just . encodeQueryParamValue) paramlist
|
||||||
|
|
||||||
hoistClientMonad pm _ f cl = \as ->
|
hoistClientMonad pm _ f cl = \as ->
|
||||||
hoistClientMonad pm (Proxy :: Proxy api) f (cl as)
|
hoistClientMonad pm (Proxy :: Proxy api) f (cl as)
|
||||||
|
|
|
@ -17,6 +17,7 @@ module Servant.Client.Core.Request (
|
||||||
addHeader,
|
addHeader,
|
||||||
appendToPath,
|
appendToPath,
|
||||||
appendToQueryString,
|
appendToQueryString,
|
||||||
|
encodeQueryParamValue,
|
||||||
setRequestBody,
|
setRequestBody,
|
||||||
setRequestBodyLBS,
|
setRequestBodyLBS,
|
||||||
) where
|
) where
|
||||||
|
@ -142,18 +143,29 @@ defaultRequest = Request
|
||||||
, requestMethod = methodGet
|
, requestMethod = methodGet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-- | Append extra path to the request being constructed.
|
||||||
|
--
|
||||||
appendToPath :: Text -> Request -> Request
|
appendToPath :: Text -> Request -> Request
|
||||||
appendToPath p req
|
appendToPath p req
|
||||||
= req { requestPath = requestPath req <> "/" <> toEncodedUrlPiece p }
|
= req { requestPath = requestPath req <> "/" <> toEncodedUrlPiece p }
|
||||||
|
|
||||||
appendToQueryString :: Text -- ^ param name
|
-- | Append a query parameter to the request being constructed.
|
||||||
-> Maybe BS.ByteString -- ^ param value
|
--
|
||||||
|
appendToQueryString :: Text -- ^ query param name
|
||||||
|
-> Maybe BS.ByteString -- ^ query param value
|
||||||
-> Request
|
-> Request
|
||||||
-> Request
|
-> Request
|
||||||
appendToQueryString pname pvalue req
|
appendToQueryString pname pvalue req
|
||||||
= req { requestQueryString = requestQueryString req
|
= req { requestQueryString = requestQueryString req
|
||||||
Seq.|> (encodeUtf8 pname, pvalue)}
|
Seq.|> (encodeUtf8 pname, pvalue)}
|
||||||
|
|
||||||
|
-- | Encode a query parameter value.
|
||||||
|
--
|
||||||
|
encodeQueryParamValue :: ToHttpApiData a => a -> BS.ByteString
|
||||||
|
encodeQueryParamValue = LBS.toStrict . Builder.toLazyByteString . toEncodedUrlPiece
|
||||||
|
|
||||||
|
-- | Add header to the request being constructed.
|
||||||
|
--
|
||||||
addHeader :: ToHttpApiData a => HeaderName -> a -> Request -> Request
|
addHeader :: ToHttpApiData a => HeaderName -> a -> Request -> Request
|
||||||
addHeader name val req
|
addHeader name val req
|
||||||
= req { requestHeaders = requestHeaders req Seq.|> (name, toHeader val)}
|
= req { requestHeaders = requestHeaders req Seq.|> (name, toHeader val)}
|
||||||
|
|
Loading…
Reference in a new issue