From 5a51a87fc43f500a5852b599b4f2f0c55ed58952 Mon Sep 17 00:00:00 2001 From: Jacob-Eliat-Eliat <167076615+Jacob-Eliat-Eliat@users.noreply.github.com> Date: Thu, 10 Oct 2024 13:15:07 +0200 Subject: [PATCH] api method to get the download URL instead of directly download (#788) * api method to get the download URL instead of directly download * fmt * fix test * cleanup * change version * bumping view name --- build.sbt | 2 +- .../sdk/scala/v1/resources/files.scala | 30 +++++++++++-------- .../com/cognite/sdk/scala/v1/FilesTest.scala | 27 +++++++++++++++++ .../v1/fdm/instances/InstancesTest.scala | 16 +++++----- 4 files changed, 53 insertions(+), 22 deletions(-) diff --git a/build.sbt b/build.sbt index 0db274ebf..2c8b0d1ca 100644 --- a/build.sbt +++ b/build.sbt @@ -40,7 +40,7 @@ lazy val commonSettings = Seq( organization := "com.cognite", organizationName := "Cognite", organizationHomepage := Some(url("https://cognite.com")), - version := "2.29." + patchVersion, + version := "2.30." + patchVersion, isSnapshot := patchVersion.endsWith("-SNAPSHOT"), scalaVersion := scala213, // use 2.13 by default // handle cross plugin https://github.com/stringbean/sbt-dependency-lock/issues/13 diff --git a/src/main/scala/com/cognite/sdk/scala/v1/resources/files.scala b/src/main/scala/com/cognite/sdk/scala/v1/resources/files.scala index d5e54028d..db564b67c 100644 --- a/src/main/scala/com/cognite/sdk/scala/v1/resources/files.scala +++ b/src/main/scala/com/cognite/sdk/scala/v1/resources/files.scala @@ -144,25 +144,29 @@ class Files[F[_]: Applicative](val requestSession: RequestSession[F]) override def search(searchQuery: FilesQuery): F[Seq[File]] = Search.search(requestSession, baseUrl, searchQuery) - def download(item: FileDownload, out: java.io.OutputStream): F[Unit] = { - val request = - requestSession - .post[Items[FileDownloadLink], Items[FileDownloadLink], Items[FileDownload]]( - Items(Seq(item)), - uri"${baseUrl.toString}/downloadlink", - values => values + def downloadLink(item: FileDownload): F[FileDownloadLink] = + requestSession + .post[Items[FileDownloadLink], Items[FileDownloadLink], Items[FileDownload]]( + Items(Seq(item)), + uri"${baseUrl.toString}/downloadlink", + values => values + ) + .map( + _.items.headOption.getOrElse( + throw SdkException(s"File download of ${item.toString} did not return download url") ) + ) + + def download(item: FileDownload, out: java.io.OutputStream): F[Unit] = { + val link: F[FileDownloadLink] = downloadLink(item) requestSession.flatMap( - request, - (files: Items[FileDownloadLink]) => { + link, + (link: FileDownloadLink) => { val response = requestSession.send { request => request .get( - uri"${files.items - .map(_.downloadUrl) - .headOption - .getOrElse(throw SdkException(s"File download of ${item.toString} did not return download url"))}" + uri"${link.downloadUrl}" ) .response(asByteArray) } diff --git a/src/test/scala/com/cognite/sdk/scala/v1/FilesTest.scala b/src/test/scala/com/cognite/sdk/scala/v1/FilesTest.scala index 1e6184d53..d09af0481 100644 --- a/src/test/scala/com/cognite/sdk/scala/v1/FilesTest.scala +++ b/src/test/scala/com/cognite/sdk/scala/v1/FilesTest.scala @@ -299,6 +299,33 @@ class FilesTest extends SdkTestSpec with ReadBehaviours with WritableBehaviors w client.files.deleteById(file.id).unsafeRunSync() } + it should "support returning download link" in { + val file = + client.files.upload( + new java.io.File("./src/test/scala/com/cognite/sdk/scala/v1/uploadTest.txt") + ).unsafeRunSync() + + + var uploadedFile = client.files.retrieveByIds(Seq(file.id)).unsafeRunSync() + var retryCount = 0 + while (!uploadedFile.headOption + .getOrElse(throw new RuntimeException("File was not uploaded in test")) + .uploaded) { + retryCount += 1 + Thread.sleep(500) + uploadedFile = client.files.retrieveByIds(Seq(file.id)).unsafeRunSync() + if (retryCount > 10) { + throw new RuntimeException("File is not uploaded after 10 retries in test") + } + } + + val link = client.files.downloadLink(FileDownloadId(file.id)).unsafeRunSync() + + + link.downloadUrl shouldNot be(empty) + client.files.deleteById(file.id).unsafeRunSync() + } + it should "support search with dataSetIds" in { val created = filesToCreate.map(f => client.files.createOneFromRead(f).unsafeRunSync()) try { diff --git a/src/test/scala/com/cognite/sdk/scala/v1/fdm/instances/InstancesTest.scala b/src/test/scala/com/cognite/sdk/scala/v1/fdm/instances/InstancesTest.scala index ffb915155..fb525e95e 100644 --- a/src/test/scala/com/cognite/sdk/scala/v1/fdm/instances/InstancesTest.scala +++ b/src/test/scala/com/cognite/sdk/scala/v1/fdm/instances/InstancesTest.scala @@ -32,16 +32,16 @@ import scala.concurrent.duration.DurationInt class InstancesTest extends CommonDataModelTestHelper { private val space = Utils.SpaceExternalId - private val edgeNodeContainerExtId = "sdkTest14EdgeNodeContainer2" - private val edgeContainerExtId = "sdkTest14EdgeContainer2" - private val nodeContainer1ExtId = "sdkTest14NodeContainer3" - private val nodeContainer2ExtId = "sdkTest14NodeContainer4" + private val edgeNodeContainerExtId = "sdkTest15EdgeNodeContainer2" + private val edgeContainerExtId = "sdkTest15EdgeContainer2" + private val nodeContainer1ExtId = "sdkTest15NodeContainer3" + private val nodeContainer2ExtId = "sdkTest15NodeContainer4" private val containerForDirectNodeRelationExtId = Utils.DirectNodeRelationContainerExtId - private val edgeNodeViewExtId = "sdkTest14EdgeNodeView2" - private val edgeViewExtId = "sdkTest14EdgeView3" - private val nodeView1ExtId = "sdkTest14NodeView4" - private val nodeView2ExtId = "sdkTest14NodeView5" + private val edgeNodeViewExtId = "sdkTest15EdgeNodeView2" + private val edgeViewExtId = "sdkTest15EdgeView3" + private val nodeView1ExtId = "sdkTest15NodeView4" + private val nodeView2ExtId = "sdkTest15NodeView5" private val viewForDirectNodeRelationExtId = Utils.DirectNodeRelationViewExtId private val viewVersion = Utils.ViewVersion