api: Update to use hff_notion_api_client instead of defunct notion_utils.

This commit is contained in:
2024-08-12 13:18:02 -05:00
parent e90f392ef1
commit b17520946e
6 changed files with 40 additions and 33 deletions

View File

@ -26,7 +26,7 @@ template jsonResp(code: HttpCode,
headersToSend: RawHeaders = @{:} ) =
## Immediately send a JSON response and stop processing the request.
let reqOrigin =
if request.headers.hasKey("Origin"): $(request.headers["Origin"])
if headers(request).hasKey("Origin"): $(headers(request)["Origin"])
else: ""
let corsHeaders =
@ -34,7 +34,7 @@ template jsonResp(code: HttpCode,
@{
"Access-Control-Allow-Origin": reqOrigin,
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Methods": $(request.reqMethod),
"Access-Control-Allow-Methods": $(reqMethod(request)),
"Access-Control-Allow-Headers": "Authorization,X-CSRF-TOKEN"
}
else: @{:}
@ -81,7 +81,7 @@ template errorResp(err: ref ApiError): void =
template optionsResp(allowedMethods: seq[HttpMethod]) =
let reqOrigin =
if request.headers.hasKey("Origin"): $(request.headers["Origin"])
if headers(request).hasKey("Origin"): $(headers(request)["Origin"])
else: ""
let corsHeaders =

View File

@ -1,5 +1,5 @@
import std/json, std/times
import notion_utils, timeutils
import hff_notion_api_client/utils, timeutils
type
@ -43,7 +43,7 @@ proc parseEventProposal*(n: JsonNode): EventProposal {.raises: [JsonParsingError
except:
raise newException(JsonParsingError, "Invalid EventProposal: " & getCurrentExceptionMsg())
proc asNotionPage*(ep: EventProposal): JsonNode =
proc toPage*(ep: EventProposal): JsonNode =
result = %*{
"properties": {
"Event": makeTextProp("title", ep.name),

View File

@ -1,22 +1,29 @@
import std/json, std/logging, std/httpclient, std/sequtils, std/strutils
import notion_utils
import std/[json, httpclient, logging, options, sequtils, strutils]
import hff_notion_api_client
import hff_notion_api_client/config
import ./models, ./service
var notionClient = none[NotionClient]()
proc getNotionClient(cfg: HffEntryFormsApiConfig): NotionClient =
if notionClient.isNone:
notionClient = some(initNotionClient(NotionClientConfig(
apiVersion: cfg.notionVersion,
apiBaseUrl: cfg.notionApiBaseUrl,
configDbId: "",
integrationToken: cfg.integrationToken)))
return notionClient.get
proc getEventProposalConfig*(cfg: HffEntryFormsApiConfig): EventProposalConfig =
let http = newNotionClient(
apiVersion = cfg.notionVersion,
integrationToken = cfg.integrationToken)
let notion = getNotionClient(cfg)
let apiResp = http.get(cfg.notionApiBaseUrl & "/databases/" & cfg.eventParentId)
debug apiResp.status
if not apiResp.status.startsWith("2"):
debug apiResp.body
var bodyJson: JsonNode
try: bodyJson = notion.fetchDatabaseObject(cfg.eventParentId)
except:
raiseApiError(Http500,
"unable to read event propsal configuration from notion API")
let bodyJson = parseJson(apiResp.body)
let departmentOptionsJson = bodyJson{
"properties", "Department", "multi_select", "options"}
@ -31,16 +38,9 @@ proc getEventProposalConfig*(cfg: HffEntryFormsApiConfig): EventProposalConfig =
)
proc createProposedEvent*(cfg: HffEntryFormsApiConfig, ep: EventProposal): bool =
let http = newNotionClient(
apiVersion = cfg.notionVersion,
integrationToken = cfg.integrationToken)
let notion = getNotionClient(cfg)
let epNotionPage = ep.asNotionPage
epNotionPage["parent"] = %*{ "database_id": cfg.eventParentId }
let apiResp = http.post(cfg.notionApiBaseUrl & "/pages", $epNotionPage)
debug apiResp.status
if not apiResp.status.startsWith("2"): debug apiResp.body
return apiResp.status.startsWith("2")
try:
discard notion.createDbPage(cfg.eventParentId, ep)
return true
except: return false