|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | + |
| 3 | +module ExtraLife.API ( userInfo, recentDonations, teamInfo, teamMembers ) where |
| 4 | + |
| 5 | +import Network.HTTP.Client as HC |
| 6 | +import Network.HTTP.Client.TLS as TLS |
| 7 | +import Data.Aeson as Aeson |
| 8 | +import Data.ByteString.Lazy (ByteString) |
| 9 | +import Prelude |
| 10 | + |
| 11 | +import ExtraLife.User (User) |
| 12 | +import ExtraLife.Donation (Donation) |
| 13 | +import ExtraLife.Team (Team) |
| 14 | +import ExtraLife.TeamMember (TeamMember) |
| 15 | + |
| 16 | +elRoot :: String |
| 17 | +elRoot = "https://www.extra-life.org/index.cfm?fuseaction=donorDrive." |
| 18 | + |
| 19 | +fetchFor :: Request -> IO ByteString |
| 20 | +fetchFor req = do |
| 21 | + let settings = HC.managerSetProxy (proxyEnvironment Nothing) tlsManagerSettings |
| 22 | + manager <- HC.newManager settings |
| 23 | + response <- httpLbs req manager |
| 24 | + return $ responseBody response |
| 25 | + |
| 26 | +userInfoRaw :: Int -> String |
| 27 | +userInfoRaw = (((elRoot ++ "participant&participantID=") ++) . (++ "&format=json")) . show |
| 28 | + |
| 29 | +userInfo' :: Int -> Request |
| 30 | +userInfo' = parseRequest_ . userInfoRaw |
| 31 | + |
| 32 | +teamInfoRaw :: Int -> String |
| 33 | +teamInfoRaw = (((elRoot ++ "team&teamID=") ++) . (++ "&format=json")) . show |
| 34 | + |
| 35 | +teamInfo' :: Int -> Request |
| 36 | +teamInfo' = parseRequest_ . teamInfoRaw |
| 37 | + |
| 38 | +recentDonationsRaw :: Int -> String |
| 39 | +recentDonationsRaw = (((elRoot ++ "participantDonations&participantID=") ++) . (++ "&format=json")) . show |
| 40 | + |
| 41 | +recentDonations' :: Int -> Request |
| 42 | +recentDonations' = parseRequest_ . recentDonationsRaw |
| 43 | + |
| 44 | +teamMembersRaw :: Int -> String |
| 45 | +teamMembersRaw = (((elRoot ++ "teamParticipants&teamID=") ++) . (++ "&format=json")) . show |
| 46 | + |
| 47 | +teamMembers' :: Int -> Request |
| 48 | +teamMembers' = parseRequest_ . teamMembersRaw |
| 49 | + |
| 50 | +userInfo :: Int -> IO (Maybe User) |
| 51 | +userInfo u = do |
| 52 | + user <- fetchFor $ userInfo' u |
| 53 | + return (Aeson.decode user :: Maybe User) |
| 54 | + |
| 55 | +recentDonations :: Int -> IO (Maybe [Donation]) |
| 56 | +recentDonations u = do |
| 57 | + user <- fetchFor $ recentDonations' u |
| 58 | + return (Aeson.decode user :: Maybe [Donation]) |
| 59 | + |
| 60 | +teamInfo :: Int -> IO (Maybe Team) |
| 61 | +teamInfo t = do |
| 62 | + team <- fetchFor $ teamInfo' t |
| 63 | + return (Aeson.decode team :: Maybe Team) |
| 64 | + |
| 65 | +teamMembers :: Int -> IO (Maybe [TeamMember]) |
| 66 | +teamMembers t = do |
| 67 | + team <- fetchFor $ teamMembers' t |
| 68 | + return (Aeson.decode team :: Maybe [TeamMember]) |
0 commit comments