api: Update to use hff_notion_api_client instead of defunct notion_utils.

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

View File

@ -1,12 +1,12 @@
FROM 063932952339.dkr.ecr.us-west-2.amazonaws.com/alpine-nim:nim-1.4.8 AS build
FROM 063932952339.dkr.ecr.us-west-2.amazonaws.com/alpine-nim:nim-1.6.10 AS build
MAINTAINER jonathan@jdbernard.com
COPY hff_entry_forms_api.nimble /hff_entry_forms_api/
COPY src /hff_entry_forms_api/src
WORKDIR /hff_entry_forms_api
RUN nimble build -y
RUN nimble build -y --passC:-fpermissive
FROM alpine
FROM alpine:3.16.4
EXPOSE 80
RUN apk -v --update add --no-cache \
ca-certificates \

View File

@ -4,7 +4,14 @@ SOURCES=$(wildcard src/*.nim) $(wildcard src/hff_entry_forms_apipkg/*.nim)
ECR_ACCOUNT_URL ?= 063932952339.dkr.ecr.us-west-2.amazonaws.com
# The port on the host machine (not the container)
PORT ?= 8300
ifeq ($(TARGET_ENV),prod)
PORT=6006
else
PORT=6005
endif
# The server to target when publishing the API
TARGET_SERVER ?= sobeck.jdb-software.com
# The Notion integration token.
AUTH_SECRET ?= 123abc

View File

@ -16,7 +16,7 @@ requires @["docopt", "jester"]
requires "https://git.jdb-software.com/jdb/nim-cli-utils.git >= 0.6.3"
requires "https://git.jdb-software.com/jdb/nim-time-utils.git >= 0.5.0"
requires "https://git.jdb-software.com/jdb/update-nim-package-version.git"
requires "https://git.jdb-software.com/hope-family-fellowship/notion_utils.git"
requires "https://git.jdb-software.com/hope-family-fellowship/notion-api-client.git"
task updateVersion, "Update the version of this package.":
exec "update_nim_package_version hff_entry_forms_api 'src/hff_entry_forms_apipkg/version.nim'"

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