Skip to content

Commit f71fc40

Browse files
committed
Initial version
1 parent c4cfe16 commit f71fc40

File tree

7 files changed

+182
-0
lines changed

7 files changed

+182
-0
lines changed

Setup.hs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

extralife.cabal

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: extralife
2+
version: 0.1.0.0
3+
synopsis: API Client for ExtraLife team and user data
4+
description: Provides types and helper functions for fetching data from ExtraLife about donation drives (participants, teams, donations)
5+
homepage: https://github.com/wuest/haskell-extralife-api
6+
license: BSD3
7+
license-file: LICENSE
8+
author: Tina Wuest
9+
maintainer: [email protected]
10+
copyright: (c) 2017 Tina Wuest
11+
category: Web
12+
build-type: Simple
13+
cabal-version: >= 1.10
14+
15+
source-repository head
16+
type: git
17+
location: https://github.com/wuest/haskell-extralife-api.git
18+
19+
library
20+
default-language: Haskell2010
21+
hs-source-dirs: src
22+
ghc-options: -static -Wall -fwarn-implicit-prelude -fwarn-monomorphism-restriction
23+
exposed-modules: ExtraLife.API,
24+
ExtraLife.Donation,
25+
ExtraLife.Team,
26+
ExtraLife.TeamMember,
27+
ExtraLife.User
28+
build-depends:
29+
base >= 4.8 && < 5.0,
30+
http-client >= 0.5 && < 0.6,
31+
http-client-tls >= 0.3 && < 0.4,
32+
aeson >= 1.0 && < 1.1,
33+
bytestring >= 0.10 && < 0.11,
34+
time >= 1.6 && < 1.7,
35+
text >= 1.2 && < 1.3

src/ExtraLife/API.hs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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])

src/ExtraLife/Donation.hs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{-# LANGUAGE DeriveGeneric #-}
2+
3+
module ExtraLife.Donation where
4+
5+
import Data.Aeson as Aeson
6+
import Data.Time as Time
7+
import Data.Text (Text)
8+
import GHC.Generics (Generic)
9+
import Prelude
10+
11+
data Donation = Donation { message :: Maybe Text
12+
, createdOn :: !Time.UTCTime
13+
, donorName :: Maybe Text
14+
, avatarImageURL :: !String
15+
, donationAmount :: Float
16+
} deriving ( Show, Generic )
17+
instance FromJSON Donation
18+
instance ToJSON Donation

src/ExtraLife/Team.hs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{-# LANGUAGE DeriveGeneric #-}
2+
3+
module ExtraLife.Team where
4+
5+
import Data.Aeson as Aeson
6+
import Data.Time as Time
7+
import Data.Text (Text)
8+
import GHC.Generics (Generic)
9+
import Prelude
10+
11+
data Team = Team { totalRaisedAmount :: Float
12+
, fundraisingGoal :: Float
13+
, createdOn :: !Time.UTCTime
14+
, avatarImageURL :: !String
15+
, teamID :: Int
16+
, name :: !Text
17+
} deriving ( Show, Generic )
18+
instance FromJSON Team
19+
instance ToJSON Team

src/ExtraLife/TeamMember.hs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{-# LANGUAGE DeriveGeneric #-}
2+
3+
module ExtraLife.TeamMember where
4+
5+
import Data.Aeson as Aeson
6+
import Data.Time as Time
7+
import Data.Text (Text)
8+
import GHC.Generics (Generic)
9+
import Prelude
10+
11+
data TeamMember =
12+
TeamMember { displayName :: !Text
13+
, participantID :: Int
14+
, createdOn :: !Time.UTCTime
15+
, avatarImageURL :: !String
16+
, isTeamCaptain :: Bool
17+
} deriving ( Show, Generic )
18+
instance FromJSON TeamMember
19+
instance ToJSON TeamMember

src/ExtraLife/User.hs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{-# LANGUAGE DeriveGeneric #-}
2+
3+
module ExtraLife.User where
4+
5+
import Data.Aeson as Aeson
6+
import Data.Time as Time
7+
import Data.Text (Text)
8+
import GHC.Generics (Generic)
9+
import Prelude
10+
11+
data User = User { displayName :: !Text
12+
, totalRaisedAmount :: Float
13+
, fundraisingGoal :: Float
14+
, participantID :: Int
15+
, createdOn :: !Time.UTCTime
16+
, avatarImageURL :: !String
17+
, teamID :: Maybe Int
18+
, isTeamCaptain :: Bool
19+
} deriving ( Show, Generic )
20+
instance FromJSON User
21+
instance ToJSON User

0 commit comments

Comments
 (0)