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

import Control.Exception (SomeException, try)

import Data.Text (Text)
import qualified Data.Text as T

import Network.HTTP (simpleHTTP, getRequest, getResponseBody)

import Model.URL

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

unsafeGetPage :: URL -> IO Text
unsafeGetPage url = simpleHTTP (getRequest (T.unpack 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)