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
type RequestFunction = (Path -> QueryText -> IO (Either Error Body))
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)
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))
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
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
runTheMovieDB ::
Settings ->
TheMovieDB a ->
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
runTheMovieDBWithManager ::
Manager ->
Settings ->
TheMovieDB a ->
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)
runTheMovieDBWithRequestFunction ::
RequestFunction ->
TheMovieDB a ->
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