Skip to content

Commit 134926b

Browse files
Etsijasschuberth
authored andcommitted
scanner: presigned URL 1st implementation, doesn't work yet
Signed-off-by: Etsija <[email protected]>
1 parent 40f02e1 commit 134926b

File tree

4 files changed

+79
-12
lines changed

4 files changed

+79
-12
lines changed

clients/dos/build.gradle.kts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ plugins {
2929

3030
dependencies {
3131
api(libs.retrofit)
32+
api(libs.log4jApiKotlin)
3233

3334
implementation(libs.bundles.kotlinxSerialization)
3435
implementation(libs.retrofitConverterKotlinxSerialization)
35-
36-
testImplementation(libs.wiremock)
3736
}
3837

3938
tasks.withType<KotlinCompile>().configureEach {

clients/dos/src/main/kotlin/DOSService.kt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,60 @@
1818
*/
1919

2020
package org.ossreviewtoolkit.clients.dos
21+
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
22+
import kotlinx.serialization.Serializable
23+
import kotlinx.serialization.json.Json
24+
import kotlinx.serialization.json.JsonNamingStrategy
25+
import okhttp3.MediaType.Companion.toMediaType
26+
import okhttp3.OkHttpClient
27+
import org.apache.logging.log4j.kotlin.Logging
28+
import retrofit2.Retrofit
29+
import retrofit2.http.Body
30+
import retrofit2.http.POST
2131

2232
interface DOSService {
2333

34+
companion object: Logging {
35+
/**
36+
* The default API URL.
37+
*/
38+
const val DEFAULT_API_URL = "https://double-open-server.herokuapp.com/api/"
39+
40+
/**
41+
* The JSON (de-)serialization object used by this service.
42+
*/
43+
val JSON = Json {
44+
ignoreUnknownKeys = true
45+
namingStrategy = JsonNamingStrategy.SnakeCase
46+
}
47+
48+
/**
49+
* Create a new service instance that connects to the [url] specified and uses the optionally provided [client].
50+
*/
51+
fun create(url: String? = null, client: OkHttpClient? = null): DOSService {
52+
val contentType = "application/json; charset=utf-8".toMediaType()
53+
val retrofit = Retrofit.Builder()
54+
.apply { if (client != null) client(client) }
55+
.baseUrl(url ?: DEFAULT_API_URL)
56+
.addConverterFactory(JSON.asConverterFactory(contentType))
57+
.build()
58+
59+
return retrofit.create(DOSService::class.java)
60+
}
61+
}
62+
@Serializable
63+
data class PresignedUrlRequestBody(
64+
val key: String? = null
65+
)
66+
@Serializable
67+
data class PresignedUrlResponseBody(
68+
val success: Boolean,
69+
val presignedUrl: String? = null
70+
)
71+
72+
val json: Json.Default
73+
74+
@POST("upload-url")
75+
suspend fun getPresignedUrl(@Body body: PresignedUrlRequestBody): PresignedUrlResponseBody
76+
2477
}

plugins/scanners/dos/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@
2020
plugins {
2121
// Apply precompiled plugins.
2222
id("ort-library-conventions")
23+
alias(libs.plugins.kotlinSerialization)
2324
}
2425

2526
dependencies {
2627
api(project(":model"))
2728
api(project(":scanner"))
29+
30+
implementation(libs.bundles.kotlinxSerialization)
31+
implementation(libs.kotlinxCoroutines)
32+
implementation(project(":clients:dos-client"))
2833
}

plugins/scanners/dos/src/main/kotlin/DOS.kt

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,12 @@
1919

2020
package org.ossreviewtoolkit.plugins.scanners.dos
2121

22-
import java.io.File
23-
import java.time.Instant
24-
25-
import okhttp3.MediaType.Companion.toMediaType
26-
import okhttp3.OkHttpClient
27-
import okhttp3.Request
28-
import okhttp3.RequestBody.Companion.toRequestBody
29-
22+
import kotlinx.coroutines.runBlocking
23+
import kotlinx.serialization.encodeToString
24+
import kotlinx.serialization.json.Json
3025
import org.apache.logging.log4j.kotlin.Logging
31-
26+
import org.ossreviewtoolkit.clients.dos.DOSService
27+
import org.ossreviewtoolkit.clients.dos.DOSService.PresignedUrlRequestBody
3228
import org.ossreviewtoolkit.model.ScanSummary
3329
import org.ossreviewtoolkit.model.ScannerDetails
3430
import org.ossreviewtoolkit.model.config.DownloaderConfiguration
@@ -37,6 +33,8 @@ import org.ossreviewtoolkit.scanner.AbstractScannerWrapperFactory
3733
import org.ossreviewtoolkit.scanner.PathScannerWrapper
3834
import org.ossreviewtoolkit.scanner.ScanContext
3935
import org.ossreviewtoolkit.scanner.ScannerCriteria
36+
import java.io.File
37+
import java.time.Instant
4038

4139
class DOS internal constructor(
4240
private val name: String,
@@ -53,15 +51,26 @@ class DOS internal constructor(
5351
get() = ScannerDetails(name, "1.0", "")
5452

5553
override val criteria: ScannerCriteria? = null
54+
55+
private val service = DOSService.create()
5656
override fun scanPath(path: File, context: ScanContext): ScanSummary {
5757

5858
val startTime = Instant.now()
5959

6060
logger.info { "DOS / path to scan: $path" }
61-
val dosUrl = System.getenv("DOS_URL")
6261
val spacesKey = System.getenv("SPACES_KEY")
6362

63+
runBlocking {
64+
val requestBody = PresignedUrlRequestBody(spacesKey)
65+
val requestBodyJson = Json.encodeToString(requestBody)
66+
logger.info { "DOS / presigned URL request body: $requestBodyJson" }
67+
val response = service.getPresignedUrl(requestBody)
68+
val responseJson = Json.encodeToString(response)
69+
logger.info { "DOS / presigned URL call results: $responseJson" }
70+
}
71+
6472
// Request presigned URL from DOS API
73+
/*
6574
val client = OkHttpClient()
6675
val endPoint = "upload-url"
6776
val jsonMediaType = "application/json; charset=utf-8".toMediaType()
@@ -81,6 +90,7 @@ class DOS internal constructor(
8190
val presignedUrlKey = response.body?.string()
8291
logger.info { "DOS / presigned URL: $presignedUrlKey" }
8392
}
93+
*/
8494

8595
val endTime = Instant.now()
8696

0 commit comments

Comments
 (0)