kino/src/Request.hs

27 lines
819 B
Haskell
Raw Normal View History

2021-09-05 19:30:29 +02:00
{-# LANGUAGE OverloadedStrings #-}
module Request where
import JSONTypes
import Network.Wreq
2021-09-06 21:02:39 +02:00
import qualified Network.Wreq as WR (Options)
2021-09-05 19:30:29 +02:00
import Control.Lens
import Data.Aeson
import Data.Text as T
import Data.Aeson.Lens (key, nth)
2021-09-06 21:02:39 +02:00
import qualified Data.ByteString.Lazy as BL
2021-09-05 19:30:29 +02:00
2021-09-06 21:02:39 +02:00
makeRequest :: (FromJSON a) => String -> WR.Options -> IO (Either T.Text a)
makeRequest url opts = do
r <- asJSON =<< getWith opts url
2021-09-05 19:30:29 +02:00
pure $ case (r ^. responseBody) of
(JSONResponse "ok" _ d) -> Right d
(JSONResponse _ m _) -> Left m
2021-09-06 21:02:39 +02:00
getMovies :: IO (Either T.Text JSONListMovies)
getMovies = makeRequest "https://yts.mx/api/v2/list_movies.json" defaults
queryMovies :: T.Text -> IO (Either T.Text JSONListMovies)
queryMovies q = makeRequest "https://yts.mx/api/v2/list_movies.json" (defaults & param "query_term" .~ [q])