aboutsummaryrefslogtreecommitdiff
path: root/src/lib/haskell/Utils/HTTP.hs
diff options
context:
space:
mode:
authorJoris2018-06-19 22:49:16 +0200
committerJoris2018-06-19 22:49:16 +0200
commit149a0470b73781022e584aaeaa7ce871d6f4173b (patch)
treec1cc762e105ae19e7f3daaa3e9279a467dbaa3dc /src/lib/haskell/Utils/HTTP.hs
parent5d921c9a2b0a7a8f1a1bb5642cbefa516cbbe4cc (diff)
downloadad-listener-149a0470b73781022e584aaeaa7ce871d6f4173b.tar.gz
ad-listener-149a0470b73781022e584aaeaa7ce871d6f4173b.tar.bz2
ad-listener-149a0470b73781022e584aaeaa7ce871d6f4173b.zip
Add automatic tests on remote pages
Diffstat (limited to 'src/lib/haskell/Utils/HTTP.hs')
-rw-r--r--src/lib/haskell/Utils/HTTP.hs22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/lib/haskell/Utils/HTTP.hs b/src/lib/haskell/Utils/HTTP.hs
new file mode 100644
index 0000000..87635ce
--- /dev/null
+++ b/src/lib/haskell/Utils/HTTP.hs
@@ -0,0 +1,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)