Skip to content

Commit

Permalink
Extract FcmClient constructor arguments into a case class
Browse files Browse the repository at this point in the history
  • Loading branch information
waisingyiu committed Apr 16, 2024
1 parent 8d86dd7 commit 75dbb73
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import java.time.Instant
import java.util.UUID
import java.util.concurrent.Executors
import scala.concurrent.{ExecutionContext, ExecutionContextExecutor}
import com.gu.notifications.worker.delivery.fcm.FcmFirebase
import scala.util.Try

class AndroidSender(val config: FcmWorkerConfiguration, val firebaseAppName: Option[String], val metricNs: String) extends SenderRequestHandler[FcmClient] {

Expand All @@ -37,8 +39,10 @@ class AndroidSender(val config: FcmWorkerConfiguration, val firebaseAppName: Opt
override implicit val ioContextShift: ContextShift[IO] = IO.contextShift(ec)
override implicit val timer: Timer[IO] = IO.timer(ec)

override val deliveryService: IO[Fcm[IO]] =
FcmClient(config.fcmConfig, firebaseAppName).fold(e => IO.raiseError(e), c => IO.delay(new Fcm(c)))
val fcmFirebase: Try[FcmFirebase] = FcmFirebase(config.fcmConfig, firebaseAppName)
override val deliveryService: IO[Fcm[IO]] =
fcmFirebase.fold(e => IO.raiseError(e), c => IO.delay(new Fcm(FcmClient(c))))

override val maxConcurrency = config.concurrencyForIndividualSend
override val batchConcurrency = 100

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ class FcmClient (firebaseMessaging: FirebaseMessaging, firebaseApp: FirebaseApp,

}

object FcmClient {
def apply(config: FcmConfig, firebaseAppName: Option[String]): Try[FcmClient] =
case class FcmFirebase(firebaseMessaging: FirebaseMessaging, firebaseApp: FirebaseApp, config: FcmConfig, projectId: String, credential: GoogleCredentials, jsonFactory: JsonFactory)

object FcmFirebase {
def apply(config: FcmConfig, firebaseAppName: Option[String]): Try[FcmFirebase] =
Try {
val credential = GoogleCredentials.fromStream(new ByteArrayInputStream(config.serviceAccountKey.getBytes))
val firebaseOptions: FirebaseOptions = FirebaseOptions.builder()
Expand All @@ -193,10 +195,15 @@ object FcmClient {
case s: ServiceAccountCredentials => s.getProjectId()
case _ => ""
}
new FcmClient(FirebaseMessaging.getInstance(firebaseApp), firebaseApp, config, projectId, credential, firebaseOptions.getJsonFactory())
new FcmFirebase(FirebaseMessaging.getInstance(firebaseApp), firebaseApp, config, projectId, credential, firebaseOptions.getJsonFactory())
}
}

object FcmClient {
def apply(firebase: FcmFirebase): FcmClient =
new FcmClient(firebase.firebaseMessaging, firebase.firebaseApp, firebase.config, firebase.projectId, firebase.credential, firebase.jsonFactory)
}

object FirebaseHelpers {

implicit class RichApiFuture[T](val af: ApiFuture[T]) extends AnyVal {
Expand Down

0 comments on commit 75dbb73

Please sign in to comment.