module Network.API.TheMovieDB.HTTP
( apiGET
) where
import Network.API.TheMovieDB.Types.API
import qualified Network.HTTP as H
import Network.URI
apiBaseURL :: String
apiBaseURL = "http://api.themoviedb.org/3/"
mkAPIRequest :: Key -> Path -> Params -> H.Request Body
mkAPIRequest key path params =
case parseURI url of
Nothing -> error ("generated an invalid URI: " ++ url)
Just uri -> H.insertHeaders headers $ request uri
where query = H.urlEncodeVars $ params ++ [("api_key", key)]
url = apiBaseURL ++ path ++ "?" ++ query
headers = [H.Header H.HdrAccept "application/json"]
request = H.mkRequest H.GET
apiGET :: Key -> Path -> Params -> IO Response
apiGET key path params =
do result <- H.simpleHTTP $ mkAPIRequest key path params
return $ case result of
Left err -> Left $ NetworkError (show err)
Right r -> Right $ H.rspBody r