{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}
module Test.WebDriver.Cookies where
import Data.Aeson
import Data.Aeson.TH
import Data.Aeson.Types
import qualified Data.Char as C
import Data.Text (Text)
import Test.WebDriver.JSON
data Cookie = Cookie { Cookie -> Text
cookName :: Text
, Cookie -> Text
cookValue :: Text
, Cookie -> Maybe Text
cookPath :: Maybe Text
, Cookie -> Maybe Text
cookDomain :: Maybe Text
, Cookie -> Maybe Bool
cookSecure :: Maybe Bool
, Cookie -> Maybe Double
cookExpiry :: Maybe Double
} deriving (Cookie -> Cookie -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Cookie -> Cookie -> Bool
$c/= :: Cookie -> Cookie -> Bool
== :: Cookie -> Cookie -> Bool
$c== :: Cookie -> Cookie -> Bool
Eq, Int -> Cookie -> ShowS
[Cookie] -> ShowS
Cookie -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Cookie] -> ShowS
$cshowList :: [Cookie] -> ShowS
show :: Cookie -> String
$cshow :: Cookie -> String
showsPrec :: Int -> Cookie -> ShowS
$cshowsPrec :: Int -> Cookie -> ShowS
Show)
mkCookie :: Text -> Text -> Cookie
mkCookie :: Text -> Text -> Cookie
mkCookie Text
name Text
value = Cookie { cookName :: Text
cookName = Text
name, cookValue :: Text
cookValue = Text
value,
cookPath :: Maybe Text
cookPath = forall a. Maybe a
Nothing, cookDomain :: Maybe Text
cookDomain = forall a. Maybe a
Nothing,
cookSecure :: Maybe Bool
cookSecure = forall a. Maybe a
Nothing, cookExpiry :: Maybe Double
cookExpiry = forall a. Maybe a
Nothing
}
instance FromJSON Cookie where
parseJSON :: Value -> Parser Cookie
parseJSON (Object Object
o) = Text
-> Text
-> Maybe Text
-> Maybe Text
-> Maybe Bool
-> Maybe Double
-> Cookie
Cookie forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromJSON a => Text -> Parser a
req Text
"name"
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. FromJSON a => Text -> Parser a
req Text
"value"
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. FromJSON a => Text -> a -> Parser a
opt Text
"path" forall a. Maybe a
Nothing
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. FromJSON a => Text -> a -> Parser a
opt Text
"domain" forall a. Maybe a
Nothing
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. FromJSON a => Text -> a -> Parser a
opt Text
"secure" forall a. Maybe a
Nothing
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. FromJSON a => Text -> a -> Parser a
opt Text
"expiry" forall a. Maybe a
Nothing
where
req :: FromJSON a => Text -> Parser a
req :: forall a. FromJSON a => Text -> Parser a
req = (Object
o forall a. FromJSON a => Object -> Key -> Parser a
.:) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Key
fromText
opt :: FromJSON a => Text -> a -> Parser a
opt :: forall a. FromJSON a => Text -> a -> Parser a
opt Text
k a
d = Object
o forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
.:?? Text
k forall a. Parser (Maybe a) -> a -> Parser a
.!= a
d
parseJSON Value
v = forall a. String -> Value -> Parser a
typeMismatch String
"Cookie" Value
v
$( deriveToJSON (defaultOptions{omitNothingFields = True, fieldLabelModifier = map C.toLower . drop 4}) ''Cookie )