api: Extract common Notion functionality to library.
This commit is contained in:
@ -1,4 +1,7 @@
|
||||
import json, times, timeutils
|
||||
import std/json, std/times
|
||||
import timeutils
|
||||
|
||||
import ../../../../notion_utils/src/notion_utils
|
||||
|
||||
type
|
||||
EventProposal* = object
|
||||
@ -26,14 +29,6 @@ proc getOrFail(n: JsonNode, key: string): JsonNode =
|
||||
proc parseIso8601(n: JsonNode, key: string): DateTime =
|
||||
return parseIso8601(n.getOrFail(key).getStr)
|
||||
|
||||
proc textProp(value: string): JsonNode =
|
||||
return %*[
|
||||
{
|
||||
"type": "text",
|
||||
"text": { "content": value }
|
||||
}
|
||||
]
|
||||
|
||||
proc parseEventProposal*(n: JsonNode): EventProposal {.raises: [JsonParsingError].} =
|
||||
|
||||
try:
|
||||
@ -52,33 +47,33 @@ proc parseEventProposal*(n: JsonNode): EventProposal {.raises: [JsonParsingError
|
||||
proc asNotionPage*(ep: EventProposal): JsonNode =
|
||||
result = %*{
|
||||
"properties": {
|
||||
"Event": { "title": textProp(ep.name) },
|
||||
"Event": makeTextProp("title", ep.name),
|
||||
"Date": { "date": { "start": formatIso8601(ep.date) } },
|
||||
"Department": { "multi_select": [ { "name": ep.department } ] },
|
||||
"Location": { "rich_text": textProp(ep.location) },
|
||||
"Owner": { "rich_text": textProp(ep.owner) },
|
||||
"Location": makeTextProp("rich_text", ep.location),
|
||||
"Owner": makeTextProp("rich_text", ep.owner),
|
||||
"State": { "select": { "name": "Proposed" } }
|
||||
},
|
||||
"children": [
|
||||
{
|
||||
"object": "block",
|
||||
"type": "heading_2",
|
||||
"heading_2": { "text": textProp("Purpose") }
|
||||
"heading_2": makeTextProp("text", "Purpose")
|
||||
},
|
||||
{
|
||||
"object": "block",
|
||||
"type": "paragraph",
|
||||
"paragraph": { "text": textProp(ep.purpose) }
|
||||
"paragraph": makeTextProp("text", ep.purpose)
|
||||
},
|
||||
{
|
||||
"object": "block",
|
||||
"type": "heading_2",
|
||||
"heading_2": { "text": textProp("Description") }
|
||||
"heading_2": makeTextProp("text", "Description")
|
||||
},
|
||||
{
|
||||
"object": "block",
|
||||
"type": "paragraph",
|
||||
"paragraph": { "text": textProp(ep.description) }
|
||||
"paragraph": makeTextProp("text", ep.description)
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -1,18 +1,12 @@
|
||||
import json, logging, std/httpclient, sequtils, strutils
|
||||
|
||||
import ../../../../notion_utils/src/notion_utils
|
||||
import ./models, ./service
|
||||
|
||||
proc makeHttpClient(cfg: HffEntryFormsApiConfig): HttpClient =
|
||||
let headers = newHttpHeaders([
|
||||
("Content-Type", "application/json"),
|
||||
("Authorization", "Bearer " & cfg.integrationToken),
|
||||
("Notion-Version", cfg.notionVersion)
|
||||
], true)
|
||||
debug $headers
|
||||
return newHttpClient(headers = headers, )
|
||||
|
||||
proc getEventProposalConfig*(cfg: HffEntryFormsApiConfig): EventProposalConfig =
|
||||
let http = makeHttpClient(cfg)
|
||||
let http = newNotionClient(
|
||||
apiVersion = cfg.notionVersion,
|
||||
integrationToken = cfg.integrationToken)
|
||||
|
||||
let apiResp = http.get(cfg.notionApiBaseUrl & "/databases/" & cfg.eventParentId)
|
||||
debug apiResp.status
|
||||
@ -37,7 +31,10 @@ proc getEventProposalConfig*(cfg: HffEntryFormsApiConfig): EventProposalConfig =
|
||||
)
|
||||
|
||||
proc createProposedEvent*(cfg: HffEntryFormsApiConfig, ep: EventProposal): bool =
|
||||
let http = makeHttpClient(cfg)
|
||||
let http = newNotionClient(
|
||||
apiVersion = cfg.notionVersion,
|
||||
integrationToken = cfg.integrationToken)
|
||||
|
||||
let epNotionPage = ep.asNotionPage
|
||||
epNotionPage["parent"] = %*{ "database_id": cfg.eventParentId }
|
||||
|
||||
|
Reference in New Issue
Block a user