on master help sign in
Export encoding function for a query parameter value (#1549)
d05da71Giorgio Marinelli
servant-client-core/src/Servant/Client/Core.hs+1
@@ -59,6 +59,7 @@ module Servant.Client.Core
59 , appendToPath
59 , appendToPath
60 , setRequestBodyLBS
60 , setRequestBodyLBS
61 , setRequestBody
61 , setRequestBody
62+ , encodeQueryParamValue
62 ) where
63 ) where
63import Servant.Client.Core.Auth
64import Servant.Client.Core.Auth
64import Servant.Client.Core.BaseUrl
65import Servant.Client.Core.BaseUrl
servant-client-core/src/Servant/Client/Core/HasClient.hs+3−8
@@ -33,9 +33,7 @@ import Control.Arrow
33import Control.Monad
33import Control.Monad
34 (unless)
34 (unless)
35import qualified Data.ByteString as BS
35import qualified Data.ByteString as BS
36−import Data.ByteString.Builder
37− (toLazyByteString)
38−import qualified Data.ByteString.Lazy as BL
36+import qualified Data.ByteString.Lazy as BL
39import Data.Either
37import Data.Either
40 (partitionEithers)
38 (partitionEithers)
41import Data.Constraint (Dict(..))
39import Data.Constraint (Dict(..))
@@ -571,7 +569,7 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient m api, SBoolI (FoldRequire
571 (Proxy :: Proxy mods) add (maybe req add) mparam
569 (Proxy :: Proxy mods) add (maybe req add) mparam
572 where
570 where
573 add :: a -> Request
571 add :: a -> Request
574− add param = appendToQueryString pname (Just $ encodeQueryParam param) req
572+ add param = appendToQueryString pname (Just $ encodeQueryParamValue param) req
575
573
576 pname :: Text
574 pname :: Text
577 pname = pack $ symbolVal (Proxy :: Proxy sym)
575 pname = pack $ symbolVal (Proxy :: Proxy sym)
@@ -579,9 +577,6 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient m api, SBoolI (FoldRequire
579 hoistClientMonad pm _ f cl = \arg ->
577 hoistClientMonad pm _ f cl = \arg ->
580 hoistClientMonad pm (Proxy :: Proxy api) f (cl arg)
578 hoistClientMonad pm (Proxy :: Proxy api) f (cl arg)
581
579
582−encodeQueryParam :: ToHttpApiData a => a -> BS.ByteString
583−encodeQueryParam = BL.toStrict . toLazyByteString . toEncodedUrlPiece
584−
585-- | If you use a 'QueryParams' in one of your endpoints in your API,
580-- | If you use a 'QueryParams' in one of your endpoints in your API,
586-- the corresponding querying function will automatically take
581-- the corresponding querying function will automatically take
587-- an additional argument, a list of values of the type specified
582-- an additional argument, a list of values of the type specified
@@ -623,7 +618,7 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient m api)
623 )
618 )
624
619
625 where pname = pack $ symbolVal (Proxy :: Proxy sym)
620 where pname = pack $ symbolVal (Proxy :: Proxy sym)
626− paramlist' = map (Just . encodeQueryParam) paramlist
621+ paramlist' = map (Just . encodeQueryParamValue) paramlist
627
622
628 hoistClientMonad pm _ f cl = \as ->
623 hoistClientMonad pm _ f cl = \as ->
629 hoistClientMonad pm (Proxy :: Proxy api) f (cl as)
624 hoistClientMonad pm (Proxy :: Proxy api) f (cl as)
servant-client-core/src/Servant/Client/Core/Request.hs+14−2
@@ -17,6 +17,7 @@ module Servant.Client.Core.Request (
17 addHeader,
17 addHeader,
18 appendToPath,
18 appendToPath,
19 appendToQueryString,
19 appendToQueryString,
20+ encodeQueryParamValue,
20 setRequestBody,
21 setRequestBody,
21 setRequestBodyLBS,
22 setRequestBodyLBS,
22 ) where
23 ) where
@@ -142,18 +143,29 @@ defaultRequest = Request
142 , requestMethod = methodGet
143 , requestMethod = methodGet
143 }
144 }
144
145
146+-- | Append extra path to the request being constructed.
147+--
145appendToPath :: Text -> Request -> Request
148appendToPath :: Text -> Request -> Request
146appendToPath p req
149appendToPath p req
147 = req { requestPath = requestPath req <> "/" <> toEncodedUrlPiece p }
150 = req { requestPath = requestPath req <> "/" <> toEncodedUrlPiece p }
148
151
152+-- | Append a query parameter to the request being constructed.
153+--
149−appendToQueryString :: Text -- ^ param name
154+appendToQueryString :: Text -- ^ query param name
150− -> Maybe BS.ByteString -- ^ param value
155+ -> Maybe BS.ByteString -- ^ query param value
151 -> Request
156 -> Request
152 -> Request
157 -> Request
153appendToQueryString pname pvalue req
158appendToQueryString pname pvalue req
154 = req { requestQueryString = requestQueryString req
159 = req { requestQueryString = requestQueryString req
155 Seq.|> (encodeUtf8 pname, pvalue)}
160 Seq.|> (encodeUtf8 pname, pvalue)}
156
161
162+-- | Encode a query parameter value.
163+--
164+encodeQueryParamValue :: ToHttpApiData a => a -> BS.ByteString
165+encodeQueryParamValue = LBS.toStrict . Builder.toLazyByteString . toEncodedUrlPiece
166+
167+-- | Add header to the request being constructed.
168+--
157addHeader :: ToHttpApiData a => HeaderName -> a -> Request -> Request
169addHeader :: ToHttpApiData a => HeaderName -> a -> Request -> Request
158addHeader name val req
170addHeader name val req
159 = req { requestHeaders = requestHeaders req Seq.|> (name, toHeader val)}
171 = req { requestHeaders = requestHeaders req Seq.|> (name, toHeader val)}