module LendingClub.Internal.JSON
( jsonGet
, investRequest
) where
import Blaze.ByteString.Builder (fromLazyByteString)
import Network.Http.Client
import OpenSSL
import System.IO.Streams (InputStream, write)
import Control.Monad.Reader (liftIO)
import Data.Aeson (FromJSON, ToJSON, encode)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as C
import Data.Monoid ((<>))
import GHC.Generics
import LendingClub.Authorization
import LendingClub.Invest
import LendingClub.Money
jsonGet
:: FromJSON a
=> Authorization
-> ByteString
-> IO a
jsonGet auth url = issueRequest auth url GET "application/json" jsonHandler (Nothing :: Maybe ())
data InvestRequest = InvestRequest
{ aid :: !Int
, orders :: ![Order]
} deriving (Generic, Show)
data Order = Order
{ loanId :: !Int
, requestedAmount :: !Money
} deriving (Generic, Show)
instance ToJSON Order where
instance ToJSON InvestRequest where
investRequest
:: Authorization
-> InvestorId
-> Int
-> Money
-> IO InvestResponse
investRequest auth (InvestorId invId) listingId amount =
issueRequest auth ("accounts/" <> C.pack (show invId) <> "/orders") POST "application/json" jsonHandler $
Just InvestRequest
{ aid = invId
, orders = [Order listingId amount]
}
apiUrl :: ByteString
apiUrl = "api.lendingclub.com"
issueRequest
:: (FromJSON a, ToJSON b)
=> Authorization
-> ByteString
-> Method
-> ContentType
-> (Response -> InputStream ByteString -> IO a)
-> Maybe b
-> IO a
issueRequest (Authorization auth) url method ct handler mBody = withOpenSSL $ do
ctx <- baselineContextSSL
con <- openConnectionSSL ctx apiUrl 443
req <- buildRequest $ do
http method $ "/api/investor/v1/" <> url
setHeader "Authorization" auth
setContentType ct
sendRequest con req (setBody mBody)
resp <- receiveResponse con handler
closeConnection con
return resp
where
setBody Nothing = emptyBody
setBody (Just body) = write (Just (buildJSON body))
buildJSON = fromLazyByteString . encode