{-# LANGUAGE OverloadedStrings #-} module Request where import JSONTypes import Network.Wreq import qualified Network.Wreq as WR (Options) import Control.Lens import Data.Aeson import Data.Text as T import Data.Aeson.Lens (key, nth) import qualified Data.ByteString.Lazy as BL makeRequest :: (FromJSON a) => String -> WR.Options -> IO (Either T.Text a) makeRequest url opts = do r <- asJSON =<< getWith opts url pure $ case (r ^. responseBody) of (JSONResponse "ok" _ d) -> Right d (JSONResponse _ m _) -> Left m 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])