Skip to content

Commit

Permalink
Merge pull request #37 from ChristopherDavenport/rateLimitEndpoint
Browse files Browse the repository at this point in the history
Rate Limit Endpoint
  • Loading branch information
ChristopherDavenport authored Nov 1, 2019
2 parents cd48715 + 27b7de4 commit 57716a1
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 7 deletions.
44 changes: 44 additions & 0 deletions core/src/main/scala/io/chrisdavenport/github/data/RateLimit.scala
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)
}
}

}
}
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"
)

}
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
}
}
"""
)
}

}
12 changes: 5 additions & 7 deletions example/src/main/scala/io/chrisdavenport/github/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ object Main extends IOApp {

auth = OAuth(authLine)

// out <- Resource.liftF(
// Users.getAllUsers[IO](None, None)
// .run(c)
// .evalTap(s => IO(println(s)))
// .compile
// .drain
// )
_ <- Resource.liftF(
endpoints.miscellaneous.RateLimit.rateLimit[IO](auth.some)
.run(c)
.flatTap(a => IO(println(a)))
)
// out <- liftPrint(endpoints.Users.userInfoAuthenticatedUser[IO](auth).run(c))
// out <- liftPrint(endpoints.Users.ownerInfoFor[IO]("http4s", auth.some).run(c))
// _ <- liftPrint(endpoints.Repositories.repository[IO]("http4s", "http4s", auth.some).run(c))
Expand Down

0 comments on commit 57716a1

Please sign in to comment.