Skip to content

Commit eeae3d5

Browse files
committed
remove original request from Response
Deliver it instead by way of the Manager's response modifier. See #464 This patch accomplishes the same thing as that one in essentially the same way, but improves upon it by not requiring a request to exist in order to construct a response. This can be useful in e.g. testing / mocking definitions in which we want to express a base fixed response. In other wrods, it is "less breaky" and easier to adopt.
1 parent 00d5803 commit eeae3d5

File tree

6 files changed

+11
-25
lines changed

6 files changed

+11
-25
lines changed

http-client/Network/HTTP/Client.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ module Network.HTTP.Client
181181
, responseHeaders
182182
, responseBody
183183
, responseCookieJar
184-
, getOriginalRequest
185184
, throwErrorStatusCodes
186185
-- ** Response body
187186
, BodyReader

http-client/Network/HTTP/Client/Core.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ responseOpen inputReq manager' = do
206206
wrapExc req0 $ mWrapException manager req0 $ do
207207
(req, res) <- go manager (redirectCount req0) req0
208208
checkResponse req req res
209-
mModifyResponse manager res
209+
mModifyResponse manager (req { requestBody = "" }) res
210210
{ responseBody = wrapExc req0 (responseBody res)
211211
}
212212
where

http-client/Network/HTTP/Client/Manager.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ defaultManagerSettings = ManagerSettings
8989
in handle wrapper
9090
, managerIdleConnectionCount = 512
9191
, managerModifyRequest = return
92-
, managerModifyResponse = return
92+
, managerModifyResponse = const return
9393
, managerProxyInsecure = defaultProxy
9494
, managerProxySecure = defaultProxy
9595
}

http-client/Network/HTTP/Client/Response.hs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ module Network.HTTP.Client.Response
44
( getRedirectedRequest
55
, getResponse
66
, lbsResponse
7-
, getOriginalRequest
87
) where
98

109
import Data.ByteString (ByteString)
@@ -124,7 +123,6 @@ getResponse timeout' req@(Request {..}) mconn cont = do
124123
, responseBody = body
125124
, responseCookieJar = Data.Monoid.mempty
126125
, responseClose' = ResponseClose (cleanup False)
127-
, responseOriginalRequest = req {requestBody = ""}
128126
}
129127

130128
-- | Does this response have no body?
@@ -135,11 +133,3 @@ hasNoBody "HEAD" _ = True
135133
hasNoBody _ 204 = True
136134
hasNoBody _ 304 = True
137135
hasNoBody _ i = 100 <= i && i < 200
138-
139-
-- | Retrieve the orignal 'Request' from a 'Response'
140-
--
141-
-- Note that the 'requestBody' is not available and always set to empty.
142-
--
143-
-- @since 0.7.8
144-
getOriginalRequest :: Response a -> Request
145-
getOriginalRequest = responseOriginalRequest

http-client/Network/HTTP/Client/Types.hs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -693,12 +693,6 @@ data Response body = Response
693693
-- be impossible.
694694
--
695695
-- Since 0.1.0
696-
, responseOriginalRequest :: Request
697-
-- ^ Holds original @Request@ related to this @Response@ (with an empty body).
698-
-- This field is intentionally not exported directly, but made availble
699-
-- via @getOriginalRequest@ instead.
700-
--
701-
-- Since 0.7.8
702696
}
703697
deriving (Show, T.Typeable, Functor, Data.Foldable.Foldable, Data.Traversable.Traversable)
704698

@@ -776,9 +770,12 @@ data ManagerSettings = ManagerSettings
776770
-- Default: no modification
777771
--
778772
-- Since 0.4.4
779-
, managerModifyResponse :: Response BodyReader -> IO (Response BodyReader)
773+
, managerModifyResponse :: Request -> Response BodyReader -> IO (Response BodyReader)
780774
-- ^ Perform the given modification to a @Response@ after receiving it.
781775
--
776+
-- The Request is the corresponding original request (before any redirects)
777+
-- but its body is always discarded.
778+
--
782779
-- Default: no modification
783780
--
784781
-- @since 0.5.5
@@ -818,7 +815,7 @@ data Manager = Manager
818815
, mWrapException :: forall a. Request -> IO a -> IO a
819816
, mModifyRequest :: Request -> IO Request
820817
, mSetProxy :: Request -> Request
821-
, mModifyResponse :: Response BodyReader -> IO (Response BodyReader)
818+
, mModifyResponse :: Request -> Response BodyReader -> IO (Response BodyReader)
822819
-- ^ See 'managerProxy'
823820
}
824821
deriving T.Typeable

http-client/test/Network/HTTP/ClientSpec.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ spec = describe "Client" $ do
6363

6464
context "managerModifyResponse" $ do
6565
it "allows to modify the response status code" $ do
66-
let modify :: Response BodyReader -> IO (Response BodyReader)
67-
modify res = do
66+
let modify :: Request -> Response BodyReader -> IO (Response BodyReader)
67+
modify _req res = do
6868
return res {
6969
responseStatus = (responseStatus res) {
7070
statusCode = 201
@@ -76,8 +76,8 @@ spec = describe "Client" $ do
7676
(statusCode.responseStatus) res `shouldBe` 201
7777

7878
it "modifies the response body" $ do
79-
let modify :: Response BodyReader -> IO (Response BodyReader)
80-
modify res = do
79+
let modify :: Request -> Response BodyReader -> IO (Response BodyReader)
80+
modify _req res = do
8181
reader <- constBodyReader [BS.pack "modified response body"]
8282
return res {
8383
responseBody = reader

0 commit comments

Comments
 (0)