packages feed

themoviedb-0.1.0.1 / Network / API / TheMovieDB / Generic.hs

{-This file is part of the Haskell package themoviedb. It is subject tothe license terms in the LICENSE file found in the top-level directoryof this distribution and at git://pmade.com/themoviedb/LICENSE. Nopart of themoviedb package, including this file, may be copied,modified, propagated, or distributed except according to the termscontained in the LICENSE file.-}module Network.API.TheMovieDB.Generic (getAndParse, getOrFail) whereimport Network.API.TheMovieDB.Types.Contextimport Network.API.TheMovieDB.Types.APIimport Data.Aeson-- Helper function to fetch and decode JSON.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)-- Helper function to fail or return the right part of an either.getOrFail :: FromJSON a => IO (Either Error a) -> IO agetOrFail toCheck = toCheck >>= either (fail . show) return