Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add traffic endpoints. #380

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions github.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ library
GitHub.Data.Search
GitHub.Data.Statuses
GitHub.Data.Teams
GitHub.Data.Traffic
GitHub.Data.URL
GitHub.Data.Webhooks
GitHub.Data.Webhooks.Validate
Expand Down Expand Up @@ -131,6 +132,7 @@ library
GitHub.Endpoints.Repos.Forks
GitHub.Endpoints.Repos.Releases
GitHub.Endpoints.Repos.Statuses
GitHub.Endpoints.Repos.Traffic
GitHub.Endpoints.Repos.Webhooks
GitHub.Endpoints.Search
GitHub.Endpoints.Users
Expand Down
8 changes: 8 additions & 0 deletions src/GitHub.hs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,13 @@ module GitHub (
pingRepoWebhookR,
deleteRepoWebhookR,

-- ** Traffic
-- | See <https://developer.github.com/v3/repos/traffic/>
popularReferrersR,
popularPathsR,
viewsR,
clonesR,

-- * Releases
releasesR,
releaseR,
Expand Down Expand Up @@ -410,6 +417,7 @@ import GitHub.Endpoints.Repos.Deployments
import GitHub.Endpoints.Repos.Forks
import GitHub.Endpoints.Repos.Releases
import GitHub.Endpoints.Repos.Statuses
import GitHub.Endpoints.Repos.Traffic
import GitHub.Endpoints.Repos.Webhooks
import GitHub.Endpoints.Search
import GitHub.Endpoints.Users
Expand Down
2 changes: 2 additions & 0 deletions src/GitHub/Data.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ module GitHub.Data (
module GitHub.Data.Search,
module GitHub.Data.Statuses,
module GitHub.Data.Teams,
module GitHub.Data.Traffic,
module GitHub.Data.URL,
module GitHub.Data.Webhooks
) where
Expand Down Expand Up @@ -90,6 +91,7 @@ import GitHub.Data.Reviews
import GitHub.Data.Search
import GitHub.Data.Statuses
import GitHub.Data.Teams
import GitHub.Data.Traffic
import GitHub.Data.URL
import GitHub.Data.Webhooks

Expand Down
126 changes: 126 additions & 0 deletions src/GitHub/Data/Traffic.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}

-- | Data types used in the traffic API
module GitHub.Data.Traffic where

import Data.Text (Text)
import Data.Time (UTCTime)
import Data.Vector (Vector)

import GitHub.Data.Name (Name)
import GitHub.Internal.Prelude
import Prelude ()

data Referrer = Referrer
{ referrer :: !(Name Referrer)
, referrerCount :: !Int
, referrerUniques :: !Int
}
deriving (Eq, Show, Generic)

instance FromJSON Referrer where
parseJSON = withObject "Referrer" $ \o -> Referrer
<$> o .: "referrer"
<*> o .: "count"
<*> o .: "uniques"

instance ToJSON Referrer where
toJSON (Referrer r c u) = object
[ "referrer" .= r
, "count" .= c
, "uniques" .= u
]

data PopularPath = PopularPath
{ popularPath :: !Text
, popularPathTitle :: !Text
, popularPathCount :: !Int
, popularPathUniques :: !Int
}
deriving (Eq, Show)

instance FromJSON PopularPath where
parseJSON = withObject "Path" $ \o -> PopularPath
<$> o .: "path"
<*> o .: "title"
<*> o .: "count"
<*> o .: "uniques"

instance ToJSON PopularPath where
toJSON (PopularPath p t c u) = object
[ "path" .= p
, "title" .= t
, "count" .= c
, "uniques" .= u
]

data Period =
Day
| Week
deriving (Eq, Show)

data TrafficEvent
= View
| Clone
deriving (Eq, Show)

data TrafficCount (e :: TrafficEvent) = TrafficCount
{ trafficCountTimestamp :: !UTCTime
, trafficCount :: !Int
, trafficCountUniques :: !Int
}
deriving (Eq, Show)

instance FromJSON (TrafficCount e) where
parseJSON = withObject "TrafficCount" $ \o -> TrafficCount
<$> o .: "timestamp"
<*> o .: "count"
<*> o .: "uniques"

instance ToJSON (TrafficCount e) where
toJSON (TrafficCount t c u) = object
[ "timestamp" .= t
, "count" .= c
, "uniques" .= u
]

