-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from ChristopherDavenport/rateLimitEndpoint
Rate Limit Endpoint
- Loading branch information
Showing
4 changed files
with
141 additions
and
7 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
core/src/main/scala/io/chrisdavenport/github/data/RateLimit.scala
This file contains 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,44 @@ | ||
package io.chrisdavenport.github.data | ||
|
||
import cats.implicits._ | ||
import io.circe.Decoder | ||
import io.circe.HCursor | ||
|
||
object RateLimit { | ||
final case class Limits( | ||
max: Int, | ||
remaining: Int, | ||
reset: Int | ||
) | ||
object Limits { | ||
implicit val decoder = new Decoder[Limits]{ | ||
def apply(c: HCursor): Decoder.Result[Limits] = | ||
( | ||
c.downField("limit").as[Int], | ||
c.downField("remaining").as[Int], | ||
c.downField("reset").as[Int] | ||
).mapN(Limits.apply) | ||
} | ||
} | ||
|
||
final case class RateLimit( | ||
core: Limits, | ||
search: Limits, | ||
graphQL: Limits, | ||
integrationManifest: Limits | ||
) | ||
object RateLimit { | ||
implicit val decoder = new Decoder[RateLimit]{ | ||
def apply(c: HCursor): Decoder.Result[RateLimit] = { | ||
val base = c.downField("resources") | ||
( | ||
base.downField("core").as[Limits], | ||
base.downField("search").as[Limits], | ||
base.downField("graphql").as[Limits], | ||
base.downField("integration_manifest").as[Limits] | ||
).mapN(RateLimit.apply) | ||
} | ||
} | ||
|
||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
core/src/main/scala/io/chrisdavenport/github/endpoints/miscellaneous/RateLimit.scala
This file contains 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,26 @@ | ||
package io.chrisdavenport.github.endpoints.miscellaneous | ||
|
||
|
||
import cats.effect._ | ||
import org.http4s._ | ||
import org.http4s.implicits._ | ||
|
||
import io.chrisdavenport.github.data.{RateLimit => DRateLimit} | ||
import io.chrisdavenport.github.Auth | ||
import io.chrisdavenport.github.internals.GithubMedia._ | ||
import io.chrisdavenport.github.internals.RequestConstructor | ||
|
||
object RateLimit { | ||
|
||
/** | ||
* Get your current rate limit status | ||
**/ | ||
def rateLimit[F[_]: Sync]( | ||
auth: Option[Auth] | ||
) = RequestConstructor.runRequestWithNoBody[F, DRateLimit.RateLimit]( | ||
auth, | ||
Method.GET, | ||
uri"rate_limit" | ||
) | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
...src/test/scala/io/chrisdavenport/github/endpoints/miscellaneous.scala/RateLimitSpec.scala
This file contains 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,66 @@ | ||
package io.chrisdavenport.github.endpoints.miscellaneous | ||
|
||
import org.specs2.mutable.Specification | ||
|
||
import cats.effect._ | ||
import cats.effect.specs2.CatsEffect | ||
|
||
|
||
import io.circe.literal._ | ||
import org.http4s._ | ||
import org.http4s.implicits._ | ||
import org.http4s.client._ | ||
import org.http4s.circe._ | ||
import org.http4s.dsl.io._ | ||
|
||
class RateLimitSpec extends Specification with CatsEffect { | ||
|
||
"RateLimit" should { | ||
|
||
"return a valid rate-limit response" in { | ||
RateLimit.rateLimit[IO](None) | ||
.run(Client.fromHttpApp(rateLimit.orNotFound)) | ||
.attempt | ||
.map(_ must beRight) | ||
} | ||
|
||
} | ||
|
||
val rateLimit : HttpRoutes[IO] = HttpRoutes.of { | ||
case GET -> Root / "rate_limit" => | ||
Ok( | ||
json""" | ||
{ | ||
"resources": { | ||
"core": { | ||
"limit": 5000, | ||
"remaining": 4999, | ||
"reset": 1372700873 | ||
}, | ||
"search": { | ||
"limit": 30, | ||
"remaining": 18, | ||
"reset": 1372697452 | ||
}, | ||
"graphql": { | ||
"limit": 5000, | ||
"remaining": 4993, | ||
"reset": 1372700389 | ||
}, | ||
"integration_manifest": { | ||
"limit": 5000, | ||
"remaining": 4999, | ||
"reset": 1551806725 | ||
} | ||
}, | ||
"rate": { | ||
"limit": 5000, | ||
"remaining": 4999, | ||
"reset": 1372700873 | ||
} | ||
} | ||
""" | ||
) | ||
} | ||
|
||
} |
This file contains 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