on master help sign in
Take out text response interpretations
c094362mrkkrp
CHANGELOG.md+1−4
@@ -1,10 +1,7 @@
1## Req 0.5.0
1## Req 0.5.0
2
2
3* Changed the signature of the `makeResponseBodyPreview` from `response ->
3* Changed the signature of the `makeResponseBodyPreview` from `response ->
4− IO ByteString` to `ByteString`.
4+ IO ByteString` to `response -> ByteString`.
5−
6−* Added `TextResponse`, `LTextResponse`, `textResponse`, and
7− `ltextResponse`.
8
5
9* Minor documentation improvements.
6* Minor documentation improvements.
10
7
Network/HTTP/Req.hs−79
@@ -197,10 +197,6 @@ module Network.HTTP.Req
197 , bsResponse
197 , bsResponse
198 , LbsResponse
198 , LbsResponse
199 , lbsResponse
199 , lbsResponse
200− , TextResponse
201− , textResponse
202− , LTextResponse
203− , ltextResponse
204 -- ** Inspecting a response
200 -- ** Inspecting a response
205 , responseBody
201 , responseBody
206 , responseStatusCode
202 , responseStatusCode
@@ -250,9 +246,6 @@ import qualified Data.CaseInsensitive as CI
250import qualified Data.List.NonEmpty as NE
246import qualified Data.List.NonEmpty as NE
251import qualified Data.Text as T
247import qualified Data.Text as T
252import qualified Data.Text.Encoding as T
248import qualified Data.Text.Encoding as T
253−import qualified Data.Text.Encoding.Error as TE
254−import qualified Data.Text.Lazy as TL
255−import qualified Data.Text.Lazy.Encoding as TL
256import qualified Data.Text.Read as TR
249import qualified Data.Text.Read as TR
257import qualified Network.Connection as NC
250import qualified Network.Connection as NC
258import qualified Network.HTTP.Client as L
251import qualified Network.HTTP.Client as L
@@ -299,8 +292,6 @@ import GHC.Exts (Constraint)
299-- * 'jsonResponse'
292-- * 'jsonResponse'
300-- * 'bsResponse' (to get a strict 'ByteString')
293-- * 'bsResponse' (to get a strict 'ByteString')
301-- * 'lbsResponse' (to get a lazy 'BL.ByteString')
294-- * 'lbsResponse' (to get a lazy 'BL.ByteString')
302−-- * 'textResponse' (to get a strict 'Text', UTF-8 decoded)
303−-- * 'ltextResponse' (to get a lazy 'TL.Text', UTF-8 decoded)
304--
295--
305-- Finally, @options@ is a 'Monoid' that holds a composite 'Option' for all
296-- Finally, @options@ is a 'Monoid' that holds a composite 'Option' for all
306-- other optional settings like query parameters, headers, non-standard port
297-- other optional settings like query parameters, headers, non-standard port
@@ -1430,70 +1421,6 @@ instance HttpResponse LbsResponse where
1430lbsResponse :: Proxy LbsResponse
1421lbsResponse :: Proxy LbsResponse
1431lbsResponse = Proxy
1422lbsResponse = Proxy
1432
1423
1433−-- | Make a request and interpret the body of the response as strict 'Text'.
1434−-- The 'handleHttpException' method of 'MonadHttp' instance corresponding to
1435−-- monad in which you use 'req' will determine what to do in the case when
1436−-- decoding fails (the 'TextEncHttpException' constructor will be used).
1437−--
1438−-- @since 0.5.0
1439−
1440−newtype TextResponse = TextResponse (L.Response Text)
1441−
1442−instance HttpResponse TextResponse where
1443− type HttpResponseBody TextResponse = Text
1444− toVanillaResponse (TextResponse r) = r
1445− getHttpResponse request manager = do
1446− r <- httpBs request manager
1447− let bs = L.responseBody r
1448− case T.decodeUtf8' bs of
1449− Left uexc ->
1450− (throwIO . TextEncHttpException uexc . B.take bodyPreviewLength) bs
1451− Right txt -> return (TextResponse r { L.responseBody = txt })
1452− makeResponseBodyPreview =
1453− B.take bodyPreviewLength . T.encodeUtf8 . responseBody
1454−
1455−-- | Use this as the fourth argument of 'req' to specify that you want to
1456−-- interpret the response body as 'T.Text'.
1457−--
1458−-- @since 0.5.0
1459−
1460−textResponse :: Proxy TextResponse
1461−textResponse = Proxy
1462−
1463−-- | Make a request and interpret the body of the response as lazy
1464−-- 'TL.Text'. The 'handleHttpException' method of 'MonadHttp' instance
1465−-- corresponding to monad in which you use 'req' will determine what to do
1466−-- in the case when decoding fails (the 'TextEncHttpException' constructor
1467−-- will be used).
1468−--
1469−-- @since 0.5.0
1470−
1471−newtype LTextResponse = LTextResponse (L.Response TL.Text)
1472−
1473−instance HttpResponse LTextResponse where
1474− type HttpResponseBody LTextResponse = TL.Text
1475− toVanillaResponse (LTextResponse r) = r
1476− getHttpResponse request manager = do
1477− r <- L.httpLbs request manager
1478− case TL.decodeUtf8' (L.responseBody r) of
1479− Left uexc -> throwIO
1480− . TextEncHttpException uexc
1481− . BL.toStrict
1482− . BL.take bodyPreviewLength
1483− . L.responseBody
1484− $ r
1485− Right txt -> return (LTextResponse r { L.responseBody = txt })
1486− makeResponseBodyPreview =
1487− BL.toStrict . BL.take bodyPreviewLength . TL.encodeUtf8 . responseBody
1488−
1489−-- | Use this as the fourth argument of 'req' to specify that you want to
1490−-- interpret the response body as a lazy 'TL.Text'.
1491−--
1492−-- @since 0.5.0
1493−
1494−ltextResponse :: Proxy LTextResponse
1495−ltextResponse = Proxy
1496−
1497-- | Perform a 'L.Request' using given 'L.Manager' and return the response
1424-- | Perform a 'L.Request' using given 'L.Manager' and return the response
1498-- as a strict 'ByteString'.
1425-- as a strict 'ByteString'.
1499
1426
@@ -1624,12 +1551,6 @@ data HttpException
1624 | JsonHttpException String
1551 | JsonHttpException String
1625 -- ^ A wrapper with Aeson-produced 'String' describing why decoding
1552 -- ^ A wrapper with Aeson-produced 'String' describing why decoding
1626 -- failed
1553 -- failed
1627− | TextEncHttpException TE.UnicodeException ByteString
1628− -- ^ This value indicates that a response that was expected to be in the
1629− -- UTF-8 encoding could not be decoded; the 'ByteString' is the
1630− -- beginning of response in raw binary form
1631− --
1632− -- @since 0.5.0
1633 deriving (Show, Typeable, Generic)
1554 deriving (Show, Typeable, Generic)
1634
1555
1635instance Exception HttpException
1556instance Exception HttpException
httpbin-tests/Network/HTTP/ReqSpec.hs+8−26
@@ -29,7 +29,6 @@ import qualified Data.HashMap.Strict as HM
29import qualified Data.Text as T
29import qualified Data.Text as T
30import qualified Data.Text.Encoding as T
30import qualified Data.Text.Encoding as T
31import qualified Data.Text.IO as TIO
31import qualified Data.Text.IO as TIO
32−import qualified Data.Text.Lazy.IO as TLIO
33import qualified Network.HTTP.Client as L
32import qualified Network.HTTP.Client as L
34import qualified Network.HTTP.Client.MultipartFormData as LM
33import qualified Network.HTTP.Client.MultipartFormData as LM
35import qualified Network.HTTP.Types as Y
34import qualified Network.HTTP.Types as Y
@@ -198,31 +197,14 @@ spec = do
198
197
199 -- TODO /delete
198 -- TODO /delete
200
199
201− describe "receiving UTF-8 encoded Unicode data" $ do
200+ describe "receiving UTF-8 encoded Unicode data" $
202− context "as strict byte string" $
203− it "works" $ do
201+ it "works" $ do
204− r <- req GET (httpbin /: "encoding" /: "utf8")
202+ r <- req GET (httpbin /: "encoding" /: "utf8")
205− NoReqBody bsResponse mempty
203+ NoReqBody bsResponse mempty
206− utf8data <- B.readFile "httpbin-data/utf8.html"
204+ utf8data <- B.readFile "httpbin-data/utf8.html"
207− responseBody r `shouldBe` utf8data
205+ responseBody r `shouldBe` utf8data
208− responseStatusCode r `shouldBe` 200
209− responseStatusMessage r `shouldBe` "OK"
210− context "as strict text" $ do
211− it "works" $ do
212− r <- req GET (httpbin /: "encoding" /: "utf8")
213− NoReqBody textResponse mempty
214− utf8data <- TIO.readFile "httpbin-data/utf8.html"
215− responseBody r `shouldBe` utf8data
216− responseStatusCode r `shouldBe` 200
206+ responseStatusCode r `shouldBe` 200
217− responseStatusMessage r `shouldBe` "OK"
218− context "as lazy text" $
219− it "works" $ do
220− r <- req GET (httpbin /: "encoding" /: "utf8")
221− NoReqBody ltextResponse mempty
222− utf8data <- TLIO.readFile "httpbin-data/utf8.html"
223− responseBody r `shouldBe` utf8data
224− responseStatusCode r `shouldBe` 200
225− responseStatusMessage r `shouldBe` "OK"
207+ responseStatusMessage r `shouldBe` "OK"
226
208
227 -- TODO /gzip
209 -- TODO /gzip
228 -- TODO /deflate
210 -- TODO /deflate