-
Notifications
You must be signed in to change notification settings - Fork 194
Add traffic endpoints. #380
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
Open
ajmcmiddlin
wants to merge
18
commits into
haskell-github:master
Choose a base branch
from
qfpl:traffic
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
63b9a3e
Sketch data types for traffic.
ajmcmiddlin 018c8bd
Start traffic endpoint.
ajmcmiddlin cb0ba83
Fix name collisions and export Data.Traffic.
ajmcmiddlin 2084970
Add Traffic modules to exports in cabal file.
ajmcmiddlin 5910d12
Get popularReferrers working.
ajmcmiddlin 086137d
Get popularPaths working.
ajmcmiddlin 3660d6e
Get clones working.
ajmcmiddlin 8a34263
s/Path/PopularPath/ to avoid collisions.
ajmcmiddlin 78f8012
Remove functions that don't take auth.
ajmcmiddlin 199f13d
Add haddocks and format Endpoints/Repos/Traffic.hs.
ajmcmiddlin 029c352
Remove type-level tracking of periods + formatting.
ajmcmiddlin 10ceda7
Add traffic functions to top level module.
ajmcmiddlin bb3a6b1
Final formatting on Data/Traffic.
ajmcmiddlin ad1e7d3
Line lengths in Endpoints/Repos/Traffic.
ajmcmiddlin 90930b5
Format Data.Traffic per feedback.
ajmcmiddlin c70f2b8
Move period serialization to Endpoint module.
ajmcmiddlin 509ed8a
Remove use of LambdaCase.
ajmcmiddlin 6e036e9
Add ToJSON instances for Traffic types.
ajmcmiddlin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
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" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.