module Prosper.Commands
( invest
, account
, allListings
, notes
, listingFromNote
, listingCSV
, AccountException (..)
) where
import Control.Exception (Exception, throwIO)
import Control.Monad (when)
import Data.ByteString.Char8 as C
import Data.Maybe (listToMaybe)
import Data.Monoid ((<>))
import Data.Typeable (Typeable)
import Data.Vector (Vector)
import Network.Http.Client
import System.IO.Streams (InputStream)
import Prosper.Account
import Prosper.Internal.JSON
import Prosper.Internal.CSV
import Prosper.Invest
import Prosper.Listing
import Prosper.Money
import Prosper.Note
import Prosper.User
invest :: User -> Money -> Listing -> IO InvestResponse
invest user amt l = investRequest user (listingId l) amt
account :: User -> IO Account
account user = jsonGetHandler user "Account" accountHandler
where
accountHandler resp is =
if getStatusCode resp == 500
then throwIO (AccountException (getStatusMessage resp))
else jsonHandler resp is
data AccountException = AccountException ByteString
deriving (Typeable, Show)
instance Exception AccountException
notes :: User -> IO (Vector Note)
notes user = jsonGet user "Notes"
listingFromNote :: User -> Note -> IO (Maybe Listing)
listingFromNote user (Note { listingNumber = lid }) = do
ls <- jsonGet user command
return $ listToMaybe ls
where
command = "ListingsHistorical?$filter=ListingNumber eq " <> C.pack (show lid)
allListings :: User -> IO (Vector Listing)
allListings user = jsonGet user "Listings"
listingCSV :: User -> Listing -> (InputStream ByteString -> IO a) -> IO a
listingCSV user l = csvGetStream user url
where
lid = listingId l
url = "Listings?$filter=ListingNumber%20eq%20" <> C.pack (show lid)