-- |
--
-- Copyright:
--   This file is part of the package themoviedb.  It is subject to
--   the license terms in the LICENSE file found in the top-level
--   directory of this distribution and at:
--
--     https://github.com/pjones/themoviedb
--
--   No part of this package, including this file, may be copied,
--   modified, propagated, or distributed except according to the terms
--   contained in the LICENSE file.
--
-- License: MIT
module Network.API.TheMovieDB.Internal.TheMovieDB
  ( TheMovieDB,
    RequestFunction,
    getAndParse,
    tmdbError,
    runTheMovieDB,
    runTheMovieDBWithManager,
    runTheMovieDBWithRequestFunction,
  )
where

import Control.Monad.Except
import Data.Aeson
import Network.API.TheMovieDB.Internal.HTTP
import Network.API.TheMovieDB.Internal.Settings
import Network.API.TheMovieDB.Internal.Types
import Network.HTTP.Client (Manager, newManager)
import Network.HTTP.Client.TLS (tlsManagerSettings)
import Network.HTTP.Types

-- | The type for functions that make requests to the API (or pretend
-- to make a request for testing purposes).
type RequestFunction = (Path -> QueryText -> IO (Either Error Body))

-- | Result type for operations involving TheMovieDB API.
newtype TheMovieDB a = TheMovieDB
  {forall a.
TheMovieDB a -> ReaderT RequestFunction (ExceptT Error IO) a
unTMDB :: ReaderT RequestFunction (ExceptT Error IO) a}
  deriving newtype (forall a b. a -> TheMovieDB b -> TheMovieDB a
forall a b. (a -> b) -> TheMovieDB a -> TheMovieDB b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> TheMovieDB b -> TheMovieDB a
$c<$ :: forall a b. a -> TheMovieDB b -> TheMovieDB a
fmap :: forall a b. (a -> b) -> TheMovieDB a -> TheMovieDB b
$cfmap :: forall a b. (a -> b) -> TheMovieDB a -> TheMovieDB b
Functor, Functor TheMovieDB
forall a. a -> TheMovieDB a
forall a b. TheMovieDB a -> TheMovieDB b -> TheMovieDB a
forall a b. TheMovieDB a -> TheMovieDB b -> TheMovieDB b
forall a b. TheMovieDB (a -> b) -> TheMovieDB a -> TheMovieDB b
forall a b c.
(a -> b -> c) -> TheMovieDB a -> TheMovieDB b -> TheMovieDB c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b. TheMovieDB a -> TheMovieDB b -> TheMovieDB a
$c<* :: forall a b. TheMovieDB a -> TheMovieDB b -> TheMovieDB a
*> :: forall a b. TheMovieDB a -> TheMovieDB b -> TheMovieDB b
$c*> :: forall a b. TheMovieDB a -> TheMovieDB b -> TheMovieDB b
liftA2 :: forall a b c.
(a -> b -> c) -> TheMovieDB a -> TheMovieDB b -> TheMovieDB c
$cliftA2 :: forall a b c.
(a -> b -> c) -> TheMovieDB a -> TheMovieDB b -> TheMovieDB c
<*> :: forall a b. TheMovieDB (a -> b) -> TheMovieDB a -> TheMovieDB b
$c<*> :: forall a b. TheMovieDB (a -> b) -> TheMovieDB a -> TheMovieDB b
pure :: forall a. a -> TheMovieDB a
$cpure :: forall a. a -> TheMovieDB a
Applicative, Applicative TheMovieDB
forall a. a -> TheMovieDB a
forall a b. TheMovieDB a -> TheMovieDB b -> TheMovieDB b
forall a b. TheMovieDB a -> (a -> TheMovieDB b) -> TheMovieDB b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> TheMovieDB a
$creturn :: forall a. a -> TheMovieDB a
>> :: forall a b. TheMovieDB a -> TheMovieDB b -> TheMovieDB b
$c>> :: forall a b. TheMovieDB a -> TheMovieDB b -> TheMovieDB b
>>= :: forall a b. TheMovieDB a -> (a -> TheMovieDB b) -> TheMovieDB b
$c>>= :: forall a b. TheMovieDB a -> (a -> TheMovieDB b) -> TheMovieDB b
Monad, Monad TheMovieDB
forall a. IO a -> TheMovieDB a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
liftIO :: forall a. IO a -> TheMovieDB a
$cliftIO :: forall a. IO a -> TheMovieDB a
MonadIO)

