on master help sign in
Add ‘reqCb’, a higher level ‘req'’ alternative
124eba9galagora
CHANGELOG.md+5
@@ -1,3 +1,8 @@
1+## Req 3.7.0
2+
3+* Added `reqCb`, a function that allows you to modify the `Request` object
4+ but otherwise performs the requst for you.
5+
1## Req 3.6.0
6## Req 3.6.0
2
7
3* Added the `httpConfigBodyPreviewLength` configuration parameter to
8* Added the `httpConfigBodyPreviewLength` configuration parameter to
Network/HTTP/Req.hs+54−7
@@ -112,6 +112,7 @@ module Network.HTTP.Req
112 -- $making-a-request
112 -- $making-a-request
113 req,
113 req,
114 reqBr,
114 reqBr,
115+ reqCb,
115 req',
116 req',
116 withReqManager,
117 withReqManager,
117
118
@@ -429,12 +430,7 @@ req ::
429 -- | Response
430 -- | Response
430 m response
431 m response
431req method url body responseProxy options =
432req method url body responseProxy options =
432− reqBr method url body (options <> extraOptions) getHttpResponse
433+ reqCb method url body responseProxy options pure
433− where
434− extraOptions =
435− case acceptHeader responseProxy of
436− Nothing -> mempty
437− Just accept -> header "Accept" accept
438
434
439-- | A version of 'req' that does not use one of the predefined instances of
435-- | A version of 'req' that does not use one of the predefined instances of
440-- 'HttpResponse' but instead allows the user to consume @'L.Response'
436-- 'HttpResponse' but instead allows the user to consume @'L.Response'
@@ -459,7 +455,58 @@ reqBr ::
459 (L.Response L.BodyReader -> IO a) ->
455 (L.Response L.BodyReader -> IO a) ->
460 -- | Result
456 -- | Result
461 m a
457 m a
458+reqBr method url body options consume =
459+ req' method url body options (reqHandler consume)
460+
461+-- | A version of 'req' that takes a callback to modify the @'L.Request',
462+-- but otherwise performs the request identically.
463+--
464+-- @since 3.7.0
465+reqCb ::
466+ ( MonadHttp m,
467+ HttpMethod method,
468+ HttpBody body,
469+ HttpResponse response,
470+ HttpBodyAllowed (AllowsBody method) (ProvidesBody body)
471+ ) =>
472+ -- | HTTP method
473+ method ->
474+ -- | 'Url'—location of resource
475+ Url scheme ->
476+ -- | Body of the request
477+ body ->
478+ -- | A hint how to interpret response
479+ Proxy response ->
480+ -- | Collection of optional parameters
481+ Option scheme ->
482+ -- | Callback to modify the request
483+ (L.Request -> m L.Request) ->
484+ -- | Response
485+ m response
486+reqCb method url body responseProxy options adjustRequest =
462−reqBr method url body options consume = req' method url body options $ \request manager -> do
487+ req' method url body (options <> extraOptions) $ \request manager -> do
488+ request' <- adjustRequest request
489+ reqHandler getHttpResponse request' manager
490+ where
491+ extraOptions =
492+ case acceptHeader responseProxy of
493+ Nothing -> mempty
494+ Just accept -> header "Accept" accept
495+
496+-- | The default handler function that the higher-level request functions
497+-- pass to 'req''. Internal function.
498+--
499+-- @since 3.7.0
500+reqHandler ::
501+ MonadHttp m =>
502+ -- | How to get final result from a 'L.Response'
503+ (L.Response L.BodyReader -> IO b) ->
504+ -- | 'L.Request' to perform
505+ L.Request ->
506+ -- | 'L.Manager' to use
507+ L.Manager ->
508+ m b
509+reqHandler consume request manager = do
463 HttpConfig {..} <- getHttpConfig
510 HttpConfig {..} <- getHttpConfig
464 let wrapVanilla = handle (throwIO . VanillaHttpException)
511 let wrapVanilla = handle (throwIO . VanillaHttpException)
465 wrapExc = handle (throwIO . LI.toHttpException request)
512 wrapExc = handle (throwIO . LI.toHttpException request)