module TD.Data.ProxyType
(ProxyType(..)) where
import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified Data.Text as T
data ProxyType
= ProxyTypeSocks5
{ ProxyType -> Maybe Text
username :: Maybe T.Text
, ProxyType -> Maybe Text
password :: Maybe T.Text
}
| ProxyTypeHttp
{ username :: Maybe T.Text
, password :: Maybe T.Text
, ProxyType -> Maybe Bool
http_only :: Maybe Bool
}
| ProxyTypeMtproto
{ ProxyType -> Maybe Text
secret :: Maybe T.Text
}
deriving (ProxyType -> ProxyType -> Bool
(ProxyType -> ProxyType -> Bool)
-> (ProxyType -> ProxyType -> Bool) -> Eq ProxyType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ProxyType -> ProxyType -> Bool
== :: ProxyType -> ProxyType -> Bool
$c/= :: ProxyType -> ProxyType -> Bool
/= :: ProxyType -> ProxyType -> Bool
Eq, Int -> ProxyType -> ShowS
[ProxyType] -> ShowS
ProxyType -> String
(Int -> ProxyType -> ShowS)
-> (ProxyType -> String)
-> ([ProxyType] -> ShowS)
-> Show ProxyType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ProxyType -> ShowS
showsPrec :: Int -> ProxyType -> ShowS
$cshow :: ProxyType -> String
show :: ProxyType -> String
$cshowList :: [ProxyType] -> ShowS
showList :: [ProxyType] -> ShowS
Show)
instance I.ShortShow ProxyType where
shortShow :: ProxyType -> String
shortShow ProxyTypeSocks5
{ username :: ProxyType -> Maybe Text
username = Maybe Text
username_
, password :: ProxyType -> Maybe Text
password = Maybe Text
password_
}
= String
"ProxyTypeSocks5"
String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
[ String
"username" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
username_
, String
"password" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
password_
]
shortShow ProxyTypeHttp
{ username :: ProxyType -> Maybe Text
username = Maybe Text
username_
, password :: ProxyType -> Maybe Text
password = Maybe Text
password_
, http_only :: ProxyType -> Maybe Bool
http_only = Maybe Bool
http_only_
}
= String
"ProxyTypeHttp"
String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
[ String
"username" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
username_
, String
"password" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
password_
, String
"http_only" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
http_only_
]
shortShow ProxyTypeMtproto
{ secret :: ProxyType -> Maybe Text
secret = Maybe Text
secret_
}
= String
"ProxyTypeMtproto"
String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
[ String
"secret" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
secret_
]
instance AT.FromJSON ProxyType where
parseJSON :: Value -> Parser ProxyType
parseJSON v :: Value
v@(AT.Object Object
obj) = do
String
t <- Object
obj Object -> Key -> Parser String
forall a. FromJSON a => Object -> Key -> Parser a
A..: Key
"@type" :: AT.Parser String
case String
t of
String
"proxyTypeSocks5" -> Value -> Parser ProxyType
parseProxyTypeSocks5 Value
v
String
"proxyTypeHttp" -> Value -> Parser ProxyType
parseProxyTypeHttp Value
v
String
"proxyTypeMtproto" -> Value -> Parser ProxyType
parseProxyTypeMtproto Value
v
String
_ -> Parser ProxyType
forall a. Monoid a => a
mempty
where
parseProxyTypeSocks5 :: A.Value -> AT.Parser ProxyType
parseProxyTypeSocks5 :: Value -> Parser ProxyType
parseProxyTypeSocks5 = String -> (Object -> Parser ProxyType) -> Value -> Parser ProxyType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ProxyTypeSocks5" ((Object -> Parser ProxyType) -> Value -> Parser ProxyType)
-> (Object -> Parser ProxyType) -> Value -> Parser ProxyType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
Maybe Text
username_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"username"
Maybe Text
password_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"password"
ProxyType -> Parser ProxyType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ProxyType -> Parser ProxyType) -> ProxyType -> Parser ProxyType
forall a b. (a -> b) -> a -> b
$ ProxyTypeSocks5
{ username :: Maybe Text
username = Maybe Text
username_
, password :: Maybe Text
password = Maybe Text
password_
}
parseProxyTypeHttp :: A.Value -> AT.Parser ProxyType
parseProxyTypeHttp :: Value -> Parser ProxyType
parseProxyTypeHttp = String -> (Object -> Parser ProxyType) -> Value -> Parser ProxyType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ProxyTypeHttp" ((Object -> Parser ProxyType) -> Value -> Parser ProxyType)
-> (Object -> Parser ProxyType) -> Value -> Parser ProxyType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
Maybe Text
username_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"username"
Maybe Text
password_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"password"
Maybe Bool
http_only_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"http_only"
ProxyType -> Parser ProxyType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ProxyType -> Parser ProxyType) -> ProxyType -> Parser ProxyType
forall a b. (a -> b) -> a -> b
$ ProxyTypeHttp
{ username :: Maybe Text
username = Maybe Text
username_
, password :: Maybe Text
password = Maybe Text
password_
, http_only :: Maybe Bool
http_only = Maybe Bool
http_only_
}
parseProxyTypeMtproto :: A.Value -> AT.Parser ProxyType
parseProxyTypeMtproto :: Value -> Parser ProxyType
parseProxyTypeMtproto = String -> (Object -> Parser ProxyType) -> Value -> Parser ProxyType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ProxyTypeMtproto" ((Object -> Parser ProxyType) -> Value -> Parser ProxyType)
-> (Object -> Parser ProxyType) -> Value -> Parser ProxyType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
Maybe Text
secret_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"secret"
ProxyType -> Parser ProxyType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ProxyType -> Parser ProxyType) -> ProxyType -> Parser ProxyType
forall a b. (a -> b) -> a -> b
$ ProxyTypeMtproto
{ secret :: Maybe Text
secret = Maybe Text
secret_
}
parseJSON Value
_ = Parser ProxyType
forall a. Monoid a => a
mempty
instance AT.ToJSON ProxyType where
toJSON :: ProxyType -> Value
toJSON ProxyTypeSocks5
{ username :: ProxyType -> Maybe Text
username = Maybe Text
username_
, password :: ProxyType -> Maybe Text
password = Maybe Text
password_
}
= [Pair] -> Value
A.object
[ Key
"@type" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"proxyTypeSocks5"
, Key
"username" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
username_
, Key
"password" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
password_
]
toJSON ProxyTypeHttp
{ username :: ProxyType -> Maybe Text
username = Maybe Text
username_
, password :: ProxyType -> Maybe Text
password = Maybe Text
password_
, http_only :: ProxyType -> Maybe Bool
http_only = Maybe Bool
http_only_
}
= [Pair] -> Value
A.object
[ Key
"@type" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"proxyTypeHttp"
, Key
"username" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
username_
, Key
"password" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
password_
, Key
"http_only" Key -> Maybe Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Bool
http_only_
]
toJSON ProxyTypeMtproto
{ secret :: ProxyType -> Maybe Text
secret = Maybe Text
secret_
}
= [Pair] -> Value
A.object
[ Key
"@type" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"proxyTypeMtproto"
, Key
"secret" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
secret_
]