data Views = Views
{ viewsCount :: !Int
, viewsUniques :: !Int
, views :: !(Vector (TrafficCount 'View))
}
deriving (Eq, Show)

instance FromJSON Views where
parseJSON = withObject "Views" $ \o -> Views
<$> o .: "count"
<*> o .: "uniques"
<*> o .: "views"

instance ToJSON Views where
toJSON (Views c u v) = object
[ "count" .= c
, "uniques" .= u
, "views" .= v
]

data Clones = Clones
{ clonesCount :: !Int
, clonesUniques :: !Int
, clones :: !(Vector (TrafficCount 'Clone))
}
deriving (Eq, Show)

instance FromJSON Clones where
parseJSON = withObject "Clones" $ \o -> Clones
<$> o .: "count"
<*> o .: "uniques"
<*> o .: "clones"

instance ToJSON Clones where
toJSON (Clones c u cs) = object
[ "count" .= c
, "uniques" .= u
, "clones" .= cs
]
80 changes: 80 additions & 0 deletions src/GitHub/Endpoints/Repos/Traffic.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
-- | The traffic API, as described at <https://developer.github.com/v3/repos/traffic/>
module GitHub.Endpoints.Repos.Traffic (
popularReferrers',
popularReferrersR,
popularPaths',
popularPathsR,
views',
viewsR,
clones',
clonesR
) where

import Data.Vector (Vector)

import GitHub.Data
(Auth, Clones, Error, Name, Owner, Period (Day, Week), PopularPath,
Referrer, Repo, Views)
import GitHub.Data.Request (query, toPathPart)
import GitHub.Internal.Prelude
import GitHub.Request (Request, executeRequest)
import Prelude ()

-- | The top 10 referrers for the past 14 days.
--
-- > popularReferrers' (BasicAuth "github-username" "github-password") "qfpl" "tasty-hedgehog"
ajmcmiddlin marked this conversation as resolved.
Show resolved Hide resolved
popularReferrers' :: Auth -> Name Owner -> Name Repo -> IO (Either Error (Vector Referrer))
popularReferrers' auth user =
executeRequest auth . popularReferrersR user

-- | The top 10 referrers for the past 14 days.
-- See <https://developer.github.com/v3/repos/traffic/#list-referrers>
popularReferrersR :: Name Owner -> Name Repo -> Request k (Vector Referrer)
popularReferrersR user repo =
query ["repos", toPathPart user, toPathPart repo, "traffic", "popular", "referrers"] []

-- | The 10 most popular paths based on visits over the last 14 days.
--
-- > popularPaths' (OAuth "supersecrettoken") "qfpl" "tasty-hedgehog"
popularPaths' :: Auth -> Name Owner -> Name Repo -> IO (Either Error (Vector PopularPath))
popularPaths' auth user =
executeRequest auth . popularPathsR user

-- | The 10 most popular paths based on visits over the last 14 days.
-- See <https://developer.github.com/v3/repos/traffic/#list-paths>
popularPathsR :: Name Owner -> Name Repo -> Request k (Vector PopularPath)
popularPathsR user repo =
query ["repos", toPathPart user, toPathPart repo, "traffic", "popular", "paths"] []

-- | The total number of views over the last 14 days, and a daily or weekly breakdown.
--
-- > views' (OAuth "supersecrettoken") "qfpl" "tasty-hedgehog" Day
views' :: Auth -> Name Owner -> Name Repo -> Period -> IO (Either Error Views)
views' auth user repo =
executeRequest auth . viewsR user repo

-- | The total number of views over the last 14 days, and a daily or weekly breakdown.
-- See <https://developer.github.com/v3/repos/traffic/#views>
viewsR :: Name Owner -> Name Repo -> Period -> Request k Views
viewsR user repo period =
query ["repos", toPathPart user, toPathPart repo, "traffic", "views"]
[("per", Just $ serializePeriod period)]

-- | The total number of clones over the last 14 days, and a daily or weekly breakdown.
--
-- > clones' (OAuth "supersecrettoken") "qfpl" "tasty-hedgehog" Week
clones' :: Auth -> Name Owner -> Name Repo -> Period -> IO (Either Error Clones)
clones' auth user repo =
executeRequest auth . clonesR user repo

-- | The total number of clones over the last 14 days, and a daily or weekly breakdown.
-- See <https://developer.github.com/v3/repos/traffic/#clones>
clonesR :: Name Owner -> Name Repo -> Period -> Request k Clones
clonesR user repo period =
query ["repos", toPathPart user, toPathPart repo, "traffic", "clones"]
[("per", Just $ serializePeriod period)]

serializePeriod :: IsString a => Period -> a
serializePeriod p = case p of
Day -> "day"
Week -> "week"