Skip to content

Commit

Permalink
Export encoding function for a query parameter value (#1549)
Browse files Browse the repository at this point in the history
  • Loading branch information
marinelli committed Mar 1, 2022
1 parent cedab65 commit d05da71
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
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

0 comments on commit d05da71

Please sign in to comment.