module OANDA.Types
( OandaEnv
, apiType
, accessToken
, sandboxAuth
, practiceAuth
, liveAuth
, APIType (..)
, apiEndpoint
, AccessToken (..)
, AccountID (..)
, Side (..)
, InstrumentText
) where
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative (pure)
#endif
import Control.Monad (mzero)
import Data.Aeson
import Data.ByteString (ByteString)
import Data.Text (Text, unpack)
import GHC.Generics (Generic)
data OandaEnv = OandaEnv
{ apiType :: APIType
, accessToken :: Maybe AccessToken
} deriving (Show)
sandboxAuth :: OandaEnv
sandboxAuth = OandaEnv Sandbox Nothing
practiceAuth :: AccessToken -> OandaEnv
practiceAuth = OandaEnv Practice . Just
liveAuth :: AccessToken -> OandaEnv
liveAuth = OandaEnv Live . Just
data APIType = Sandbox
| Practice
| Live
deriving (Show)
apiEndpoint :: APIType -> String
apiEndpoint Sandbox = "http://api-sandbox.oanda.com"
apiEndpoint Practice = "https://api-fxpractice.oanda.com"
apiEndpoint Live = "https://api-fxtrade.oanda.com"
newtype AccessToken = AccessToken { unAccessToken :: ByteString }
deriving (Show)
newtype AccountID = AccountID { unAccountID :: Int}
deriving (Show)
data Side = Buy
| Sell
deriving (Show, Generic)
instance FromJSON Side where
parseJSON (String s) = fmap readSide (pure $ unpack s)
parseJSON _ = mzero
readSide :: String -> Side
readSide "buy" = Buy
readSide "sell" = Sell
readSide _ = error "No parse Side"
type InstrumentText = Text