kino/src/Kino/Request.hs

38 lines
1.1 KiB
Haskell
Raw Normal View History

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