Skip to content

Commit 6fc6dc7

Browse files
santolucitophadej
authored andcommitted
Add support for RateLimit API call
1 parent 4986ca6 commit 6fc6dc7

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

github.cabal

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Library
6565
GADTs
6666
KindSignatures
6767
StandaloneDeriving
68+
RecordWildCards
6869
exposed-modules:
6970
GitHub
7071
GitHub.Internal.Prelude
@@ -87,6 +88,7 @@ Library
8788
GitHub.Data.Name
8889
GitHub.Data.Options
8990
GitHub.Data.PullRequests
91+
GitHub.Data.RateLimit
9092
GitHub.Data.Releases
9193
GitHub.Data.Repos
9294
GitHub.Data.Request
@@ -117,6 +119,7 @@ Library
117119
GitHub.Endpoints.PullRequests
118120
GitHub.Endpoints.PullRequests.Reviews
119121
GitHub.Endpoints.PullRequests.Comments
122+
GitHub.Endpoints.RateLimit
120123
GitHub.Endpoints.Repos
121124
GitHub.Endpoints.Repos.Collaborators
122125
GitHub.Endpoints.Repos.Comments

src/GitHub.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,11 @@ module GitHub (
344344
createStatusR,
345345
statusesForR,
346346
statusForR,
347+
348+
-- ** Rate Limit
349+
-- | See <https://developer.github.com/v3/rate_limit/>
350+
rateLimit,
351+
rateLimit',
347352

348353
-- * Data definitions
349354
module GitHub.Data,
@@ -372,6 +377,7 @@ import GitHub.Endpoints.Organizations.Teams
372377
import GitHub.Endpoints.PullRequests
373378
import GitHub.Endpoints.PullRequests.Reviews
374379
import GitHub.Endpoints.PullRequests.Comments
380+
import GitHub.Endpoints.RateLimit
375381
import GitHub.Endpoints.Repos
376382
import GitHub.Endpoints.Repos.Collaborators
377383
import GitHub.Endpoints.Repos.Comments

src/GitHub/Data.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module GitHub.Data (
4747
module GitHub.Data.Milestone,
4848
module GitHub.Data.Options,
4949
module GitHub.Data.PullRequests,
50+
module GitHub.Data.RateLimit,
5051
module GitHub.Data.Releases,
5152
module GitHub.Data.Repos,
5253
module GitHub.Data.Request,
@@ -79,6 +80,7 @@ import GitHub.Data.Milestone
7980
import GitHub.Data.Name
8081
import GitHub.Data.Options
8182
import GitHub.Data.PullRequests
83+
import GitHub.Data.RateLimit
8284
import GitHub.Data.Releases
8385
import GitHub.Data.Repos
8486
import GitHub.Data.Request

src/GitHub/Data/RateLimit.hs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{-# LANGUAGE RecordWildCards #-}
2+
3+
-----------------------------------------------------------------------------
4+
-- |
5+
-- License : BSD-3-Clause
6+
-- Maintainer : Oleg Grenrus <oleg.grenrus@iki.fi>
7+
--
8+
module GitHub.Data.RateLimit where
9+
10+
import GitHub.Internal.Prelude
11+
import Prelude ()
12+
13+
14+
data RateLimit = RateLimit
15+
{ coreLimit :: !Int
16+
, coreRemaining :: !Int
17+
, coreReset :: !Int
18+
, searchLimit :: !Int
19+
, searchRemaining :: !Int
20+
, searchReset :: !Int
21+
}
22+
deriving (Show, Data, Typeable, Eq, Ord, Generic)
23+
24+
instance NFData RateLimit where rnf = genericRnf
25+
instance Binary RateLimit
26+
27+
instance FromJSON RateLimit where
28+
parseJSON = withObject "RateLimit" $ \o -> do
29+
resources <- o .: "resources"
30+
core <- resources .: "core"
31+
coreLimit <- core .: "limit"
32+
coreRemaining <- core .: "remaining"
33+
coreReset <- core .: "reset"
34+
35+
search <- resources .: "search"
36+
searchLimit <- search .: "limit"
37+
searchRemaining <- search .: "remaining"
38+
searchReset <- search .: "reset"
39+
40+
return RateLimit{..}

src/GitHub/Endpoints/RateLimit.hs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
-----------------------------------------------------------------------------
2+
-- |
3+
-- License : BSD-3-Clause
4+
-- Maintainer : Oleg Grenrus <oleg.grenrus@iki.fi>
5+
--
6+
-- The Github RateLimit API, as described at
7+
-- <http://developer.github.com/v3/rate_limit/>.
8+
module GitHub.Endpoints.RateLimit(
9+
rateLimit',
10+
rateLimit,
11+
module GitHub.Data,
12+
) where
13+
14+
import GitHub.Data
15+
import GitHub.Internal.Prelude
16+
import GitHub.Request
17+
import Prelude ()
18+
19+
import qualified Data.Text.Encoding as TE
20+
21+
-- | Get your current rate limit status (Note: Accessing this endpoint does not count against your rate limit.)
22+
-- With authentication.
23+
rateLimit' :: Maybe Auth -> IO (Either Error RateLimit)
24+
rateLimit' auth = executeRequestMaybe auth rateLimitR
25+
26+
-- | Get your current rate limit status (Note: Accessing this endpoint does not count against your rate limit.)
27+
-- Without authentication.
28+
--
29+
-- > searchRepos "q=a in%3Aname language%3Ahaskell created%3A>2013-10-01&per_page=100"
30+
rateLimit :: IO (Either Error RateLimit)
31+
rateLimit = rateLimit' Nothing
32+
33+
rateLimitR :: Request k RateLimit
34+
rateLimitR = query ["rate_limit"] []

0 commit comments

Comments
 (0)