aboutsummaryrefslogtreecommitdiff
path: root/src/Page.hs
blob: b048410e328b74f4397b89115c2fa48092c47e0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module Page
  ( getPage
  ) where

import Control.Exception (SomeException, try)

import qualified Data.Text as T

import Network.HTTP (simpleHTTP, getRequest, getResponseBody)

import Model.URL

getPage :: URL -> IO (Either T.Text T.Text)
getPage url =
  mapLeft (T.pack . show) <$> (try (unsafeGetPage url) :: IO (Either SomeException T.Text))

unsafeGetPage :: URL -> IO T.Text
unsafeGetPage url = simpleHTTP (getRequest url) >>= (\x -> T.pack <$> getResponseBody x)

mapLeft :: (a -> c) -> Either a b -> Either c b
mapLeft f (Left l) = Left (f l)
mapLeft _ (Right r) = (Right r)