Skip to content

Export encoding function for a query parameter value #1549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions servant-client-core/src/Servant/Client/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ module Servant.Client.Core
, appendToPath
, setRequestBodyLBS
, setRequestBody
, encodeQueryParamValue
) where
import Servant.Client.Core.Auth
import Servant.Client.Core.BaseUrl
Expand Down
11 changes: 3 additions & 8 deletions servant-client-core/src/Servant/Client/Core/HasClient.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ import Control.Arrow
import Control.Monad
(unless)
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
(partitionEithers)
import Data.Constraint (Dict(..))
Expand Down Expand Up @@ -571,17 +569,14 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient m api, SBoolI (FoldRequire
(Proxy :: Proxy mods) add (maybe req add) mparam
where
add :: a -> Request
add param = appendToQueryString pname (Just $ encodeQueryParam param) req
add param = appendToQueryString pname (Just $ encodeQueryParamValue param) req

pname :: Text
pname = pack $ symbolVal (Proxy :: Proxy sym)

hoistClientMonad pm _ 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,
-- the corresponding querying function will automatically take
-- an additional argument, a list of values of the type specified
Expand Down Expand Up @@ -623,7 +618,7 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient m api)
)

where pname = pack $ symbolVal (Proxy :: Proxy sym)
paramlist' = map (Just . encodeQueryParam) paramlist
paramlist' = map (Just . encodeQueryParamValue) paramlist

hoistClientMonad pm _ f cl = \as ->
hoistClientMonad pm (Proxy :: Proxy api) f (cl as)
Expand Down
16 changes: 14 additions & 2 deletions servant-client-core/src/Servant/Client/Core/Request.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Servant.Client.Core.Request (
addHeader,
appendToPath,
appendToQueryString,
encodeQueryParamValue,
setRequestBody,
setRequestBodyLBS,
) where
Expand Down Expand Up @@ -142,18 +143,29 @@ defaultRequest = Request
, requestMethod = methodGet
}

-- | Append extra path to the request being constructed.
--
appendToPath :: Text -> Request -> Request
appendToPath p req
= req { requestPath = requestPath req <> "/" <> toEncodedUrlPiece p }

appendToQueryString :: Text -- ^ param name
-> Maybe BS.ByteString -- ^ param value
-- | Append a query parameter to the request being constructed.
--
appendToQueryString :: Text -- ^ query param name
-> Maybe BS.ByteString -- ^ query param value
-> Request
-> Request
appendToQueryString pname pvalue req
= req { requestQueryString = requestQueryString req
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 name val req
= req { requestHeaders = requestHeaders req Seq.|> (name, toHeader val)}
Expand Down