module Network.API.TheMovieDB.Internal.Configuration
( Configuration (..),
posterURLs,
)
where
import Data.Aeson
data Configuration = Configuration
{
Configuration -> Text
cfgImageBaseURL :: Text,
Configuration -> Text
cfgImageSecBaseURL :: Text,
Configuration -> [Text]
cfgPosterSizes :: [Text]
}
deriving (forall x. Rep Configuration x -> Configuration
forall x. Configuration -> Rep Configuration x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Configuration x -> Configuration
$cfrom :: forall x. Configuration -> Rep Configuration x
Generic)
instance FromJSON Configuration where
parseJSON :: Value -> Parser Configuration
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Configuration" forall a b. (a -> b) -> a -> b
$ \Object
v ->
Text -> Text -> [Text] -> Configuration
Configuration
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {b}. FromJSON b => Object -> Key -> Parser b
images Object
v Key
"base_url"
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall {b}. FromJSON b => Object -> Key -> Parser b
images Object
v Key
"secure_base_url"
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall {b}. FromJSON b => Object -> Key -> b -> Parser b
imagesM Object
v Key
"poster_sizes" []
where
images :: Object -> Key -> Parser b
images Object
v Key
key = (Object
v forall {b}. FromJSON b => Object -> Key -> Parser b
.: Key
"images") forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (forall {b}. FromJSON b => Object -> Key -> Parser b
.: Key
key)
imagesM :: Object -> Key -> b -> Parser b
imagesM Object
v Key
key b
def = (Object
v forall {b}. FromJSON b => Object -> Key -> Parser b
.: Key
"images") forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\Object
x -> Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
key forall a. Parser (Maybe a) -> a -> Parser a
.!= b
def)
instance ToJSON Configuration where
toJSON :: Configuration -> Value
toJSON Configuration
c =
[Pair] -> Value
object
[ Key
"images"
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [Pair] -> Value
object
[ Key
"base_url" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Configuration -> Text
cfgImageBaseURL Configuration
c,
Key
"secure_base_url" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Configuration -> Text
cfgImageSecBaseURL Configuration
c,
Key
"poster_sizes" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Configuration -> [Text]
cfgPosterSizes Configuration
c
]
]
posterURLs :: Configuration -> Text -> [Text]
posterURLs :: Configuration -> Text -> [Text]
posterURLs Configuration
c Text
p = [Configuration -> Text
cfgImageBaseURL Configuration
c forall a. Semigroup a => a -> a -> a
<> Text
size forall a. Semigroup a => a -> a -> a
<> Text
p | Text
size <- Configuration -> [Text]
cfgPosterSizes Configuration
c]