I’m working on a custom gradle task that should extract all my Android project dependencies and send them to a Notion database using their API. Everything seems to be set up correctly, but when I execute the task I keep getting HTTP 400 errors from the Notion API. Strangely enough, if I test with just hardcoded values like simple strings, the API call works fine. I’m really confused about what might be causing this issue. Has anyone dealt with something similar before?
Here’s my current implementation:
abstract class ExportLibrariesToNotion : DefaultTask() {
init {
group = "documentation"
description = "Send project libraries to Notion database"
}
@TaskAction
fun execute() = runBlocking {
val apiKey = System.getenv("NOTION_API_KEY") ?: error("NOTION_API_KEY not found")
val databaseId = System.getenv("NOTION_DB_ID") ?: error("NOTION_DB_ID not found")
val libraryNames = mutableListOf<String>()
val configuration = project.configurations.getByName("releaseRuntimeClasspath")
configuration.incoming.resolutionResult.allDependencies.forEach { dep ->
when (dep) {
is ResolvedDependencyResult -> {
val info = dep.selected
"• ${info.moduleVersion}"
}
else -> null
}?.let {
libraryNames += it
}
}
val sampleData = mutableListOf("alpha", "beta", "gamma")
val contentBlocks = mutableListOf<ContentBlock>()
libraryNames.forEach {
contentBlocks += ContentBlock(
blockType = "paragraph",
paragraphData = ParagraphContent(
textContent = listOf(
TextElement(textData = TextInfo(it))
)
)
)
}
val pageRequest = DatabasePageRequest(
parentRef = DatabaseParent(db_id = databaseId),
pageProperties = mapOf(
"Title" to PageTitle(
titleText = listOf(
TextElement(textData = TextInfo(value = "📚 Project Libraries"))
)
)
),
blockContent = contentBlocks.toList()
)
val httpClient = HttpClient(CIO) {
install(ContentNegotiation) {
json(Json {
prettyPrint = true
ignoreUnknownKeys = true
})
}
}
val apiResponse: HttpResponse = httpClient.post("https://api.notion.com/v1/pages") {
contentType(ContentType.Application.Json)
header("Authorization", "Bearer $apiKey")
header("Notion-Version", "2022-06-28")
setBody(pageRequest)
}
println("🎉 Notion page created! Response: ${apiResponse.status}, Details: ${apiResponse}")
}
}
@Serializable data class DatabasePageRequest(
val parentRef: DatabaseParent,
val pageProperties: Map<String, PageTitle>,
val blockContent: List<ContentBlock>
)
@Serializable data class DatabaseParent(val db_id: String)
@Serializable data class PageTitle(val titleText: List<TextElement>)
@Serializable data class ContentBlock(
val objectType: String = "block",
val blockType: String,
val paragraphData: ParagraphContent? = null
)
@Serializable data class ParagraphContent(val textContent: List<TextElement>)
@Serializable data class TextElement(val elementType: String = "text", val textData: TextInfo)
@Serializable data class TextInfo(val value: String)