-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2018-11-22_1.hs
46 lines (34 loc) · 1.35 KB
/
2018-11-22_1.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
import qualified Network.HTTP.Client as Http
import qualified Network.HTTP.Types.Status as Http (statusCode)
import Control.Exception (try, throwIO)
import Data.ByteString.Lazy (ByteString)
pattern BadStatus :: Int -> Http.HttpException
pattern BadStatus n <-
Http.HttpExceptionRequest _
(Http.StatusCodeException (responseStatusCode -> n) _)
pattern Status404 :: Http.HttpException
pattern Status404 <- BadStatus 404
responseStatusCode :: Http.Response a -> Int
responseStatusCode = Http.statusCode . Http.responseStatus
-- | Make a request and return `Nothing` if the response is 404.
getMaybe
:: Http.Manager
-> Http.Request
-> IO (Maybe (Http.Response ByteString))
getMaybe manager request = do
result <- try (Http.httpLbs request manager)
case result of
Right response -> pure (Just response)
Left Status404 -> pure Nothing
Left err -> throwIO err
main :: IO ()
main = do
manager <- Http.newManager Http.defaultManagerSettings
-- NOTE: parseUrlThrow throws an exception for non-2XX response codes
let mkRequest = Http.parseUrlThrow
-- e.g. Just
mkRequest "http://httpbin.org/status/200" >>= getMaybe manager >>= print
-- e.g. Nothing
mkRequest "http://httpbin.org/status/404" >>= getMaybe manager >>= print