Skip to content

Commit

Permalink
oembed-proxy: Add DTO suffix to all DTO types
Browse files Browse the repository at this point in the history
  • Loading branch information
jnatten committed Dec 10, 2024
1 parent 1d2f4e6 commit f2b04de
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ trait OEmbedProxyController {
.in(query[Option[String]]("maxwidth").description("The maximum width of the embedded resource"))
.in(query[Option[String]]("maxheight").description("The maximum height of the embedded resource"))
.errorOut(errorOutputsFor(400, 401, 403, 404, 410, 422, 502))
.out(jsonBody[OEmbed])
.out(jsonBody[OEmbedDTO])
.serverLogicPure { case (url, maxWidth, maxHeight) =>
oEmbedService.get(url, maxWidth, maxHeight) match {
case Success(oembed) => oembed.asRight
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import sttp.tapir.Schema.annotations.description

// format: off
@description("oEmbed information for an url.")
case class OEmbed(
case class OEmbedDTO(
@description("The resource type") `type`: String,
@description("The oEmbed version number. This must be 1.0.") version: String,
@description("A text title, describing the resource.") title: Option[String],
Expand All @@ -34,7 +34,7 @@ case class OEmbed(
)
// format: on

object OEmbed {
implicit val encoder: Encoder[OEmbed] = deriveEncoder
implicit val decoder: Decoder[OEmbed] = deriveDecoder
object OEmbedDTO {
implicit val encoder: Encoder[OEmbedDTO] = deriveEncoder
implicit val decoder: Decoder[OEmbedDTO] = deriveDecoder
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ case class OEmbedProvider(
providerUrl: String,
endpoints: List[OEmbedEndpoint],
urlParser: String => String = identity,
postProcessor: (String, OEmbed) => OEmbed = (_: String, o: OEmbed) => o
postProcessor: (String, OEmbedDTO) => OEmbedDTO = (_: String, o: OEmbedDTO) => o
) {

def supports(url: String): Boolean = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
package no.ndla.oembedproxy.service

import io.lemonlabs.uri.Url
import no.ndla.oembedproxy.model.OEmbed
import no.ndla.oembedproxy.model.OEmbedDTO
import io.lemonlabs.uri.typesafe.dsl._
import org.jsoup.Jsoup

object OEmbedConverterService {

def addYoutubeTimestampIfdefinedInRequest(requestUrl: String, oembed: OEmbed): OEmbed = {
def addYoutubeTimestampIfdefinedInRequest(requestUrl: String, oembed: OEmbedDTO): OEmbedDTO = {
val paramTypesToTransfer = List("start", "time_continue", "t", "end", "rel")
val queryParamsToTransfer = requestUrl.query.filterNames(pn => paramTypesToTransfer.contains(pn)).params

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package no.ndla.oembedproxy.service
import com.typesafe.scalalogging.StrictLogging
import no.ndla.network.NdlaClient
import no.ndla.network.model.HttpRequestException
import no.ndla.oembedproxy.model.{InvalidUrlException, OEmbed, OEmbedProvider, ProviderNotSupportedException}
import no.ndla.oembedproxy.model.{InvalidUrlException, OEmbedDTO, OEmbedProvider, ProviderNotSupportedException}
import org.log4s.MDC
import sttp.client3.quick.*
import sttp.model.HttpVersion
Expand Down Expand Up @@ -41,9 +41,9 @@ trait OEmbedServiceComponent {
maxWidth: Option[String],
maxHeight: Option[String],
retryCount: Int
): Try[OEmbed] = {
): Try[OEmbedDTO] = {
val uri = uri"${provider.requestUrl(url, maxWidth, maxHeight)}"
ndlaClient.fetch[OEmbed](
ndlaClient.fetch[OEmbedDTO](
quickRequest
.get(uri)
.followRedirects(true)
Expand All @@ -68,7 +68,7 @@ trait OEmbedServiceComponent {
}
}

def get(url: String, maxWidth: Option[String], maxHeight: Option[String]): Try[OEmbed] = {
def get(url: String, maxWidth: Option[String], maxHeight: Option[String]): Try[OEmbedDTO] = {
io.lemonlabs.uri.Uri.parseTry(url) match {
case Failure(_) => Failure(InvalidUrlException(s"$url does not seem to be a valid url."))
case Success(_) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
package no.ndla.oembedproxy.controller

import no.ndla.network.model.HttpRequestException
import no.ndla.oembedproxy.model.OEmbed
import no.ndla.oembedproxy.model.OEmbedDTO
import no.ndla.oembedproxy.{TestEnvironment, UnitSuite}
import no.ndla.tapirtesting.TapirControllerTest
import org.mockito.ArgumentMatchers.{any, anyString}
Expand All @@ -23,7 +23,7 @@ import scala.util.{Failure, Success}
class OEmbedProxyControllerTest extends UnitSuite with TestEnvironment with TapirControllerTest {
val controller: OEmbedProxyController = new OEmbedProxyController

val oembed: OEmbed = OEmbed(
val oembed: OEmbedDTO = OEmbedDTO(
`type` = "rich",
version = "1.0",
title = Some("Title"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

package no.ndla.oembedproxy.service

import no.ndla.oembedproxy.model.OEmbed
import no.ndla.oembedproxy.model.OEmbedDTO
import no.ndla.oembedproxy.{TestEnvironment, UnitSuite}

class OEmbedConverterServiceTest extends UnitSuite with TestEnvironment {
Expand All @@ -22,7 +22,7 @@ class OEmbedConverterServiceTest extends UnitSuite with TestEnvironment {
"https://www.youtube.com/watch?start=43&end=58&v=vZCsuV7Rb_w"
val requestUrlWithtoutTimestamp =
"https://www.youtube.com/watch?v=vZCsuV7Rb_w"
val oembed = OEmbed(
val oembed = OEmbedDTO(
"video",
"1.0",
Some("ESS® expandable sand screen"),
Expand Down Expand Up @@ -75,7 +75,7 @@ class OEmbedConverterServiceTest extends UnitSuite with TestEnvironment {
test("That rel=0 also is added to youtube url if defined in request") {
val requestUrl =
"https://www.youtube.com/watch?v=vZCsuV7Rb_w&rel=0&time_continue=5&meh=1"
val oembed = OEmbed(
val oembed = OEmbedDTO(
"video",
"1.0",
Some("ESS® expandable sand screen"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class OEmbedServiceTest extends UnitSuite with TestEnvironment {
)
)

val OEmbedResponse: OEmbed = OEmbed(
val OEmbedResponse: OEmbedDTO = OEmbedDTO(
"rich",
"1.0",
Some("A Confectioner in the UK"),
Expand Down Expand Up @@ -73,15 +73,15 @@ class OEmbedServiceTest extends UnitSuite with TestEnvironment {
}

test("That get returns a failure with HttpRequestException when receiving http error") {
when(ndlaClient.fetch[OEmbed](any[NdlaRequest])(any))
when(ndlaClient.fetch[OEmbedDTO](any[NdlaRequest])(any))
.thenReturn(Failure(new HttpRequestException("An error occured")))
val oembedTry = oEmbedService.get("https://www.youtube.com/abc", None, None)
oembedTry.isFailure should be(true)
oembedTry.failure.exception.getMessage should equal("An error occured")
}

test("That get returns a Success with an oEmbed when http call is successful") {
when(ndlaClient.fetch[OEmbed](any[NdlaRequest])(any))
when(ndlaClient.fetch[OEmbedDTO](any[NdlaRequest])(any))
.thenReturn(Success(OEmbedResponse))
val oembedTry = oEmbedService.get("https://ndla.no/abc", None, None)
oembedTry.isSuccess should be(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ProviderServiceTest extends UnitSuite with TestEnvironment {

test("That loadProvidersFromRequest fails on invalid url/bad response") {
val invalidUrl = "invalidUrl123"
when(ndlaClient.fetch[OEmbed](any[NdlaRequest])(any))
when(ndlaClient.fetch[OEmbedDTO](any[NdlaRequest])(any))
.thenReturn(Failure(new HttpRequestException("An error occured")))
intercept[DoNotUpdateMemoizeException] {
providerService.loadProvidersFromRequest(quickRequest.get(uri"$invalidUrl"))
Expand Down

0 comments on commit f2b04de

Please sign in to comment.