{-# LANGUAGE OverloadedStrings #-} {-| Module : Kino.Request Description : Contains code for issuing http requests Contains code for issuing http requests -} module Kino.Request (getMovies, queryMovies) where import Control.Lens import Data.Aeson import Network.Wreq hiding (Options) import qualified Data.Text as T import qualified Network.Wreq as WR (Options) import Kino.Types -- | Sends a request and unwraps the top level respone data structure 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" _ (Just d)) -> Right d (JSONResponse _ m _) -> Left m -- | Requests a list of all movies getMovies :: IO (Either T.Text JSONListMovies) getMovies = makeRequest "https://yts.mx/api/v2/list_movies.json" (defaults & param "limit" .~ ["50"]) -- | Requests a list of all movies matching a query term queryMovies :: T.Text -> IO (Either T.Text JSONListMovies) queryMovies q = makeRequest "https://yts.mx/api/v2/list_movies.json" (defaults & param "query_term" .~ [q] & param "limit" .~ ["50"])