{-# LANGUAGE DataKinds     #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeOperators #-}

module Zero.Bitfinex.Handlers
  (
    getPublicTicker
  ) where

import           Control.Monad.IO.Class (liftIO)
import           Data.Text (Text)
import           Network.HTTP.Client (newManager)
import           Network.HTTP.Client.TLS (tlsManagerSettings)
import           Servant (Handler(..))
import           Servant.Client
import           Zero.Bitfinex.API
import           Zero.Bitfinex.Internal

------------------------------------------------------------------------------
publicTicker :: Text -> ClientM (Ticker)

publicTicker = client bitfinexAPI

------------------------------------------------------------------------------

getPublicTicker :: Text -> Handler (Ticker)
getPublicTicker market = do
  manager <- liftIO $ newManager tlsManagerSettings
  res <- liftIO $ runClientM (publicTicker market) (ClientEnv manager (BaseUrl Https "api.bitfinex.com" 443 ""))
  case res of
    Left err ->
      error $ show err
    Right ms ->
      return ms
