{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Tesla.Auth (AuthInfo(..),
clientID, clientSecret, email, password, bearerToken,
fromToken,
AuthResponse(..), access_token, expires_in, refresh_token,
HasTeslaAuth(..)
) where
import Control.Lens
import Data.Aeson (FromJSON (..), Options, ToJSON (..), defaultOptions, fieldLabelModifier,
genericParseJSON, genericToJSON)
import Generics.Deriving.Base (Generic)
data AuthInfo = AuthInfo {
AuthInfo -> String
_clientID :: String
, AuthInfo -> String
_clientSecret :: String
, AuthInfo -> String
_email :: String
, AuthInfo -> String
_password :: String
, AuthInfo -> String
_bearerToken :: String
} deriving(Int -> AuthInfo -> ShowS
[AuthInfo] -> ShowS
AuthInfo -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AuthInfo] -> ShowS
$cshowList :: [AuthInfo] -> ShowS
show :: AuthInfo -> String
$cshow :: AuthInfo -> String
showsPrec :: Int -> AuthInfo -> ShowS
$cshowsPrec :: Int -> AuthInfo -> ShowS
Show)
makeLenses ''AuthInfo
fromToken :: String -> AuthInfo
fromToken :: String -> AuthInfo
fromToken String
t = AuthInfo{_bearerToken :: String
_bearerToken=String
t, _clientID :: String
_clientID=String
"", _clientSecret :: String
_clientSecret=String
"", _email :: String
_email=String
"", _password :: String
_password=String
""}
jsonOpts :: Data.Aeson.Options
jsonOpts :: Options
jsonOpts = Options
defaultOptions {
fieldLabelModifier :: ShowS
fieldLabelModifier = forall a. (a -> Bool) -> [a] -> [a]
dropWhile (forall a. Eq a => a -> a -> Bool
== Char
'_')
}
data AuthResponse = AuthResponse {
AuthResponse -> String
_access_token :: String
, AuthResponse -> Int
_expires_in :: Int
, AuthResponse -> String
_refresh_token :: String
} deriving(forall x. Rep AuthResponse x -> AuthResponse
forall x. AuthResponse -> Rep AuthResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep AuthResponse x -> AuthResponse
$cfrom :: forall x. AuthResponse -> Rep AuthResponse x
Generic, Int -> AuthResponse -> ShowS
[AuthResponse] -> ShowS
AuthResponse -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AuthResponse] -> ShowS
$cshowList :: [AuthResponse] -> ShowS
show :: AuthResponse -> String
$cshow :: AuthResponse -> String
showsPrec :: Int -> AuthResponse -> ShowS
$cshowsPrec :: Int -> AuthResponse -> ShowS
Show)
makeLenses ''AuthResponse
class Monad m => HasTeslaAuth m where
teslaAuth :: m AuthInfo
instance FromJSON AuthResponse where
parseJSON :: Value -> Parser AuthResponse
parseJSON = forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON Options
jsonOpts
instance ToJSON AuthResponse where
toJSON :: AuthResponse -> Value
toJSON = forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON Options
jsonOpts