Skip to content

Commit

Permalink
Upgrade to OkHttp 4
Browse files Browse the repository at this point in the history
This upgrade was prompted by guardian/prout#114,
where Ash noted that updating to OkHttp 4 was blocked by `play-git-hub`s
use of OkHttp 's internal `HttpDate` class.

Thankfully the `java.time` package offers the `RFC_1123_DATE_TIME`
date time formatter, which looks to be a good substitute for parsing
the `Date` header returned by the GitHub API.

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#RFC_1123_DATE_TIME
  • Loading branch information
rtyley committed Nov 2, 2023
1 parent 32377f8 commit 74b2155
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
6 changes: 3 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

description := "Group of library code for Play, Git, and GitHub"

ThisBuild / scalaVersion := "2.13.10"
ThisBuild / scalaVersion := "2.13.12"

ThisBuild / organization := "com.madgag.play-git-hub"

Expand All @@ -12,8 +12,8 @@ lazy val core = (project in file("core")).settings(
libraryDependencies ++= Seq(
"com.madgag" %% "rate-limit-status" % "0.7",
"com.typesafe.play" %% "play" % "2.8.19",
"com.squareup.okhttp3" % "okhttp" % "3.14.9",
"com.lihaoyi" %% "fastparse" % "2.3.3",
"com.squareup.okhttp3" % "okhttp" % "4.10.0",
"com.lihaoyi" %% "fastparse" % "3.0.0",
"com.madgag" %% "scala-collection-plus" % "0.11",
"com.madgag.scala-git" %% "scala-git" % "4.6",
scalaGitTest % Test,
Expand Down
10 changes: 6 additions & 4 deletions core/src/main/scala/com/madgag/scalagithub/GitHub.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import akka.NotUsed
import akka.stream.scaladsl.Source

import java.time.Duration.ofHours
import java.time.Instant
import java.time.{Instant, ZonedDateTime}
import java.util.concurrent.TimeUnit.SECONDS
import com.madgag.okhttpscala._
import com.madgag.ratelimitstatus.{QuotaUpdate, RateLimit}
Expand All @@ -29,7 +29,6 @@ import com.madgag.scalagithub.commands._
import com.madgag.scalagithub.model._
import okhttp3.Request.Builder
import okhttp3._
import okhttp3.internal.http.HttpDate
import play.api.Logger
import play.api.http.Status
import play.api.http.Status._
Expand All @@ -39,7 +38,10 @@ import play.api.libs.json._
import scala.jdk.CollectionConverters._
import scala.concurrent.{Future, ExecutionContext => EC}
import scala.language.implicitConversions
import fastparse._, NoWhitespace._
import fastparse._
import NoWhitespace._

import java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME

case class Quota(
consumed: Int,
Expand Down Expand Up @@ -73,7 +75,7 @@ object ResponseMeta {
remaining = remaining.toInt,
limit = limit.toInt,
reset = Instant.ofEpochSecond(reset.toLong),
capturedAt = HttpDate.parse(date).toInstant
capturedAt = ZonedDateTime.parse(date, RFC_1123_DATE_TIME).toInstant
))

def rateLimitFrom(response: Response): Quota = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,27 @@
package com.madgag.scalagithub

import okhttp3.Headers
import org.scalatest.OptionValues
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class ResponseMetaTest extends AnyFlatSpec with Matchers {
import java.time.Instant

class ResponseMetaTest extends AnyFlatSpec with Matchers with OptionValues {
it should "not crash if ratelimit state response headers are missing" in {
ResponseMeta.rateLimitStatusFrom(
Headers.of("nothing-useful", "whatever")
) shouldBe None
}

it should "parse the Date header from a GitHub request" in {
ResponseMeta.rateLimitStatusFrom(
new Headers.Builder()
.add("date", "Thu, 02 Nov 2023 12:37:22 GMT")
.add("x-ratelimit-remaining", "58")
.add("x-ratelimit-limit", "60")
.add("x-ratelimit-reset", "1698932232")
.build()
).value.quotaUpdate.capturedAt shouldBe Instant.parse("2023-11-02T12:37:22Z")
}
}
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.8.2
sbt.version=1.9.7

0 comments on commit 74b2155

Please sign in to comment.