module Network.API.TheMovieDB.Generic (getAndParse, getOrFail) where
import Network.API.TheMovieDB.Types.Context
import Network.API.TheMovieDB.Types.API
import Data.Aeson
getAndParse :: FromJSON a => Context -> Path -> Params -> IO (Either Error a)
getAndParse ctx path params =
do httpResult <- ioFunc ctx (apiKey ctx) path params
return $ case httpResult of
Left err -> Left err
Right body -> maybe (parseErr body) Right $ decode body
where parseErr j = Left $ ParseError ("failed to parse JSON: " ++ show j)
getOrFail :: FromJSON a => IO (Either Error a) -> IO a
getOrFail toCheck = toCheck >>= either (fail . show) return