Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Replace spray json with zio-json for FileMetadataSipiResponse #2941

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

package org.knora.webapi.store.iiif.api

import org.apache.pekko.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json.DefaultJsonProtocol
import spray.json.RootJsonFormat
import zio.*
import zio.json.DeriveJsonDecoder
import zio.json.JsonDecoder
import zio.macros.accessible
import zio.nio.file.Path

Expand Down Expand Up @@ -43,18 +42,23 @@
throw SipiException(s"Sipi returned an empty originalFilename")
}

if (originalMimeType.contains("")) {
throw SipiException(s"Sipi returned an empty originalMimeType")

Check warning on line 46 in webapi/src/main/scala/org/knora/webapi/store/iiif/api/SipiService.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/store/iiif/api/SipiService.scala#L45-L46

Added lines #L45 - L46 were not covered by tests
}
}

object FileMetadataSipiResponse extends SprayJsonSupport with DefaultJsonProtocol {
implicit val sipiKnoraJsonResponseFormat: RootJsonFormat[FileMetadataSipiResponse] = jsonFormat8(
FileMetadataSipiResponse.apply
)
object FileMetadataSipiResponse {
// Because Sipi returns JSON Numbers which are whole numbers but not a valid Scala Int, e.g. `width: 1920.0`, we need to
// use a custom decoder for Int. See also https://github.com/zio/zio-json/issues/1049#issuecomment-1814108354
implicit val anyWholeNumber: JsonDecoder[Int] = JsonDecoder[Double].mapOrFail { d =>
val i = d.toInt
if (d == i.toDouble) { Right(i) }
else { Left("32-bit int expected") }

Check warning on line 56 in webapi/src/main/scala/org/knora/webapi/store/iiif/api/SipiService.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/store/iiif/api/SipiService.scala#L53-L56

Added lines #L53 - L56 were not covered by tests
}
implicit val decoder: JsonDecoder[FileMetadataSipiResponse] = DeriveJsonDecoder.gen[FileMetadataSipiResponse]
}

@accessible

Check warning on line 61 in webapi/src/main/scala/org/knora/webapi/store/iiif/api/SipiService.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/store/iiif/api/SipiService.scala#L61

Added line #L61 was not covered by tests
trait SipiService {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.http.util.EntityUtils
import spray.json.*
import zio.*
import zio.json.DecoderOps
import zio.nio.file.Path

import java.net.URI
Expand Down Expand Up @@ -72,8 +73,12 @@
* @return a [[FileMetadataSipiResponse]] containing the requested metadata.
*/
override def getFileMetadata(filePath: String): Task[FileMetadataSipiResponse] =
doSipiRequest(new HttpGet(sipiConfig.internalBaseUrl + filePath + "/knora.json"))
.mapAttempt(_.parseJson.convertTo[FileMetadataSipiResponse])
.flatMap(bodyStr =>

Check warning on line 77 in webapi/src/main/scala/org/knora/webapi/store/iiif/impl/SipiServiceLive.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/store/iiif/impl/SipiServiceLive.scala#L76-L77

Added lines #L76 - L77 were not covered by tests
ZIO
.fromEither(bodyStr.fromJson[FileMetadataSipiResponse])
.mapError(e => SipiException(s"Invalid response from Sipi: $e, $bodyStr"))

Check warning on line 80 in webapi/src/main/scala/org/knora/webapi/store/iiif/impl/SipiServiceLive.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/store/iiif/impl/SipiServiceLive.scala#L79-L80

Added lines #L79 - L80 were not covered by tests
)

/**
* Asks Sipi to move a file from temporary storage to permanent storage.
Expand Down Expand Up @@ -390,6 +395,6 @@
config <- ZIO.serviceWith[AppConfig](_.sipi)
jwtService <- ZIO.service[JwtService]
httpClient <- ZIO.acquireRelease(acquire(config))(release)
} yield SipiServiceLive(config, jwtService, httpClient)

Check warning on line 398 in webapi/src/main/scala/org/knora/webapi/store/iiif/impl/SipiServiceLive.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/store/iiif/impl/SipiServiceLive.scala#L398

Added line #L398 was not covered by tests
}
}
Loading