-- | Helper function for making a request using the request function
-- stashed away in the reader monad.
runRequest :: Path -> QueryText -> TheMovieDB Body
runRequest :: Path -> QueryText -> TheMovieDB Body
runRequest Path
path QueryText
params = forall a.
ReaderT RequestFunction (ExceptT Error IO) a -> TheMovieDB a
TheMovieDB forall a b. (a -> b) -> a -> b
$ do
  RequestFunction
func <- forall r (m :: * -> *). MonadReader r m => m r
ask
  forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (RequestFunction
func Path
path QueryText
params))

-- | Helper function to preform an HTTP GET and decode the JSON result.
getAndParse :: FromJSON a => Path -> QueryText -> TheMovieDB a
getAndParse :: forall a. FromJSON a => Path -> QueryText -> TheMovieDB a
getAndParse Path
path QueryText
params = do
  Body
body <- Path -> QueryText -> TheMovieDB Body
runRequest Path
path QueryText
params

  case forall a. FromJSON a => Body -> Either Path a
eitherDecode Body
body of
    Left Path
e -> forall a. Error -> TheMovieDB a
tmdbError forall a b. (a -> b) -> a -> b
$ Path -> Maybe Body -> Error
ResponseParseError (Path
"bad JSON: " forall a. [a] -> [a] -> [a]
++ Path
e) (forall a. a -> Maybe a
Just Body
body)
    Right a
a -> forall (m :: * -> *) a. Monad m => a -> m a
return a
a

-- | Create a 'TheMovieDB' value representing an error.
tmdbError :: Error -> TheMovieDB a
tmdbError :: forall a. Error -> TheMovieDB a
tmdbError = forall a.
ReaderT RequestFunction (ExceptT Error IO) a -> TheMovieDB a
TheMovieDB forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError

-- | Execute requests for TheMovieDB with the given API key and produce
-- either an error or a result.
--
-- This version creates a temporary 'Manager' using
-- 'tlsManagerSettings'.  If you want to use an existing 'Manager' you
-- should use 'runTheMovieDBWithManager' instead.
runTheMovieDB ::
  -- | Library settings.
  Settings ->
  -- | The API calls to make.
  TheMovieDB a ->
  -- | Response or error.
  IO (Either Error a)
runTheMovieDB :: forall a. Settings -> TheMovieDB a -> IO (Either Error a)
runTheMovieDB Settings
s TheMovieDB a
t = do
  Manager
m <- ManagerSettings -> IO Manager
newManager ManagerSettings
tlsManagerSettings
  forall a.
Manager -> Settings -> TheMovieDB a -> IO (Either Error a)
runTheMovieDBWithManager Manager
m Settings
s TheMovieDB a
t

-- | Execute requests for TheMovieDB with the given API key and produce
-- either an error or a result.
--
-- This version allows you to provide a 'Manager' value which should
-- have been created to allow TLS requests (e.g., with 'tlsManagerSettings').
runTheMovieDBWithManager ::
  -- | The 'Manager' to use.
  Manager ->
  -- | Library settings.
  Settings ->
  -- | The API calls to make.
  TheMovieDB a ->
  -- | Response or error.
  IO (Either Error a)
runTheMovieDBWithManager :: forall a.
Manager -> Settings -> TheMovieDB a -> IO (Either Error a)
runTheMovieDBWithManager Manager
m Settings
s = forall a. RequestFunction -> TheMovieDB a -> IO (Either Error a)
runTheMovieDBWithRequestFunction (Manager -> Settings -> RequestFunction
apiGET Manager
m Settings
s)

-- | Low-level interface for executing a 'TheMovieDB' using the given
-- request function.
runTheMovieDBWithRequestFunction ::
  -- | The request function to use.
  RequestFunction ->
  -- | The API calls to make.
  TheMovieDB a ->
  -- | Response.
  IO (Either Error a)
runTheMovieDBWithRequestFunction :: forall a. RequestFunction -> TheMovieDB a -> IO (Either Error a)
runTheMovieDBWithRequestFunction RequestFunction
f TheMovieDB a
t = forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT forall a b. (a -> b) -> a -> b
$ forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (forall a.
TheMovieDB a -> ReaderT RequestFunction (ExceptT Error IO) a
unTMDB TheMovieDB a
t) RequestFunction
f