req 0.4.0 → 0.5.0
raw patch · 5 files changed
+58/−53 lines, 5 files
Files
- CHANGELOG.md +7/−0
- Network/HTTP/Req.hs +40/−26
- httpbin-tests/Network/HTTP/ReqSpec.hs +6/−17
- pure-tests/Network/HTTP/ReqSpec.hs +1/−6
- req.cabal +4/−4
CHANGELOG.md view
@@ -1,3 +1,10 @@+## Req 0.5.0++* Changed the signature of the `makeResponseBodyPreview` from `response ->+ IO ByteString` to `response -> ByteString`.++* Minor documentation improvements.+ ## Req 0.4.0 * Added the `Req` monad and `runReq` function to run it. This allows to use
Network/HTTP/Req.hs view
@@ -496,7 +496,8 @@ -- When writing a library, keep your API polymorphic in terms of -- 'MonadHttp', only define instance of 'MonadHttp' in final application. -- Another option is to use @newtype@ wrapped monad stack and define--- 'MonadHttp' for it.+-- 'MonadHttp' for it. As of version /0.4.0/, the 'Req' monad is provided+-- for this out-of-the-box. -- | A type class for monads that support performing HTTP requests. -- Typically, you only need to define the 'handleHttpException' method@@ -567,10 +568,10 @@ , httpConfigAltManager = Nothing , httpConfigCheckResponse = \_ response -> let statusCode = responseStatusCode response in- unless (200 <= statusCode && statusCode < 300) $ do- chunk <- makeResponseBodyPreview response- let vresponse = toVanillaResponse response- LI.throwHttp (L.StatusCodeException (void vresponse) chunk)+ unless (200 <= statusCode && statusCode < 300) $+ let chunk = makeResponseBodyPreview response+ vresponse = toVanillaResponse response+ in LI.throwHttp (L.StatusCodeException (void vresponse) chunk) , httpConfigRetryPolicy = def , httpConfigRetryJudge = \_ r -> return $ responseStatusCode r `elem`@@ -931,7 +932,7 @@ -- 'queryFlag'. -- -- This body option sets the @Content-Type@ header to--- @\"application/x-www-from-urlencoded\"@ value.+-- @\"application/x-www-form-urlencoded\"@ value. newtype ReqBodyUrlEnc = ReqBodyUrlEnc FormUrlEncodedParam @@ -1336,7 +1337,7 @@ ---------------------------------------------------------------------------- -- Response interpretations --- | Make a request and ignore body of response.+-- | Make a request and ignore the body of the response. data IgnoreResponse = IgnoreResponse (L.Response ()) @@ -1345,15 +1346,15 @@ toVanillaResponse (IgnoreResponse response) = response getHttpResponse request manager = IgnoreResponse <$> liftIO (L.httpNoBody request manager)- makeResponseBodyPreview _ = return "<ignored response>"+ makeResponseBodyPreview _ = "<ignored response>" -- | Use this as the fourth argument of 'req' to specify that you want it to--- return the 'IgnoreResponse' interpretation.+-- ignore the response body. ignoreResponse :: Proxy IgnoreResponse ignoreResponse = Proxy --- | Make a request and interpret body of response as JSON. The+-- | Make a request and interpret the body of the response as JSON. The -- 'handleHttpException' method of 'MonadHttp' instance corresponding to -- monad in which you use 'req' will determine what to do in the case when -- parsing fails (the 'JsonHttpException' constructor will be used).@@ -1374,15 +1375,16 @@ . L.responseBody $ response return $ JsonResponse response { L.responseBody = x } preview- makeResponseBodyPreview (JsonResponse _ preview) = return preview+ makeResponseBodyPreview (JsonResponse _ preview) = preview --- | Use this as the forth argument of 'req' to specify that you want it to+-- | Use this as the fourth argument of 'req' to specify that you want it to -- return the 'JsonResponse' interpretation. jsonResponse :: Proxy (JsonResponse a) jsonResponse = Proxy --- | Make a request and interpret body of response as a strict 'ByteString'.+-- | Make a request and interpret the body of the response as a strict+-- 'ByteString'. newtype BsResponse = BsResponse (L.Response ByteString) @@ -1390,18 +1392,17 @@ type HttpResponseBody BsResponse = ByteString toVanillaResponse (BsResponse response) = response getHttpResponse request manager =- L.withResponse request manager $ \response -> do- chunks <- L.brConsume (L.responseBody response)- return $ BsResponse response { L.responseBody = B.concat chunks }- makeResponseBodyPreview = return . B.take bodyPreviewLength . responseBody+ BsResponse <$> httpBs request manager+ makeResponseBodyPreview =+ B.take bodyPreviewLength . responseBody --- | Use this as the forth argument of 'req' to specify that you want to--- interpret response body as a strict 'ByteString'.+-- | Use this as the fourth argument of 'req' to specify that you want to+-- interpret the response body as a strict 'ByteString'. bsResponse :: Proxy BsResponse bsResponse = Proxy --- | Make a request and interpret body of response as a lazy+-- | Make a request and interpret the body of the response as a lazy -- 'BL.ByteString'. newtype LbsResponse = LbsResponse (L.Response BL.ByteString)@@ -1411,14 +1412,23 @@ toVanillaResponse (LbsResponse response) = response getHttpResponse request manager = LbsResponse <$> L.httpLbs request manager- makeResponseBodyPreview = return . BL.toStrict . BL.take 1027 . responseBody+ makeResponseBodyPreview =+ BL.toStrict . BL.take bodyPreviewLength . responseBody --- | Use this as the forth argument of 'req' to specify that you want to--- interpret response body as a lazy 'BL.ByteString'.+-- | Use this as the fourth argument of 'req' to specify that you want to+-- interpret the response body as a lazy 'BL.ByteString'. lbsResponse :: Proxy LbsResponse lbsResponse = Proxy +-- | Perform a 'L.Request' using given 'L.Manager' and return the response+-- as a strict 'ByteString'.++httpBs :: L.Request -> L.Manager -> IO (L.Response ByteString)+httpBs request manager = L.withResponse request manager $ \response -> do+ chunks <- L.brConsume (L.responseBody response)+ return response { L.responseBody = B.concat chunks }+ ---------------------------------------------------------------------------- -- Inspecting a response @@ -1497,9 +1507,12 @@ -- length to 1024 bytes. This is mainly useful for inclusion of response -- body fragments in exceptions. --- -- @since 0.3.0+ -- __Note__: in versions 0.3.0–0.4.0 this function returned @'IO'+ -- 'ByteString'@.+ --+ -- @since 0.5.0 - makeResponseBodyPreview :: response -> IO ByteString+ makeResponseBodyPreview :: response -> ByteString ---------------------------------------------------------------------------- -- Other@@ -1536,7 +1549,8 @@ = VanillaHttpException L.HttpException -- ^ A wrapper with an 'L.HttpException' from "Network.HTTP.Client" | JsonHttpException String- -- ^ A wrapper with Aeson-produced 'String' describing why decoding failed+ -- ^ A wrapper with Aeson-produced 'String' describing why decoding+ -- failed deriving (Show, Typeable, Generic) instance Exception HttpException
httpbin-tests/Network/HTTP/ReqSpec.hs view
@@ -323,40 +323,29 @@ instance MonadHttp IO where handleHttpException = throwIO -instance MonadHttp (ReaderT HttpConfig IO) where- handleHttpException = liftIO . throwIO- getHttpConfig = ask- ---------------------------------------------------------------------------- -- Helpers -- | Run request with such settings that it does not signal error on adverse -- response status codes. -prepareForShit- :: ReaderT HttpConfig IO a- -> IO a-prepareForShit m = runReaderT m def { httpConfigCheckResponse = noNoise }+prepareForShit :: Req a -> IO a+prepareForShit = runReq def { httpConfigCheckResponse = noNoise } where noNoise _ _ = return () -- | Run request with such settings that it throws on any response. -blindlyThrowing- :: ReaderT HttpConfig IO a- -> IO a-blindlyThrowing m = runReaderT m def { httpConfigCheckResponse = doit }+blindlyThrowing :: Req a -> IO a+blindlyThrowing = runReq def { httpConfigCheckResponse = doit } where doit _ _ = error "Oops!" -- | Run request with such settings that every retry increments the given -- @'IORef' 'Int'@. -countingRetries- :: IORef Int- -> ReaderT HttpConfig IO a- -> IO a-countingRetries nref m = runReaderT m def+countingRetries :: IORef Int -> Req a -> IO a+countingRetries nref = runReq def { httpConfigCheckResponse = noNoise , httpConfigRetryPolicy = R.constantDelay 50000 <> R.limitRetries 5 , httpConfigRetryJudge = judge }
pure-tests/Network/HTTP/ReqSpec.hs view
@@ -17,7 +17,6 @@ where import Control.Exception (throwIO)-import Control.Monad.Reader import Data.Aeson (ToJSON (..)) import Data.ByteString (ByteString) import Data.Default.Class@@ -54,7 +53,7 @@ describe "config" $ it "getHttpConfig has effect on resulting request" $ property $ \config -> do- request <- runReaderT (req_ GET url NoReqBody mempty) config+ request <- runReq config (req_ GET url NoReqBody mempty) L.proxy request `shouldBe` httpConfigProxy config L.redirectCount request `shouldBe` httpConfigRedirectCount config @@ -295,10 +294,6 @@ instance MonadHttp IO where handleHttpException = throwIO--instance MonadHttp (ReaderT HttpConfig IO) where- handleHttpException = liftIO . throwIO- getHttpConfig = ask instance Arbitrary HttpConfig where arbitrary = do
req.cabal view
@@ -1,5 +1,5 @@ name: req-version: 0.4.0+version: 0.5.0 cabal-version: >= 1.18 tested-with: GHC==7.8.4, GHC==7.10.3, GHC==8.0.2, GHC==8.2.1 license: BSD3@@ -50,7 +50,7 @@ build-depends: semigroups == 0.18.* exposed-modules: Network.HTTP.Req if flag(dev)- ghc-options: -Wall -Werror+ ghc-options: -O0 -Wall -Werror else ghc-options: -O2 -Wall default-language: Haskell2010@@ -76,7 +76,7 @@ , text >= 0.2 && < 1.3 , time >= 1.2 && < 1.9 if flag(dev)- ghc-options: -Wall -Werror+ ghc-options: -O0 -Wall -Werror else ghc-options: -O2 -Wall default-language: Haskell2010@@ -101,7 +101,7 @@ , text >= 0.2 && < 1.3 , unordered-containers >= 0.2.5 && < 0.2.9 if flag(dev)- ghc-options: -Wall -Werror+ ghc-options: -O0 -Wall -Werror else ghc-options: -O2 -Wall default-language: Haskell2010