added Properties to created Tasks
This commit is contained in:
1 parent
99a494608f
commit
8078e34a2c
10 files changed
+792
-51
No files matched your search
@@ -3,18 +3,29 @@ package ch.ergon.anytask.api
|
||||
import ch.ergon.anytask.api.dto.CreateObjectRequest
|
||||
import ch.ergon.anytask.api.dto.CreateObjectResponse
|
||||
import ch.ergon.anytask.api.dto.ObjectType
|
||||
import ch.ergon.anytask.api.dto.SearchObject
|
||||
import ch.ergon.anytask.api.dto.SearchObjectsResponse
|
||||
import ch.ergon.anytask.api.dto.Space
|
||||
import ch.ergon.anytask.api.dto.SpacesResponse
|
||||
import ch.ergon.anytask.api.dto.TypesResponse
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.call.*
|
||||
import io.ktor.client.engine.cio.*
|
||||
import io.ktor.client.plugins.*
|
||||
import io.ktor.client.plugins.contentnegotiation.*
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.client.statement.*
|
||||
import io.ktor.http.*
|
||||
import io.ktor.serialization.kotlinx.json.*
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
import io.ktor.client.plugins.HttpSend
|
||||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
||||
import io.ktor.client.plugins.plugin
|
||||
import io.ktor.client.request.bearerAuth
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.client.request.parameter
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.client.request.setBody
|
||||
import io.ktor.client.statement.bodyAsText
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.contentType
|
||||
import io.ktor.http.isSuccess
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.apache.logging.log4j.LogManager
|
||||
import kotlin.time.DurationUnit.MILLISECONDS
|
||||
@@ -22,7 +33,7 @@ import kotlin.time.toDuration
|
||||
|
||||
/**
|
||||
* Client for the local AnyType API (anytype-heart middleware).
|
||||
* The API runs on http://localhost:31007 by default.
|
||||
* The API runs on http://localhost:31009 by default.
|
||||
* apiKey and spaceId are loaded from anytask.yaml.
|
||||
*/
|
||||
class AnyTypeApi(
|
||||
@@ -71,13 +82,36 @@ class AnyTypeApi(
|
||||
response.body<TypesResponse>().data
|
||||
}
|
||||
|
||||
suspend fun createTask(title: String, details: String = ""): Result<String> =
|
||||
suspend fun searchObjects(typeKey: String): Result<List<SearchObject>> =
|
||||
runCatching {
|
||||
val response = http.post("$baseUrl/v1/spaces/$spaceId/search") {
|
||||
contentType(ContentType.Application.Json)
|
||||
header("Anytype-Version", "0.1")
|
||||
bearerAuth(apiKey)
|
||||
setBody(mapOf("types" to listOf(typeKey)))
|
||||
}
|
||||
response.body<SearchObjectsResponse>().data
|
||||
}
|
||||
|
||||
suspend fun createObject(
|
||||
title: String,
|
||||
details: String = "",
|
||||
typeKey: String,
|
||||
properties: List<JsonElement> = emptyList(),
|
||||
): Result<String> =
|
||||
runCatching {
|
||||
val response = http.post("$baseUrl/v1/spaces/$spaceId/objects") {
|
||||
contentType(ContentType.Application.Json)
|
||||
header("Anytype-Version", "0.1")
|
||||
bearerAuth(apiKey)
|
||||
setBody(CreateObjectRequest(name = title, body = details, typeKey = "ot-task"))
|
||||
setBody(
|
||||
CreateObjectRequest(
|
||||
name = title,
|
||||
body = details,
|
||||
typeKey = typeKey,
|
||||
properties = properties,
|
||||
)
|
||||
)
|
||||
}
|
||||
response.body<CreateObjectResponse>().obj.id
|
||||
}
|
||||
|
||||
@@ -2,10 +2,12 @@ package ch.ergon.anytask.api.dto
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
|
||||
@Serializable
|
||||
data class CreateObjectRequest(
|
||||
val name: String,
|
||||
val body: String,
|
||||
@SerialName("type_key") val typeKey: String,
|
||||
)
|
||||
val properties: List<JsonElement> = emptyList(),
|
||||
)
|
||||
@@ -0,0 +1,34 @@
|
||||
package ch.ergon.anytask.api.dto
|
||||
|
||||
import kotlinx.serialization.json.JsonArray
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
|
||||
fun buildPropertyValue(key: String, format: String, value: String): JsonElement? {
|
||||
if (value.isBlank()) return null
|
||||
|
||||
return when (format) {
|
||||
"text" -> JsonObject(mapOf("key" to JsonPrimitive(key), "text" to JsonPrimitive(value)))
|
||||
"number" -> JsonObject(mapOf("key" to JsonPrimitive(key), "number" to JsonPrimitive(value.toDoubleOrNull() ?: 0.0)))
|
||||
"select" -> JsonObject(mapOf("key" to JsonPrimitive(key), "select" to JsonPrimitive(value)))
|
||||
"multi_select" -> {
|
||||
val items = value.split(",").map { it.trim() }.filter { it.isNotEmpty() }
|
||||
JsonObject(mapOf("key" to JsonPrimitive(key), "multi_select" to JsonArray(items.map { JsonPrimitive(it) })))
|
||||
}
|
||||
"date" -> JsonObject(mapOf("key" to JsonPrimitive(key), "date" to JsonPrimitive(value)))
|
||||
"checkbox" -> JsonObject(mapOf("key" to JsonPrimitive(key), "checkbox" to JsonPrimitive(value.toBooleanStrictOrNull() ?: false)))
|
||||
"url" -> JsonObject(mapOf("key" to JsonPrimitive(key), "url" to JsonPrimitive(value)))
|
||||
"email" -> JsonObject(mapOf("key" to JsonPrimitive(key), "email" to JsonPrimitive(value)))
|
||||
"phone" -> JsonObject(mapOf("key" to JsonPrimitive(key), "phone" to JsonPrimitive(value)))
|
||||
"files" -> {
|
||||
val ids = value.split(",").map { it.trim() }.filter { it.isNotEmpty() }
|
||||
JsonObject(mapOf("key" to JsonPrimitive(key), "files" to JsonArray(ids.map { JsonPrimitive(it) })))
|
||||
}
|
||||
"object", "objects" -> {
|
||||
val ids = value.split(",").map { it.trim() }.filter { it.isNotEmpty() }
|
||||
JsonObject(mapOf("key" to JsonPrimitive(key), "objects" to JsonArray(ids.map { JsonPrimitive(it) })))
|
||||
}
|
||||
else -> JsonObject(mapOf("key" to JsonPrimitive(key), "text" to JsonPrimitive(value)))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package ch.ergon.anytask.api.dto
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class SearchObject(
|
||||
val id: String,
|
||||
val name: String,
|
||||
)
|
||||
@@ -0,0 +1,8 @@
|
||||
package ch.ergon.anytask.api.dto
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class SearchObjectsResponse(
|
||||
val data: List<SearchObject>,
|
||||
)
|
||||