on master help sign in
Add the ‘renderUrl’ function
6105d7fSridhar Ratnakumar
CHANGELOG.md+2
@@ -6,6 +6,8 @@
6* Added the `httpConfigRetryJudgeException` field to `HttpConfig` so that
6* Added the `httpConfigRetryJudgeException` field to `HttpConfig` so that
7 requests that result in exceptions can be retried.
7 requests that result in exceptions can be retried.
8
8
9+* Added the function `renderUrl`.
10+
9## Req 3.3.0
11## Req 3.3.0
10
12
11* Derived `Show` instances for response types `IgnoreResponse`,
13* Derived `Show` instances for response types `IgnoreResponse`,
Network/HTTP/Req.hs+14
@@ -149,6 +149,7 @@ module Network.HTTP.Req
149 useHttpsURI,
149 useHttpsURI,
150 useURI,
150 useURI,
151 urlQ,
151 urlQ,
152+ renderUrl,
152
153
153 -- ** Body
154 -- ** Body
154 -- $body
155 -- $body
@@ -929,6 +930,19 @@ infixl 5 /:
929(/:) :: Url scheme -> Text -> Url scheme
930(/:) :: Url scheme -> Text -> Url scheme
930(/:) = (/~)
931(/:) = (/~)
931
932
933+-- | Render a 'Url' as 'Text'.
934+--
935+-- @since 3.4.0
936+renderUrl :: Url scheme -> Text
937+renderUrl = \case
938+ Url Https parts ->
939+ "https://" <> renderParts parts
940+ Url Http parts ->
941+ "http://" <> renderParts parts
942+ where
943+ renderParts parts =
944+ T.intercalate "/" (reverse $ NE.toList parts)
945+
932-- | The 'useHttpURI' function provides an alternative method to get 'Url'
946-- | The 'useHttpURI' function provides an alternative method to get 'Url'
933-- (possibly with some 'Option's) from a 'URI'. This is useful when you are
947-- (possibly with some 'Option's) from a 'URI'. This is useful when you are
934-- given a URL to query dynamically and don't know it beforehand.
948-- given a URL to query dynamically and don't know it beforehand.
pure-tests/Network/HTTP/ReqSpec.hs+20
@@ -182,6 +182,26 @@ spec = do
182 lookup "Authorization" (L.requestHeaders requestHttps)
182 lookup "Authorization" (L.requestHeaders requestHttps)
183 `shouldBe` uriBasicAuth uriHttps
183 `shouldBe` uriBasicAuth uriHttps
184
184
185+ describe "renderUrl" $ do
186+ context "http" $ do
187+ context "empty path" $
188+ it "renders correctly" $ do
189+ let (uriHttp, _) = [urlQ|http://httpbin.org|]
190+ renderUrl uriHttp `shouldBe` "http://httpbin.org"
191+ context "non-empty path" $
192+ it "renders correctly" $ do
193+ let (uriHttp, _) = [urlQ|http://httpbin.org/here/we/go|]
194+ renderUrl uriHttp `shouldBe` "http://httpbin.org/here/we/go"
195+ context "http" $ do
196+ context "empty path" $
197+ it "renders correctly" $ do
198+ let (uriHttp, _) = [urlQ|https://httpbin.org|]
199+ renderUrl uriHttp `shouldBe` "https://httpbin.org"
200+ context "non-empty path" $
201+ it "renders correctly" $ do
202+ let (uriHttp, _) = [urlQ|https://httpbin.org/here/we/go|]
203+ renderUrl uriHttp `shouldBe` "https://httpbin.org/here/we/go"
204+
185 describe "bodies" $ do
205 describe "bodies" $ do
186 describe "NoReqBody" $
206 describe "NoReqBody" $
187 it "sets body to empty byte string" $ do
207 it "sets body to empty byte string" $ do