on master help sign in
Take out text response interpretations
commitc0943628d48935c242c8cf54e224ca03f98302ec authormrkkrp <markkarpov92@gmail.com> dateSat 11 Nov 2017, 15:42 (9 years ago) parente33ea25
3 files changed+9−109
CHANGELOG.md+1−4 Network/HTTP/Req.hs−79 httpbin-tests/Network/HTTP/ReqSpec.hs+8−26
CHANGELOG.md+1−4
@@ -1,10 +1,7 @@
11## Req 0.5.0
22
33* Changed the signature of the `makeResponseBodyPreview` from `response ->
4− IO ByteString` to `ByteString`.
5−
6−* Added `TextResponse`, `LTextResponse`, `textResponse`, and
7− `ltextResponse`.
4+ IO ByteString` to `response -> ByteString`.
85
96* Minor documentation improvements.
107
Network/HTTP/Req.hs−79
@@ -197,10 +197,6 @@ module Network.HTTP.Req
197197 , bsResponse
198198 , LbsResponse
199199 , lbsResponse
200− , TextResponse
201− , textResponse
202− , LTextResponse
203− , ltextResponse
204200 -- ** Inspecting a response
205201 , responseBody
206202 , responseStatusCode
@@ -250,9 +246,6 @@ import qualified Data.CaseInsensitive as CI
250246import qualified Data.List.NonEmpty as NE
251247import qualified Data.Text as T
252248import 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
256249import qualified Data.Text.Read as TR
257250import qualified Network.Connection as NC
258251import qualified Network.HTTP.Client as L
@@ -299,8 +292,6 @@ import GHC.Exts (Constraint)
299292-- * 'jsonResponse'
300293-- * 'bsResponse' (to get a strict 'ByteString')
301294-- * '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)
304295--
305296-- Finally, @options@ is a 'Monoid' that holds a composite 'Option' for all
306297-- other optional settings like query parameters, headers, non-standard port
@@ -1430,70 +1421,6 @@ instance HttpResponse LbsResponse where
14301421lbsResponse :: Proxy LbsResponse
14311422lbsResponse = Proxy
14321423
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−
14971424-- | Perform a 'L.Request' using given 'L.Manager' and return the response
14981425-- as a strict 'ByteString'.
14991426
@@ -1624,12 +1551,6 @@ data HttpException
16241551 | JsonHttpException String
16251552 -- ^ A wrapper with Aeson-produced 'String' describing why decoding
16261553 -- 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
16331554 deriving (Show, Typeable, Generic)
16341555
16351556instance Exception HttpException
httpbin-tests/Network/HTTP/ReqSpec.hs+8−26
@@ -29,7 +29,6 @@ import qualified Data.HashMap.Strict as HM
2929import qualified Data.Text as T
3030import qualified Data.Text.Encoding as T
3131import qualified Data.Text.IO as TIO
32−import qualified Data.Text.Lazy.IO as TLIO
3332import qualified Network.HTTP.Client as L
3433import qualified Network.HTTP.Client.MultipartFormData as LM
3534import qualified Network.HTTP.Types as Y
@@ -198,31 +197,14 @@ spec = do
198197
199198 -- TODO /delete
200199
201− describe "receiving UTF-8 encoded Unicode data" $ do
202− context "as strict byte string" $
203− it "works" $ do
204− r <- req GET (httpbin /: "encoding" /: "utf8")
205− NoReqBody bsResponse mempty
206− utf8data <- B.readFile "httpbin-data/utf8.html"
207− 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
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"
200+ describe "receiving UTF-8 encoded Unicode data" $
201+ it "works" $ do
202+ r <- req GET (httpbin /: "encoding" /: "utf8")
203+ NoReqBody bsResponse mempty
204+ utf8data <- B.readFile "httpbin-data/utf8.html"
205+ responseBody r `shouldBe` utf8data
206+ responseStatusCode r `shouldBe` 200
207+ responseStatusMessage r `shouldBe` "OK"
226208
227209 -- TODO /gzip
228210 -- TODO /deflate