module Web.Data.Yahoo.API where
import Control.Lens ((^.))
import Data.Time.Calendar (Day, fromGregorian)
import Network.Wreq (get, responseBody)
import Web.Data.Yahoo.Utils (right)
import Web.Data.Yahoo.Request
( YahooRequest(..),
TimeRange(Before, After, Range),
Interval(Daily, Weekly),
Ticker(..),
requestUrl)
import Web.Data.Yahoo.Response (PriceResponse, tryParseAsPrice)
type Request = YahooRequest
fetch :: YahooRequest -> IO (Either String [PriceResponse])
fetch :: YahooRequest -> IO (Either String [PriceResponse])
fetch YahooRequest
request = do
Response ByteString
response <- String -> IO (Response ByteString)
get (String -> IO (Response ByteString))
-> String -> IO (Response ByteString)
forall a b. (a -> b) -> a -> b
$ YahooRequest -> String
requestUrl YahooRequest
request
Either String [PriceResponse] -> IO (Either String [PriceResponse])
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String [PriceResponse]
-> IO (Either String [PriceResponse]))
-> Either String [PriceResponse]
-> IO (Either String [PriceResponse])
forall a b. (a -> b) -> a -> b
$ ByteString -> Either String [PriceResponse]
tryParseAsPrice (ByteString -> Either String [PriceResponse])
-> ByteString -> Either String [PriceResponse]
forall a b. (a -> b) -> a -> b
$ Response ByteString
response Response ByteString
-> Getting ByteString (Response ByteString) ByteString
-> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString (Response ByteString) ByteString
forall body0 body1.
Lens (Response body0) (Response body1) body0 body1
responseBody
fetchLatest :: String -> IO (Either String PriceResponse)
fetchLatest :: String -> IO (Either String PriceResponse)
fetchLatest String
ticker = do
Response ByteString
response <- String -> IO (Response ByteString)
get (String -> IO (Response ByteString))
-> (String -> String) -> String -> IO (Response ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. YahooRequest -> String
requestUrl (YahooRequest -> String)
-> (String -> YahooRequest) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> YahooRequest
request (String -> IO (Response ByteString))
-> String -> IO (Response ByteString)
forall a b. (a -> b) -> a -> b
$ String
ticker
Either String PriceResponse -> IO (Either String PriceResponse)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String PriceResponse -> IO (Either String PriceResponse))
-> Either String PriceResponse -> IO (Either String PriceResponse)
forall a b. (a -> b) -> a -> b
$ ([PriceResponse] -> PriceResponse)
-> Either String [PriceResponse] -> Either String PriceResponse
forall t b a. (t -> b) -> Either a t -> Either a b
right [PriceResponse] -> PriceResponse
forall a. [a] -> a
head (Either String [PriceResponse] -> Either String PriceResponse)
-> (ByteString -> Either String [PriceResponse])
-> ByteString
-> Either String PriceResponse
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either String [PriceResponse]
tryParseAsPrice (ByteString -> Either String PriceResponse)
-> ByteString -> Either String PriceResponse
forall a b. (a -> b) -> a -> b
$ Response ByteString
response Response ByteString
-> Getting ByteString (Response ByteString) ByteString
-> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString (Response ByteString) ByteString
forall body0 body1.
Lens (Response body0) (Response body1) body0 body1
responseBody
request :: String -> YahooRequest
request :: String -> YahooRequest
request String
t = YahooRequest :: Ticker -> Maybe Interval -> Maybe TimeRange -> YahooRequest
YahooRequest {
ticker :: Ticker
ticker = String -> Ticker
Ticker String
t,
interval :: Maybe Interval
interval = Maybe Interval
forall a. Maybe a
Nothing,
period :: Maybe TimeRange
period = Maybe TimeRange
forall a. Maybe a
Nothing
}
withDaily :: YahooRequest -> YahooRequest
withDaily :: YahooRequest -> YahooRequest
withDaily (YahooRequest {ticker :: YahooRequest -> Ticker
ticker = Ticker
t, period :: YahooRequest -> Maybe TimeRange
period = Maybe TimeRange
p}) = YahooRequest :: Ticker -> Maybe Interval -> Maybe TimeRange -> YahooRequest
YahooRequest {
ticker :: Ticker
ticker = Ticker
t,
interval :: Maybe Interval
interval = Interval -> Maybe Interval
forall a. a -> Maybe a
Just Interval
Daily,
period :: Maybe TimeRange
period = Maybe TimeRange
p
}
withWeekly :: YahooRequest -> YahooRequest
withWeekly :: YahooRequest -> YahooRequest
withWeekly (YahooRequest {ticker :: YahooRequest -> Ticker
ticker = Ticker
t, period :: YahooRequest -> Maybe TimeRange
period = Maybe TimeRange
p}) = YahooRequest :: Ticker -> Maybe Interval -> Maybe TimeRange -> YahooRequest
YahooRequest {
ticker :: Ticker
ticker = Ticker
t,
interval :: Maybe Interval
interval = Interval -> Maybe Interval
forall a. a -> Maybe a
Just Interval
Weekly,
period :: Maybe TimeRange
period = Maybe TimeRange
p
}
after :: Day -> YahooRequest -> YahooRequest
after :: Day -> YahooRequest -> YahooRequest
after Day
day (YahooRequest {ticker :: YahooRequest -> Ticker
ticker = Ticker
t, interval :: YahooRequest -> Maybe Interval
interval = Maybe Interval
i}) = YahooRequest :: Ticker -> Maybe Interval -> Maybe TimeRange -> YahooRequest
YahooRequest {
ticker :: Ticker
ticker = Ticker
t,
interval :: Maybe Interval
interval = Maybe Interval
i,
period :: Maybe TimeRange
period = TimeRange -> Maybe TimeRange
forall a. a -> Maybe a
Just (TimeRange -> Maybe TimeRange) -> TimeRange -> Maybe TimeRange
forall a b. (a -> b) -> a -> b
$ Day -> TimeRange
After Day
day
}
before :: Day -> YahooRequest -> YahooRequest
before :: Day -> YahooRequest -> YahooRequest
before Day
day (YahooRequest {ticker :: YahooRequest -> Ticker
ticker = Ticker
t, interval :: YahooRequest -> Maybe Interval
interval = Maybe Interval
i}) = YahooRequest :: Ticker -> Maybe Interval -> Maybe TimeRange -> YahooRequest
YahooRequest {
ticker :: Ticker
ticker = Ticker
t,
interval :: Maybe Interval
interval = Maybe Interval
i,
period :: Maybe TimeRange
period = TimeRange -> Maybe TimeRange
forall a. a -> Maybe a
Just (TimeRange -> Maybe TimeRange) -> TimeRange -> Maybe TimeRange
forall a b. (a -> b) -> a -> b
$ Day -> TimeRange
Before Day
day
}
between :: (Day, Day) -> YahooRequest -> YahooRequest
between :: (Day, Day) -> YahooRequest -> YahooRequest
between (Day
from, Day
to) (YahooRequest {ticker :: YahooRequest -> Ticker
ticker = Ticker
t, interval :: YahooRequest -> Maybe Interval
interval = Maybe Interval
i}) = YahooRequest :: Ticker -> Maybe Interval -> Maybe TimeRange -> YahooRequest
YahooRequest {
ticker :: Ticker
ticker = Ticker
t,
interval :: Maybe Interval
interval = Maybe Interval
i,
period :: Maybe TimeRange
period = TimeRange -> Maybe TimeRange
forall a. a -> Maybe a
Just (TimeRange -> Maybe TimeRange) -> TimeRange -> Maybe TimeRange
forall a b. (a -> b) -> a -> b
$ Day -> Day -> TimeRange
Range Day
from Day
to
}
day :: Integer -> Int -> Int -> Day
day :: Integer -> Int -> Int -> Day
day = Integer -> Int -> Int -> Day
fromGregorian