Skip to content

Commit

Permalink
Fix unused import and try handling of brightcove client
Browse files Browse the repository at this point in the history
  • Loading branch information
ekrojo77 committed Jan 6, 2025
1 parent 628be06 commit 6c355db
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
package no.ndla.audioapi.controller

import no.ndla.audioapi.Props
import no.ndla.audioapi.model.api.{JobAlreadyFoundException, TranscriptionResultDTO}
import no.ndla.audioapi.model.api.TranscriptionResultDTO
import no.ndla.audioapi.service.{ReadService, TranscriptionService}
import no.ndla.network.tapir.NoNullJsonPrinter.jsonBody
import no.ndla.network.tapir.TapirController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import io.circe.parser.*
import sttp.client3.{HttpClientSyncBackend, UriContext, basicRequest}
import no.ndla.common.configuration.HasBaseProps

import scala.util.{Failure, Success, Try}

case class TokenResponse(access_token: String, token_type: String, expires_in: Int)

trait NdlaBrightcoveClient {
Expand All @@ -23,24 +25,25 @@ trait NdlaBrightcoveClient {
class NdlaBrightcoveClient {
private val backend = HttpClientSyncBackend()

def getToken(clientID: String, clientSecret: String): Either[String, String] = {
def getToken(clientID: String, clientSecret: String): Try[String] = {
val request =
basicRequest.auth
.basic(clientID, clientSecret)
.post(uri"${props.BrightCoveAuthUri}?grant_type=client_credentials")
val authResponse = request.send(backend)

authResponse.body match {
case Right(jsonString) =>
decode[TokenResponse](jsonString) match {
case Right(tokenResponse) => Right(tokenResponse.access_token)
case Left(error) => Left(s"Failed to decode token response: ${error.getMessage}")
}
case Left(error) => Left(s"Failed to get token: ${error}")
Try {
authResponse.body match {
case Right(jsonString) =>
decode[TokenResponse](jsonString) match {
case Right(tokenResponse) => tokenResponse.access_token
case Left(error) => throw new Exception(s"Failed to decode token response: ${error.getMessage}")
}
case Left(error) => throw new Exception(s"Failed to get token: ${error}")
}
}
}

def getVideoSource(accountId: String, videoId: String, bearerToken: String): Either[String, Vector[Json]] = {
def getVideoSource(accountId: String, videoId: String, bearerToken: String): Try[Vector[Json]] = {

val videoSourceUrl = props.BrightCoveVideoUri(accountId, videoId)
val request = basicRequest
Expand All @@ -50,18 +53,19 @@ trait NdlaBrightcoveClient {
implicit val backend = HttpClientSyncBackend()

val response = request.send(backend)

response.body match {
case Right(jsonString) =>
parse(jsonString) match {
case Right(json) =>
json.asArray match {
case Some(videoSources) => Right(videoSources)
case None => Left("Expected a JSON array but got something else.")
}
case Left(error) => Left(s"Failed to decode video source response: ${error.getMessage}")
}
case Left(error) => Left(s"Failed to get video source: ${error}")
Try {
response.body match {
case Right(jsonString) =>
parse(jsonString) match {
case Right(json) =>
json.asArray match {
case Some(videoSources) => videoSources
case None => throw new Exception("Failed to parse video source")
}
case Left(error) => throw new Exception(s"Failed to parse video source: ${error.getMessage}")
}
case Left(error) => throw new Exception(s"Failed to get video source: ${error}")
}
}
}
}
Expand Down

0 comments on commit 6c355db

Please sign in to comment.