aboutsummaryrefslogtreecommitdiff
path: root/src/lib/haskell/Utils/HTTP.hs
blob: 87635ced2c1fcae86d3f9bf731bb126d8bcea45a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module Utils.HTTP
  ( get
  ) where

import           Control.Exception    (SomeException, try)
import           Data.ByteString      (ByteString)
import qualified Data.ByteString.Lazy as BS
import           Data.Text            (Text)
import qualified Data.Text            as T
import           Network.HTTP.Conduit

import           Model.URL

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

unsafeGetPage :: (ByteString -> Text) -> URL -> IO Text
unsafeGetPage decode url = (decode . BS.toStrict) <$> simpleHttp (T.unpack url)

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