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

RUMM-2342 Add support to v2 request #99

Merged
merged 3 commits into from
Jul 20, 2022
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 @@ -9,15 +9,16 @@ package com.datadog.gradle.plugin.internal
import com.datadog.gradle.plugin.DdAndroidGradlePlugin.Companion.LOGGER
import com.datadog.gradle.plugin.RepositoryInfo
import java.io.File
import java.lang.IllegalStateException
import java.net.HttpURLConnection
import java.util.concurrent.TimeUnit
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import org.json.JSONObject

internal class OkHttpUploader : Uploader {

Expand All @@ -28,8 +29,9 @@ internal class OkHttpUploader : Uploader {
internal val client
get() = OkHttpClient
.Builder()
.callTimeout(NETWORK_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.callTimeout(0, TimeUnit.MILLISECONDS) // unlimited
.writeTimeout(NETWORK_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.connectTimeout(NETWORK_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.build()

@Suppress("TooGenericExceptionCaught")
Expand Down Expand Up @@ -81,17 +83,33 @@ internal class OkHttpUploader : Uploader {
.format(mappingFile.absolutePath)
)
}

val eventJson = JSONObject()
eventJson.put("version", identifier.version)
eventJson.put("service", identifier.serviceName)
eventJson.put("variant", identifier.variant)
eventJson.put("type", TYPE_JVM_MAPPING_FILE)

val builder = MultipartBody.Builder()
builder.setType(MultipartBody.FORM)
.addFormDataPart("version", identifier.version)
.addFormDataPart("service", identifier.serviceName)
.addFormDataPart("variant", identifier.variant)
.addFormDataPart("type", TYPE_JVM_MAPPING_FILE)
.addFormDataPart("jvm_mapping_file", mappingFile.name, mappingFileBody)
.addFormDataPart(
KEY_EVENT,
KEY_EVENT,
eventJson.toString(0).toRequestBody(MEDIA_TYPE_JSON)
)
.addFormDataPart(
KEY_JVM_MAPPING_FILE,
KEY_JVM_MAPPING,
mappingFileBody
)

if (repositoryFile != null) {
val repositoryFileBody = repositoryFile.asRequestBody(MEDIA_TYPE_JSON)
builder.addFormDataPart("repository", repositoryFile.name, repositoryFileBody)
builder.addFormDataPart(
KEY_REPOSITORY,
KEY_REPOSITORY,
repositoryFileBody
)
}
if (repositoryInfo != null) {
builder.addFormDataPart("git_repository_url", repositoryInfo.url)
Expand All @@ -118,10 +136,17 @@ internal class OkHttpUploader : Uploader {
"Unable to upload mapping file for $identifier; " +
"verify that you're using a valid API Key"
)
statusCode >= HttpURLConnection.HTTP_BAD_REQUEST -> throw IllegalStateException(
"Unable to upload mapping file for $identifier; " +
"it can be because the mapping file already exist for this version"
statusCode == HttpURLConnection.HTTP_CLIENT_TIMEOUT -> throw RuntimeException(
"Unable to upload mapping file for $identifier because of a request timeout; " +
"check your network connection"
)
statusCode >= HttpURLConnection.HTTP_BAD_REQUEST -> {
throw IllegalStateException(
"Unable to upload mapping file for $identifier ($statusCode); " +
"it can be because the mapping file already exist for this version.\n" +
"${response.body?.string()}"
)
}
}
}

Expand All @@ -137,6 +162,11 @@ internal class OkHttpUploader : Uploader {
internal const val HEADER_EVP_ORIGIN_VERSION = "DD-EVP-ORIGIN-VERSION"
internal const val HEADER_REQUEST_ID = "DD-REQUEST-ID"

internal const val KEY_EVENT = "event"
internal const val KEY_JVM_MAPPING_FILE = "jvm_mapping_file"
xgouchet marked this conversation as resolved.
Show resolved Hide resolved
internal const val KEY_JVM_MAPPING = "jvm_mapping"
internal const val KEY_REPOSITORY = "repository"

internal val NETWORK_TIMEOUT_MS = TimeUnit.SECONDS.toMillis(45)
internal val MEDIA_TYPE_TXT = "text/plain".toMediaTypeOrNull()
internal val MEDIA_TYPE_JSON = "application/json".toMediaTypeOrNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,20 @@ internal class OkHttpUploaderTest {
}

@Test
fun `M use an OkHttpClient W callTimeout { 45 seconds }`() {
assertThat(testedUploader.client.callTimeoutMillis).isEqualTo(
fun `M set unlimited client callTimeout W init()`() {
assertThat(testedUploader.client.callTimeoutMillis).isEqualTo(0)
}

@Test
fun `M set client writeTimeout W init()`() {
assertThat(testedUploader.client.writeTimeoutMillis).isEqualTo(
OkHttpUploader.NETWORK_TIMEOUT_MS.toInt()
)
}

@Test
fun `M use an OkHttpClient W writeTimeout { 45 seconds }`() {
assertThat(testedUploader.client.writeTimeoutMillis).isEqualTo(
fun `M set client connectTimeout W init()`() {
assertThat(testedUploader.client.connectTimeoutMillis).isEqualTo(
OkHttpUploader.NETWORK_TIMEOUT_MS.toInt()
)
}
Expand All @@ -139,21 +144,26 @@ internal class OkHttpUploaderTest {
assertThat(mockWebServer.requestCount).isEqualTo(1)
assertThat(dispatchedRequest)
.hasMethod("POST")
.containsFormData("version", fakeIdentifier.version)
.containsFormData("service", fakeIdentifier.serviceName)
.containsFormData("variant", fakeIdentifier.variant)
.containsFormData("type", OkHttpUploader.TYPE_JVM_MAPPING_FILE)
.containsFormData("git_repository_url", fakeRepositoryInfo.url)
.containsFormData("git_commit_sha", fakeRepositoryInfo.hash)
.containsMultipartFile(
"event",
"event",
"{\"service\":\"${fakeIdentifier.serviceName}\"," +
"\"variant\":\"${fakeIdentifier.variant}\"," +
"\"type\":\"${OkHttpUploader.TYPE_JVM_MAPPING_FILE}\"," +
"\"version\":\"${fakeIdentifier.version}\"}",
"application/json; charset=utf-8"
)
.containsMultipartFile(
"jvm_mapping_file",
fakeMappingFileName,
"jvm_mapping",
fakeMappingFileContent,
"text/plain"
)
.containsMultipartFile(
"repository",
fakeRepositoryFileName,
"repository",
fakeRepositoryFileContent,
"application/json"
)
Expand All @@ -180,13 +190,18 @@ internal class OkHttpUploaderTest {
assertThat(mockWebServer.requestCount).isEqualTo(1)
assertThat(dispatchedRequest)
.hasMethod("POST")
.containsFormData("version", fakeIdentifier.version)
.containsFormData("service", fakeIdentifier.serviceName)
.containsFormData("variant", fakeIdentifier.variant)
.containsFormData("type", OkHttpUploader.TYPE_JVM_MAPPING_FILE)
.containsMultipartFile(
"event",
"event",
"{\"service\":\"${fakeIdentifier.serviceName}\"," +
"\"variant\":\"${fakeIdentifier.variant}\"," +
"\"type\":\"${OkHttpUploader.TYPE_JVM_MAPPING_FILE}\"," +
"\"version\":\"${fakeIdentifier.version}\"}",
"application/json; charset=utf-8"
)
.containsMultipartFile(
"jvm_mapping_file",
fakeMappingFileName,
"jvm_mapping",
fakeMappingFileContent,
"text/plain"
)
Expand Down Expand Up @@ -218,21 +233,26 @@ internal class OkHttpUploaderTest {
assertThat(mockWebServer.requestCount).isEqualTo(1)
assertThat(dispatchedRequest)
.hasMethod("POST")
.containsFormData("version", fakeIdentifier.version)
.containsFormData("service", fakeIdentifier.serviceName)
.containsFormData("variant", fakeIdentifier.variant)
.containsFormData("type", OkHttpUploader.TYPE_JVM_MAPPING_FILE)
.containsFormData("git_repository_url", fakeRepositoryInfo.url)
.containsFormData("git_commit_sha", fakeRepositoryInfo.hash)
.containsMultipartFile(
"event",
"event",
"{\"service\":\"${fakeIdentifier.serviceName}\"," +
"\"variant\":\"${fakeIdentifier.variant}\"," +
"\"type\":\"${OkHttpUploader.TYPE_JVM_MAPPING_FILE}\"," +
"\"version\":\"${fakeIdentifier.version}\"}",
"application/json; charset=utf-8"
)
.containsMultipartFile(
"jvm_mapping_file",
fakeMappingFileName,
"jvm_mapping",
fakeMappingFileContent,
"text/plain"
)
.containsMultipartFile(
"repository",
fakeRepositoryFileName,
"repository",
fakeRepositoryFileContent,
"application/json"
)
Expand Down Expand Up @@ -267,21 +287,26 @@ internal class OkHttpUploaderTest {
assertThat(mockWebServer.requestCount).isEqualTo(1)
assertThat(dispatchedRequest)
.hasMethod("POST")
.containsFormData("version", fakeIdentifier.version)
.containsFormData("service", fakeIdentifier.serviceName)
.containsFormData("variant", fakeIdentifier.variant)
.containsFormData("type", OkHttpUploader.TYPE_JVM_MAPPING_FILE)
.containsFormData("git_repository_url", fakeRepositoryInfo.url)
.containsFormData("git_commit_sha", fakeRepositoryInfo.hash)
.containsMultipartFile(
"event",
"event",
"{\"service\":\"${fakeIdentifier.serviceName}\"," +
"\"variant\":\"${fakeIdentifier.variant}\"," +
"\"type\":\"${OkHttpUploader.TYPE_JVM_MAPPING_FILE}\"," +
"\"version\":\"${fakeIdentifier.version}\"}",
"application/json; charset=utf-8"
)
.containsMultipartFile(
"jvm_mapping_file",
fakeMappingFileName,
"jvm_mapping",
fakeMappingFileContent,
"text/plain"
)
.containsMultipartFile(
"repository",
fakeRepositoryFileName,
"repository",
fakeRepositoryFileContent,
"application/json"
)
Expand Down