Skip to content

Commit

Permalink
api method to get the download URL instead of directly download (#788)
Browse files Browse the repository at this point in the history
* api method to get the download URL instead of directly download

* fmt

* fix test

* cleanup

* change version

* bumping view name
  • Loading branch information
Jacob-Eliat-Eliat authored Oct 10, 2024
1 parent e22de8e commit 5a51a87
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 17 additions & 13 deletions src/main/scala/com/cognite/sdk/scala/v1/resources/files.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/scala/com/cognite/sdk/scala/v1/FilesTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5a51a87

Please sign in to comment.