Skip to content

Commit

Permalink
Allow connecting the MP client to MP 1.0 with backwards compatible MP…
Browse files Browse the repository at this point in the history
…Project
  • Loading branch information
blootsvoets committed Aug 23, 2023
1 parent 0e84017 commit eb10e93
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.radarbase.management.client

import io.ktor.http.*
import java.io.IOException

class HttpStatusException(
val code: HttpStatusCode,
message: String,
) : IOException(message)
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class MPClient(config: Config) {
): T = withContext(Dispatchers.IO) {
with(httpClient.request(block)) {
if (!status.isSuccess()) {
throw IOException("Request to ${request.url} failed (code $status)")
throw HttpStatusException(status, "Request to ${request.url} failed (code $status)")
}
body()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

/** ManagementPortal Project DTO. */
@Serializable
@Serializable(with = MPProjectSerializer::class)
data class MPProject(
/** Project id, a name that identifies it uniquely. */
@SerialName("projectName") val id: String,
Expand All @@ -14,6 +14,8 @@ data class MPProject(
val location: String? = null,
/** Organization that organizes the project. */
val organization: MPOrganization? = null,
/** Free-text name of the organization. */
val organizationName: String? = null,
/** Project description. */
val description: String? = null,
/** Any other attributes. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.radarbase.management.client

import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonTransformingSerializer

class MPProjectSerializer : JsonTransformingSerializer<MPProject>(MPProject.serializer()) {
override fun transformDeserialize(element: JsonElement): JsonElement {
if (element !is JsonObject) return element
val organization = element["organization"]
return if (organization == null || organization is JsonNull || organization is JsonObject) {
// MP 2.0 structure
element
} else {
// MP 0.x structure
val elementMap = element.toMutableMap()
elementMap["organization"] = JsonNull
elementMap["organizationName"] = organization
return JsonObject(elementMap)
}
}
}

0 comments on commit eb10e93

Please sign in to comment.