diff --git a/hff_notion_api_client.nimble b/hff_notion_api_client.nimble index 55d1b4e..6570061 100644 --- a/hff_notion_api_client.nimble +++ b/hff_notion_api_client.nimble @@ -1,6 +1,6 @@ # Package -version = "0.4.5" +version = "0.5.0" author = "Jonathan Bernard" description = "Utilities and bindings for HFF's Notion API." license = "GPL-3.0-or-later" diff --git a/src/hff_notion_api_client.nim b/src/hff_notion_api_client.nim index 911a356..3960bdd 100644 --- a/src/hff_notion_api_client.nim +++ b/src/hff_notion_api_client.nim @@ -1,11 +1,8 @@ -import std/httpclient, std/json, std/logging, std/sequtils, std/tables, std/strutils +import std/[httpclient, json, logging, options, sequtils, tables, strutils] import buffoonery/jsonutils -import hff_notion_api_client/config -import hff_notion_api_client/models -import hff_notion_api_client/sequtils_ext -import hff_notion_api_client/utils +import hff_notion_api_client/[config, models, sequtils_ext, utils] type NotionClientConfig* = object @@ -61,20 +58,19 @@ proc fetchAllPages*( bodyTemplate = %*{ "page_size": NOTION_MAX_PAGE_SIZE}): seq[JsonNode] = result = @[] - var nextCursor: string = "" + var nextCursor: Option[string] = none[string]() while true: let body = parseJson($bodyTemplate) - if not nextCursor.isEmptyOrWhitespace: body["start_cursor"] = %nextCursor + if nextCursor.isSome: body["start_cursor"] = %nextCursor.get debug "Fetching pages from database:\n\tPOST " & url & "\n\t" & $body let jsonResp = parseJson(http.postContent(url, $body)) result = result & jsonResp["results"].getElems if jsonResp.hasKey("next_cursor") and jsonResp["next_cursor"].kind != JNull: - nextCursor = jsonResp["next_cursor"].getStr - - if nextCursor.isEmptyOrWhitespace: break + nextCursor = some(jsonResp["next_cursor"].getStr) + else: break template fetchDatabaseObject*(notion: NotionClient, dbId: string): untyped = let resp = notion.http.get(notion.apiBaseUrl & "/databases/" & dbId) diff --git a/src/hff_notion_api_client/models.nim b/src/hff_notion_api_client/models.nim index 1c5e8f7..23d030f 100644 --- a/src/hff_notion_api_client/models.nim +++ b/src/hff_notion_api_client/models.nim @@ -11,6 +11,7 @@ type city*: string state*: string street*: string + suiteOrBuilding*: string zipCode*: string createdAt*: Option[DateTime] lastUpdatedAt*: Option[DateTime] @@ -127,6 +128,7 @@ func toPage*(a: Address): JsonNode = "City": makeTextProp("rich_text", a.city), "State": makeSelectProp(a.state), "Street Address": makeTextProp("title", a.street), + "Suite or Building": makeTextProp("rich_text", a.suiteOrBuilding), "Zip Code": makeTextProp("rich_text", a.zipCode) } } @@ -182,6 +184,7 @@ proc addressFromPage*(page: JsonNode): Address = city: page.getText("City"), state: page.getSelect("State"), street: page.getTitle("Street Address"), + suiteOrBuilding: page.getText("Suite or Building"), zipCode: page.getText("Zip Code"), createdAt: some(parseIso8601(page["created_time"].getStr)), lastUpdatedAt: some(parseIso8601(page["last_edited_time"].getStr))) diff --git a/src/hff_notion_api_client/utils.nim b/src/hff_notion_api_client/utils.nim index 082b34b..55433cf 100644 --- a/src/hff_notion_api_client/utils.nim +++ b/src/hff_notion_api_client/utils.nim @@ -87,15 +87,18 @@ proc getRollupArrayValues*(page: JsonNode, propName: string): seq[JsonNode] = proc getRolledupRecordTitles*(page: JsonNode, propName: string): seq[string] = let rollups = page.getPropNode("rollup", propName)["array"] if rollups.getElems.len == 0: return @[] - elif rollups.getElems.len != 1 or not rollups.getElems[0].hasKey("title"): - log().debug "Expected exactly one item of type 'title'. Received: \n" & rollups.pretty - raise newException( - ValueError, - "unexpected format of rollup value for '" & propName & "' property") - let titleElems = rollups.getElems[0]["title"] + result = @[] + for rollupItem in rollups.getElems: + if not rollupItem.hasKey("title") or rollupItem["title"].getElems.len != 1: + log().debug "Expected exactly one item of type 'title'. Received: \n" & + rollupItem.pretty - return titleElems.getElems.mapIt(it["plain_text"].getStr) + raise newException( + ValueError, + "unexpected format of rollup value for '" & propName & "' property") + + result.add(rollupItem["title"].getElems[0]["plain_text"].getStr) proc getText*(page: JsonNode, propName: string): string = let propNode = page.getPropNode("rich_text", propName)