-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: hassu 1012 maakunnat haussa (#365)
* maakunta-api ja sen käyttö fe:ssä * Ota huomioon ruotsinkielisyys Co-authored-by: Valhe Kouneli <valhe.kouneli@cgi.com>
- Loading branch information
1 parent
40bceba
commit 98b546c
Showing
6 changed files
with
125 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import { S3Cache } from "../../../backend/src/cache/s3Cache"; | ||
import log from "loglevel"; | ||
import { setupLambdaMonitoring, wrapXrayAsync } from "../../../backend/src/aws/monitoring"; | ||
|
||
const MAAKUNTALISTA_TTL_SECONDS = 24 * 3 * 60 * 60; | ||
|
||
export type MaakuntaListaOption = { | ||
label: string; | ||
labelRuo: string; | ||
value: string; | ||
}; | ||
|
||
async function fetchMaakuntaLista() { | ||
const response = await fetch("http://rajapinnat.ymparisto.fi/api/Hakemistorajapinta/1.0/odata/Maakunta"); | ||
|
||
const data: any = await response.json(); | ||
const list = data?.value?.map((maakunta: any) => { | ||
return { label: maakunta.Nimi, labelRuo: maakunta.NimiRuo, value: maakunta.Nimi.toUpperCase() }; | ||
}); | ||
list.unshift({ label: "", value: "" }); | ||
return list; | ||
} | ||
|
||
export default async function handler(_req: NextApiRequest, res: NextApiResponse) { | ||
setupLambdaMonitoring(); | ||
return await wrapXrayAsync("handler", async () => { | ||
const s3Cache = new S3Cache(); | ||
const kuntaList: Record<string, string> = await s3Cache.get( | ||
"maakuntalista", | ||
MAAKUNTALISTA_TTL_SECONDS * 1000, | ||
async () => { | ||
log.info("Updating maakuntalista, it has been expired"); | ||
const list = await fetchMaakuntaLista(); | ||
s3Cache.put("kuntalista", list); | ||
}, | ||
async () => { | ||
log.info("Updating maakuntalista, it is missing"); | ||
const list = await fetchMaakuntaLista(); | ||
s3Cache.put("kuntalista", list); | ||
return list; | ||
} | ||
); | ||
|
||
res.setHeader( | ||
"Cache-Control", | ||
"public, s-maxage=" + MAAKUNTALISTA_TTL_SECONDS + ", stale-while-revalidate=" + (MAAKUNTALISTA_TTL_SECONDS - 30) | ||
); | ||
res.status(200).json(kuntaList); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters