module OANDA.Internal.Types
( OandaEnv
, apiType
, accessToken
, practiceAuth
, liveAuth
, APIType (..)
, AccessToken (..)
, AccountID (..)
, InstrumentText
, InstrumentName (..)
, AccountUnits (..)
, Currency (..)
, OandaZonedTime (..)
) where
import Control.Applicative ((<|>))
import Control.Lens (from, view)
import qualified Data.ByteString as BS
import Data.Thyme.Clock.POSIX (posixTime)
import Data.Thyme.Time.Core (fromMicroseconds)
import OANDA.Internal.Import
import Debug.Trace
data OandaEnv = OandaEnv
{ apiType :: APIType
, accessToken :: AccessToken
} deriving (Show)
practiceAuth :: AccessToken -> OandaEnv
practiceAuth = OandaEnv Practice
liveAuth :: AccessToken -> OandaEnv
liveAuth = OandaEnv Live
data APIType
= Practice
| Live
deriving (Show)
newtype AccessToken = AccessToken { unAccessToken :: BS.ByteString }
deriving (Show)
newtype AccountID = AccountID { unAccountID :: String }
deriving (Show, FromJSON, ToJSON)
type InstrumentText = Text
newtype InstrumentName = InstrumentName { unInstrumentName :: Text }
deriving (Show, FromJSON, ToJSON, IsString)
newtype AccountUnits = AccountUnits { unAccountUnits :: Text }
deriving (Show, FromJSON, ToJSON, IsString)
newtype Currency = Currency { unCurrency :: Text }
deriving (Show, FromJSON, ToJSON, IsString)
newtype OandaZonedTime = OandaZonedTime { unOandaZonedTime :: ZonedTime }
deriving (Show, Eq, ToJSON)
instance FromJSON OandaZonedTime where
parseJSON v = OandaZonedTime <$> (parseJSON v <|> parseZonedFromEpoch v)
where
parseZonedFromEpoch :: Value -> Parser ZonedTime
parseZonedFromEpoch v' = do
(secondsSinceEpoch :: Double) <- parseJSONFromString v'
let
microsecondsSinceEpoch = secondsSinceEpoch * 1e6
(timeInUTC :: UTCTime) = view (from posixTime) (fromMicroseconds (round microsecondsSinceEpoch) :: NominalDiffTime)
(timeInZoned :: ZonedTime) = view zonedTime (utc, timeInUTC)
return $ traceShow (secondsSinceEpoch, microsecondsSinceEpoch, timeInUTC, timeInZoned) timeInZoned