Skip to content

Commit

Permalink
Merge pull request #128 from hmrc/BAU-requestheader
Browse files Browse the repository at this point in the history
Replace Request[?] with RequestHeader
  • Loading branch information
colin-lamed authored Jan 10, 2025
2 parents ec51d4f + 1717662 commit a9b025c
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 19 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ In the exceptional case that a TTL Index is not required, this can be indicated

## Changes

### Version 2.4.0
- SessionCache only requires `RequestHeader` rather than `Request`.

### Version 2.3.0
- Only logging relevant parts of mongo uri

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package uk.gov.hmrc.mongo.cache

import play.api.mvc.Request
import play.api.mvc.RequestHeader

trait CacheIdType[CacheId] {
trait CacheIdType[-CacheId] {
def run: CacheId => String
}

Expand All @@ -27,17 +27,17 @@ object CacheIdType {
override def run: String => String = identity
}

object SessionCacheId extends CacheIdType[Request[Any]] {
override def run: Request[Any] => String =
object SessionCacheId extends CacheIdType[RequestHeader] {
override def run: RequestHeader => String =
_.session
.get("sessionId")
.getOrElse(throw NoSessionException)

case object NoSessionException extends Exception("Could not find sessionId")
}

case class SessionUuid(sessionIdKey: String) extends CacheIdType[Request[Any]] {
override def run: Request[Any] => String =
case class SessionUuid(sessionIdKey: String) extends CacheIdType[RequestHeader] {
override def run: RequestHeader => String =
_.session
.get(sessionIdKey)
.getOrElse(java.util.UUID.randomUUID.toString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package uk.gov.hmrc.mongo.cache

import org.bson.codecs.Codec
import org.mongodb.scala.{WriteConcern, ObservableFuture, SingleObservableFuture}
import org.mongodb.scala.{WriteConcern, SingleObservableFuture}
import org.mongodb.scala.model.{Filters, FindOneAndUpdateOptions, IndexModel, IndexOptions, Indexes, ReturnDocument, Updates}
import play.api.libs.functional.syntax._
import play.api.libs.json.{Format, JsObject, JsPath, JsResultException, Reads, Writes, __}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package uk.gov.hmrc.mongo.cache
import org.bson.codecs.Codec
import org.mongodb.scala.model.IndexModel
import play.api.libs.json.{Reads, Writes}
import play.api.mvc.Request
import play.api.mvc.RequestHeader
import uk.gov.hmrc.mongo.{MongoComponent, MongoDatabaseCollection, TimestampSupport}

import scala.concurrent.{ExecutionContext, Future}
Expand All @@ -38,7 +38,7 @@ class SessionCacheRepository(
)(implicit
ec: ExecutionContext
) extends MongoDatabaseCollection {
val cacheRepo = new MongoCacheRepository[Request[Any]](
val cacheRepo = new MongoCacheRepository[RequestHeader](
mongoComponent = mongoComponent,
collectionName = collectionName,
replaceIndexes = replaceIndexes,
Expand All @@ -58,14 +58,14 @@ class SessionCacheRepository(
def putSession[T: Writes](
dataKey: DataKey[T],
data: T
)(implicit request: Request[Any]): Future[(String, String)] =
)(implicit request: RequestHeader): Future[(String, String)] =
cacheRepo
.put[T](request)(dataKey, data)
.map(res => sessionIdKey -> res.id)

def getFromSession[T: Reads](dataKey: DataKey[T])(implicit request: Request[Any]): Future[Option[T]] =
def getFromSession[T: Reads](dataKey: DataKey[T])(implicit request: RequestHeader): Future[Option[T]] =
cacheRepo.get[T](request)(dataKey)

def deleteFromSession[T](dataKey: DataKey[T])(implicit request: Request[Any]): Future[Unit] =
def deleteFromSession[T](dataKey: DataKey[T])(implicit request: RequestHeader): Future[Unit] =
cacheRepo.delete(request)(dataKey)
}
4 changes: 2 additions & 2 deletions hmrc-mongo-test-play-30/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class UserRepositorySpec
with ScalaFutures {

override protected lazy val optSchema: Option[BsonDocument] =
BsonDocument("""
Some(BsonDocument("""
{ bsonType: "object"
, required: [ "_id", "created", "name" ]
, properties:
Expand All @@ -113,7 +113,7 @@ class UserRepositorySpec
, name : { bsonType: "string" }
}
}
""")
"""))
}
```

Expand Down
8 changes: 4 additions & 4 deletions project/LibDependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object LibDependencies {
)

def mongoCommon(scalaVersion: String): Seq[ModuleID] = Seq(
"org.mongodb.scala" %% "mongo-scala-driver" % "5.1.2" cross CrossVersion.for3Use2_13,
"org.mongodb.scala" %% "mongo-scala-driver" % "5.2.1" cross CrossVersion.for3Use2_13,
"org.slf4j" % "slf4j-api" % "1.7.30"
) ++ test(scalaVersion)

Expand All @@ -21,7 +21,7 @@ object LibDependencies {
def hmrcMongoPlay(playSuffix: String, scalaVersion: String): Seq[ModuleID] = Seq(
playOrg(playSuffix) %% "play" % playVersion(playSuffix),
playOrg(playSuffix) %% "play-guice" % playVersion(playSuffix),
"uk.gov.hmrc" %% s"crypto-json-$playSuffix" % "8.0.0" % Test
"uk.gov.hmrc" %% s"crypto-json-$playSuffix" % "8.1.0" % Test
) ++ test(scalaVersion)

def hmrcMongoTestPlay(playSuffix: String, scalaVersion: String): Seq[ModuleID] = Seq(
Expand All @@ -40,8 +40,8 @@ object LibDependencies {
private def playVersion(playSuffix: String) =
playSuffix match {
case "play-28" => "2.8.21"
case "play-29" => "2.9.3"
case "play-30" => "3.0.3"
case "play-29" => "2.9.6"
case "play-30" => "3.0.6"
}

private def playOrg(playSuffix: String) =
Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resolvers += MavenRepository("HMRC-open-artefacts-maven2", "https://open.artefacts.tax.service.gov.uk/maven2")
resolvers += Resolver.url("HMRC-open-artefacts-ivy2", url("https://open.artefacts.tax.service.gov.uk/ivy2"))(Resolver.ivyStylePatterns)

addSbtPlugin("uk.gov.hmrc" % "sbt-auto-build" % "3.22.0")
addSbtPlugin("uk.gov.hmrc" % "sbt-auto-build" % "3.24.0")

0 comments on commit a9b025c

Please sign in to comment.