diff --git a/api/Dockerfile b/api/Dockerfile index 5380367..5339f9d 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -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 \ diff --git a/api/Makefile b/api/Makefile index 21a5e5b..9c4d2b7 100644 --- a/api/Makefile +++ b/api/Makefile @@ -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 diff --git a/api/hff_entry_forms_api.nimble b/api/hff_entry_forms_api.nimble index 7d0f1dc..de97ce1 100644 --- a/api/hff_entry_forms_api.nimble +++ b/api/hff_entry_forms_api.nimble @@ -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'" diff --git a/api/src/hff_entry_forms_apipkg/api.nim b/api/src/hff_entry_forms_apipkg/api.nim index 17817ac..bca5919 100644 --- a/api/src/hff_entry_forms_apipkg/api.nim +++ b/api/src/hff_entry_forms_apipkg/api.nim @@ -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 = diff --git a/api/src/hff_entry_forms_apipkg/models.nim b/api/src/hff_entry_forms_apipkg/models.nim index bff2f17..abb9f19 100644 --- a/api/src/hff_entry_forms_apipkg/models.nim +++ b/api/src/hff_entry_forms_apipkg/models.nim @@ -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), diff --git a/api/src/hff_entry_forms_apipkg/notion_client.nim b/api/src/hff_entry_forms_apipkg/notion_client.nim index 1ca115c..a3ad1af 100644 --- a/api/src/hff_entry_forms_apipkg/notion_client.nim +++ b/api/src/hff_entry_forms_apipkg/notion_client.nim @@ -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