This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01c8336
commit 1dabdf5
Showing
21 changed files
with
786 additions
and
346 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
object Config { | ||
object Versions { | ||
object Kotlin { | ||
const val kotlin = "1.4.20-RC" | ||
const val coroutines = "1.4.1" | ||
const val kotlin = "1.4.20" | ||
const val serialization = "1.0.1" | ||
} | ||
const val ktor = "1.4.1" | ||
|
||
const val ktor = "1.4.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
rootProject.name = "notionapi" | ||
rootProject.name = "notion-sdk-kotlin" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/kotlin/com/petersamokhin/notionapi/mapper/NotionPropertyMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.petersamokhin.notionapi.mapper | ||
|
||
import com.petersamokhin.notionapi.model.NotionColumn | ||
import com.petersamokhin.notionapi.model.NotionProperty | ||
import com.petersamokhin.notionapi.model.response.NotionCollection | ||
import com.petersamokhin.notionapi.model.response.NotionColumnType | ||
import com.petersamokhin.notionapi.serializer.NotionBooleanSerializer | ||
import com.petersamokhin.notionapi.utils.contentAsStringOrNull | ||
import com.petersamokhin.notionapi.utils.jsonArrayOrNull | ||
import com.petersamokhin.notionapi.utils.trimNotionTextField | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonArray | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.decodeFromJsonElement | ||
|
||
fun NotionCollection.title() = value.name?.trimNotionTextField() | ||
|
||
fun NotionCollection.description() = value.description?.trimNotionTextField() | ||
|
||
fun parseNotionColumn(json: Json, name: String, type: NotionColumnType, field: JsonArray?): NotionColumn { | ||
if (field.isNullOrEmpty()) return NotionColumn.SingleValue(name, type, null) | ||
val masterList = field.filterIsInstance<JsonArray>() | ||
|
||
return when (type) { | ||
NotionColumnType.Title, NotionColumnType.Text, NotionColumnType.Number, | ||
NotionColumnType.Checkbox, NotionColumnType.Select, NotionColumnType.MultiSelect -> { | ||
val label = masterList.firstOrNull()?.getOrNull(0)?.contentAsStringOrNull | ||
?: return NotionColumn.SingleValue(name, type, null) | ||
|
||
return NotionColumn.SingleValue( | ||
name = name, type = type, | ||
value = when (type) { | ||
NotionColumnType.Title -> NotionProperty.Value.Title(label) | ||
NotionColumnType.Text -> NotionProperty.Value.Text(label) | ||
NotionColumnType.Number -> NotionProperty.Value.Number(label.toDouble()) | ||
NotionColumnType.Checkbox -> NotionProperty.Value.Checkbox(label == NotionBooleanSerializer.NOTION_TRUE) | ||
NotionColumnType.Select -> NotionProperty.Value.Select(label) | ||
NotionColumnType.MultiSelect -> NotionProperty.Value.MultiSelect(label.split(",")) | ||
else -> throw IllegalStateException("exhaustive") | ||
}.let { NotionProperty(label = label, it) } | ||
) | ||
} | ||
NotionColumnType.Email, NotionColumnType.Url, NotionColumnType.PhoneNumber, | ||
NotionColumnType.Person, NotionColumnType.File -> { | ||
NotionColumn.MultiValue( | ||
name = name, | ||
type = type, | ||
values = masterList.map { currentItemList -> | ||
currentItemList.filterIsInstance<JsonArray>().mapNotNull { | ||
currentItemList.firstOrNull()?.contentAsStringOrNull?.let { label -> | ||
it.getOrNull(0)?.jsonArrayOrNull?.getOrNull(1)?.contentAsStringOrNull?.let { | ||
label to it | ||
} | ||
} | ||
} | ||
}.flatten() | ||
.map { (label, item) -> | ||
val v = when (type) { | ||
NotionColumnType.Email -> NotionProperty.Value.Entry.Email(item) | ||
NotionColumnType.Url -> NotionProperty.Value.Entry.Link(item) | ||
NotionColumnType.PhoneNumber -> NotionProperty.Value.Entry.PhoneNumber(item) | ||
NotionColumnType.Person -> NotionProperty.Value.Entry.Person(item) | ||
NotionColumnType.File -> NotionProperty.Value.Entry.File(item) | ||
else -> null | ||
} | ||
|
||
NotionProperty(label, v) | ||
} | ||
) | ||
} | ||
NotionColumnType.Date -> { | ||
NotionColumn.SingleValue( | ||
name = name, | ||
type = type, | ||
value = NotionProperty( | ||
label = masterList.firstOrNull()?.getOrNull(0)?.contentAsStringOrNull | ||
?: return NotionColumn.SingleValue(name, type, null), | ||
value = masterList.firstOrNull()?.getOrNull(1) | ||
?.jsonArrayOrNull?.getOrNull(0) | ||
?.jsonArrayOrNull?.getOrNull(1) | ||
?.let<JsonElement, NotionProperty.Value.Entry.Date>(json::decodeFromJsonElement) | ||
) | ||
) | ||
} | ||
NotionColumnType.LastEditedTime, NotionColumnType.LastEditedBy, | ||
NotionColumnType.CreatedTime, NotionColumnType.CreatedBy, | ||
NotionColumnType.Rollup, NotionColumnType.Relation, NotionColumnType.Formula -> { | ||
NotionColumn.SingleValue(name, type, null) | ||
} | ||
} | ||
} |
123 changes: 49 additions & 74 deletions
123
src/main/kotlin/com/petersamokhin/notionapi/mapper/NotionResponseMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,68 @@ | ||
package com.petersamokhin.notionapi.mapper | ||
|
||
import com.petersamokhin.notionapi.model.* | ||
import com.petersamokhin.notionapi.serializer.NotionBooleanSerializer | ||
import com.petersamokhin.notionapi.utils.trimNotionTextField | ||
import kotlinx.serialization.json.JsonArray | ||
import kotlinx.serialization.json.JsonNull | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.JsonPrimitive | ||
import com.petersamokhin.notionapi.model.response.NotionBlock | ||
import com.petersamokhin.notionapi.model.response.NotionCollection | ||
import com.petersamokhin.notionapi.model.response.NotionResponse | ||
import kotlinx.serialization.json.* | ||
|
||
fun NotionCollection.mapTable(blocks: List<NotionBlock>): NotionTable<Map<String, NotionColumn<*>>> { | ||
val rows = blocks.map { | ||
val props = it.value.properties | ||
|
||
mutableMapOf<String, NotionColumn<*>>().also { map -> | ||
props?.keys?.forEach { innerRowKey -> | ||
val schemaItem = value.schema[innerRowKey] ?: return@forEach | ||
fun NotionResponse.mapTable(json: Json, sortColumns: Boolean = false): NotionTable? { | ||
val collectionId = recordMap.collectionsMap?.keys?.firstOrNull() ?: return null | ||
val collection = recordMap.collectionsMap[collectionId] | ||
|
||
schemaItem.name.also { name -> | ||
val fieldText = props[innerRowKey]?.trimNotionTextField() | ||
val value: NotionColumn.Entry<*> = when (schemaItem.type) { | ||
NotionColumnType.Title, NotionColumnType.Text, NotionColumnType.Select -> { | ||
NotionColumn.Entry.Text(name, fieldText) | ||
} | ||
NotionColumnType.Number -> { | ||
NotionColumn.Entry.Number(name, fieldText?.toDoubleOrNull()) | ||
} | ||
NotionColumnType.Checkbox -> { | ||
NotionColumn.Entry.Bool(name, fieldText?.equals(NotionBooleanSerializer.NOTION_TRUE)) | ||
} | ||
NotionColumnType.MultiSelect -> { | ||
NotionColumn.Entry.TextList(name, fieldText?.split(",")) | ||
} | ||
} | ||
val collectionViewId = recordMap.collectionViewsMap?.keys?.firstOrNull() | ||
val collectionView = recordMap.collectionViewsMap?.get(collectionViewId) | ||
val collectionViewFormat = collectionView?.value?.format | ||
|
||
map[name] = NotionColumn(name, schemaItem.type, value) | ||
} | ||
} | ||
} | ||
val sortMap = if (sortColumns && collectionViewFormat != null) { | ||
collectionViewFormat.tableProperties.mapIndexed { index, item -> item.property to index }.toMap() | ||
} else { | ||
null | ||
} | ||
|
||
val schema = value.schema.mapKeys { it.value.name } | ||
val blocks = result?.blockIds?.mapNotNull { recordMap.blocksMap?.get(it) } | ||
|
||
return NotionTable(rows, schema) | ||
return blocks?.let { collection?.mapTable(json, it, sortMap) } | ||
} | ||
|
||
fun NotionResponse.mapTable(): NotionTable<Map<String, NotionColumn<*>>>? { | ||
val collectionId = recordMap.collectionsMap.keys.firstOrNull() | ||
val collection = recordMap.collectionsMap[collectionId] | ||
val blocks = result?.blockIds?.map { recordMap.blocksMap.getValue(it) } | ||
|
||
return blocks?.let { collection?.mapTable(it) } | ||
} | ||
|
||
fun NotionResponse.mapCollectionToJsonArray(): JsonArray? { | ||
val collectionId = recordMap.collectionsMap.keys.firstOrNull() ?: return null | ||
val collection = recordMap.collectionsMap[collectionId] ?: return null | ||
val blocks = result?.blockIds?.map { recordMap.blocksMap.getValue(it) } ?: return null | ||
|
||
val list = blocks.map { | ||
val props = it.value.properties ?: return null | ||
fun NotionCollection.mapTable( | ||
json: Json, | ||
blocks: List<NotionBlock>, | ||
sortMap: Map<String, Int>? = null | ||
): NotionTable { | ||
val rows: List<NotionRow> = blocks.map { block -> | ||
val props = block.value.properties | ||
val propsKeys = props?.keys?.let { propsKeys -> | ||
if (sortMap != null) { | ||
propsKeys.sortedBy { sortMap[it] } | ||
} else { | ||
propsKeys | ||
} | ||
} | ||
val rowItems = mutableMapOf<String, NotionColumn>() | ||
|
||
val map = props.keys.mapNotNull { innerRowKey -> | ||
val schemaItem = collection.value.schema[innerRowKey] ?: return@mapNotNull null | ||
propsKeys?.forEach { innerRowKey -> | ||
val schemaItem = value.schema[innerRowKey] ?: return@forEach | ||
|
||
schemaItem.name.let { name -> | ||
val fieldText = props[innerRowKey]?.trimNotionTextField() | ||
val value = when (schemaItem.type) { | ||
NotionColumnType.Title, NotionColumnType.Text, NotionColumnType.Select -> { | ||
JsonPrimitive(fieldText) | ||
} | ||
NotionColumnType.Number -> { | ||
JsonPrimitive(fieldText?.toDoubleOrNull()) | ||
} | ||
NotionColumnType.Checkbox -> { | ||
JsonPrimitive(fieldText?.equals(NotionBooleanSerializer.NOTION_TRUE)) | ||
} | ||
NotionColumnType.MultiSelect -> { | ||
fieldText?.let { | ||
JsonArray(fieldText.split(",").map(::JsonPrimitive)) | ||
} ?: JsonNull | ||
} | ||
} | ||
schemaItem.name.also { name -> | ||
val field = props[innerRowKey] | ||
|
||
name to value | ||
rowItems[name] = parseNotionColumn(json, name, schemaItem.type, field) | ||
} | ||
}.toMap() | ||
} | ||
|
||
JsonObject(map) | ||
NotionRow( | ||
properties = rowItems, | ||
metaInfo = NotionRow.MetaInfo( | ||
lastEditedBy = block.value.lastEditedById, | ||
lastEditedTime = block.value.lastEditedTime, | ||
createdBy = block.value.createdById, | ||
createdTime = block.value.createdTime | ||
) | ||
) | ||
} | ||
|
||
return JsonArray(list) | ||
val schema = value.schema.mapKeys { it.value.name } | ||
|
||
return NotionTable(rows, schema) | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/petersamokhin/notionapi/model/NotionCredentials.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.petersamokhin.notionapi.model | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class NotionCredentials( | ||
val email: String, | ||
val password: String | ||
) |
33 changes: 0 additions & 33 deletions
33
src/main/kotlin/com/petersamokhin/notionapi/model/NotionMappedResponse.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.