Add Suite or Building to Address model.
Bug fixes: - Fix pagination when pulling from Notion pages. - Fix the parsing of rollup fields.
This commit is contained in:
parent
ad8f36dc48
commit
03e6c0bf5a
@ -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"
|
||||
|
@ -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)
|
||||
|
@ -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)))
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user