on master help sign in
Start working on response interpretation and inspecting
commitd3afecec243664a934168c14b3a32efce3d876e2 authormrkkrp <markkarpov@opmbx.org> dateFri 4 Nov 2016, 03:24 (10 years ago) parent1df79c7
1 file changed+66−5
Network/HTTP/Req.hs+66−5
Network/HTTP/Req.hs+66−5
@@ -168,7 +168,15 @@ module Network.HTTP.Req
168168 , responseTimeout
169169 , httpVersion
170170 -- * Response
171− -- $response
171+ -- ** Response interpretations
172+ , IgnoredResponseBody
173+ -- ** Inspecting a response
174+ , responseBody
175+ , responseStatusCode
176+ , responseStatusMessage
177+ , responseHeader
178+ , responseCookieJar
179+ -- ** Defining your own interpretation
172180 , HttpResponse (..)
173181 -- * Other
174182 , CanHaveBody (..)
@@ -983,15 +991,68 @@ httpVersion major minor = withRequest $ \x ->
983991 Y.HttpVersion (fromIntegral major) (fromIntegral minor) }
984992
985993----------------------------------------------------------------------------
986−-- Response
994+-- Response interpretations
987995
988−-- $response
989−--
990−-- Something.
996+data IgnoredResponseBody = IgnoredResponseBody (L.Response ())
997+
998+----------------------------------------------------------------------------
999+-- Inspecting a response
1000+
1001+-- | Get response body.
1002+
1003+responseBody
1004+ :: HttpResponse response
1005+ => response
1006+ -> HttpResponseBody response
1007+responseBody = L.responseBody . toVanillaResponse
1008+
1009+-- | Get response status code.
1010+
1011+responseStatusCode
1012+ :: HttpResponse response
1013+ => response
1014+ -> Word
1015+responseStatusCode =
1016+ fromIntegral . Y.statusCode . L.responseStatus . toVanillaResponse
1017+
1018+-- | Get response status message.
1019+
1020+responseStatusMessage
1021+ :: HttpResponse response
1022+ => response
1023+ -> ByteString
1024+responseStatusMessage =
1025+ Y.statusMessage . L.responseStatus . toVanillaResponse
1026+
1027+-- | Look a particular header from a response.
1028+
1029+responseHeader
1030+ :: HttpResponse response
1031+ => response -- ^ Response interpretation
1032+ -> ByteString -- ^ Header to lookup
1033+ -> Maybe ByteString -- ^ Header value if found
1034+responseHeader r h =
1035+ (lookup (CI.mk h) . L.responseHeaders . toVanillaResponse) r
1036+
1037+-- | Get response 'L.CookieJar'.
1038+
1039+responseCookieJar
1040+ :: HttpResponse response
1041+ => response
1042+ -> L.CookieJar
1043+responseCookieJar = L.responseCookieJar . toVanillaResponse
1044+
1045+----------------------------------------------------------------------------
1046+-- Response — defining your own interpretation
9911047
9921048-- | Here we need to provide various options how to consume responses.
9931049
9941050class HttpResponse response where
1051+
1052+ type HttpResponseBody response :: *
1053+
1054+ toVanillaResponse :: response -> L.Response (HttpResponseBody response)
1055+
9951056 getHttpResponse :: L.Manager -> L.Request -> IO response
9961057
9971058----------------------------------------------------------------------------