Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Implement APIs for palveluväylä #1043

Merged
merged 6 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion etp-backend/deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
prismatic/schema-generators {:mvn/version "0.1.5"}
ring/ring-mock {:mvn/version "0.4.0"}
org.xmlunit/xmlunit-core {:mvn/version "2.9.1"}}
:jvm-opts ["-Djava.awt.headless=true"]}
:jvm-opts ["-Djava.awt.headless=true" "-DALLOW_PALVELUVAYLA_API=true"]}
:test {:main-opts ["-e" "(run-tests-and-exit!)" "-A:dev"]}
:test-ci {:main-opts ["-e" "(run-tests-with-junit-reporter-and-exit!)" "-A:dev"]}
:coverage {:extra-deps {cloverage/cloverage {:mvn/version "1.2.4"}}
Expand Down
63 changes: 50 additions & 13 deletions etp-backend/src/main/clj/solita/etp/api/palveluvayla.clj
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
(ns solita.etp.api.palveluvayla
(:require [schema.core :as s]
[solita.etp.api.response :as api-response]
[solita.etp.schema.common :as schema.common]
[solita.etp.schema.energiatodistus :as schema.energiatodistus]))
[solita.etp.schema.energiatodistus :as schema.energiatodistus]
[solita.etp.service.palveluvayla :as service.palveluvayla]))

(def accept-language-header {(s/optional-key :accept-language) schema.common/AcceptLanguage})

(defn parse-preferred-language-order
"Sort Accept-Language headers by quality and return only the languages tags in order of preference"
[accept-language]
(some->> accept-language
(sort-by second)
(reverse)
(map first)))

(def routes ["/energiatodistukset"
["/pdf/:id"
["" {:get {:summary "Hae PDF-muotoinen energiatodistus tunnuksen id:llä"
:parameters {:path {:id schema.common/Key}
:header accept-language-header}
:responses {200 {:body nil}
404 {:body s/Str}}
:handler (constantly {:status 200})
:handler (fn [{:keys [db aws-s3-client],
{{:keys [accept-language]} :header} :parameters
{{:keys [id]} :path} :parameters}]
(let [language-preference-order (parse-preferred-language-order accept-language)]
(api-response/pdf-response ; Return the first language version that exists if any
(service.palveluvayla/find-first-existing-pdf id language-preference-order db aws-s3-client)
"energiatodistus.pdf"
(str "Energiatodistus " id " does not exists."))))
:openapi {:responses {200 {:description "PDF-muotoinen energiatodistus"
:content {:application/pdf {:schema {:type "string"
:format "binary"}}}}}}}}]]
Expand All @@ -21,27 +38,47 @@
["" {:get {:summary "Hae energiatodistuksia json-muodossa. Palauttaa jaetut kentät sekä 2013, että 2018 lain mukaisista energiatodistuksista"
:parameters {:query {:rakennustunnus schema.common/Rakennustunnus}}
:responses {200 {:body [schema.energiatodistus/EnergiatodistusForAnyLaatija]}}
:handler (constantly {:status 200})}}]
:handler (fn [{{:keys [query]} :parameters :keys [db]}]
(api-response/get-response
(service.palveluvayla/search-by-rakennustunnus (:rakennustunnus query) schema.energiatodistus/EnergiatodistusForAnyLaatija db)
(str "Virhe haussa")))}}]
["/:id" {:get {:summary "Hae yksittäinen energiatodistus todistuksen tunnuksen perusteella. Vastaus sisältää vain kentät jotka ovat yhteisiä 2013 ja 2018 versioille."
:parameters {:path {:id schema.common/Key}}
:responses {200 {:body [schema.energiatodistus/EnergiatodistusForAnyLaatija]}
:responses {200 {:body schema.energiatodistus/EnergiatodistusForAnyLaatija}
404 {:body s/Str}}
:handler (constantly {:status 200})}}]]
:handler (fn [{{{:keys [id]} :path} :parameters :keys [db]}]
(api-response/get-response
(service.palveluvayla/get-by-id id db)
(str "Energiatodistus " id " does not exists.")))}}]]
["/2013"
["" {:get {:summary "Hae energiatodistuksia, jotka on laadittu vuoden 2013 säännösten mukaan"
:responses {200 {:body [schema.energiatodistus/Energiatodistus2013]}}
:handler (constantly {:status 200})}}]
["" {:get {:summary "Hae energiatodistuksia, jotka on laadittu vuoden 2013 säännösten mukaan"
:parameters {:query {:rakennustunnus schema.common/Rakennustunnus}}
:responses {200 {:body [schema.energiatodistus/Energiatodistus2013]}}
:handler (fn [{{:keys [query]} :parameters :keys [db]}]
(api-response/get-response
(service.palveluvayla/search-by-rakennustunnus (:rakennustunnus query) schema.energiatodistus/Energiatodistus2013 db 2013)
(str "Virhe haussa")))}}]
["/:id" {:get {:summary "Hae yksittäinen vuoden 2013 säännösten mukainen energiatodistus todistuksen tunnuksen perusteella"
:parameters {:path {:id schema.common/Key}}
:responses {200 {:body schema.energiatodistus/Energiatodistus2013}
404 {:body s/Str}}
:handler (constantly {:status 200})}}]]
:handler (fn [{{{:keys [id]} :path} :parameters :keys [db]}]
(api-response/get-response
(service.palveluvayla/get-by-id id db 2013)
(str "Energiatodistus " id " does not exists.")))}}]]
["/2018"
["" {:get {:summary "Hae energiatodistuksia, jotka on laadittu vuoden 2018 säännösten mukaan"
:responses {200 {:body [schema.energiatodistus/Energiatodistus2018]}}
:handler (constantly {:status 200})}}]
["" {:get {:summary "Hae energiatodistuksia, jotka on laadittu vuoden 2018 säännösten mukaan"
:parameters {:query {:rakennustunnus schema.common/Rakennustunnus}}
:responses {200 {:body [schema.energiatodistus/Energiatodistus2018]}}
:handler (fn [{{:keys [query]} :parameters :keys [db]}]
(api-response/get-response
(service.palveluvayla/search-by-rakennustunnus (:rakennustunnus query) schema.energiatodistus/Energiatodistus2018 db 2018)
(str "Virhe haussa")))}}]
["/:id" {:get {:summary "Hae yksittäinen vuoden 2018 säännösten mukainen energiatodistus todistuksen tunnuksen perusteella"
:parameters {:path {:id schema.common/Key}}
:responses {200 {:body schema.energiatodistus/Energiatodistus2018}
404 {:body s/Str}}
:handler (constantly {:status 200})}}]]]])
:handler (fn [{{{:keys [id]} :path} :parameters :keys [db]}]
(api-response/get-response
(service.palveluvayla/get-by-id id db 2018)
(str "Energiatodistus " id " does not exists.")))}}]]]])
58 changes: 35 additions & 23 deletions etp-backend/src/main/clj/solita/etp/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@
(def use-local-env-credentials?
(not (System/getenv "FILES_BUCKET_NAME")))

(defn env [name default]
(or (System/getenv name) default))
(defn find-configuration
"Return configuration value for given key. Returns first hit in order from given reader"
([key readers default]
(-> (some identity (->> readers (map #(% key)))) (or default))))

(defn env
([name] (env name nil))
([name default] (or (System/getenv name) default)))

(defn property
[name] (System/getProperty name))

(defn env-or-resource [env-name resource-path]
(or (System/getenv env-name)
Expand All @@ -24,40 +33,40 @@
(defn db
([] (db {}))
([opts]
{:solita.etp/db (merge {:adapter "postgresql"
:server-name (env "DB_HOST" "localhost")
:port-number (env "DB_PORT" 5432)
:username (env "DB_USER" "etp_app")
:password (env "DB_PASSWORD" "etp")
:database-name (env "DB_DATABASE" "etp_dev")
{:solita.etp/db (merge {:adapter "postgresql"
:server-name (env "DB_HOST" "localhost")
:port-number (env "DB_PORT" 5432)
:username (env "DB_USER" "etp_app")
:password (env "DB_PASSWORD" "etp")
:database-name (env "DB_DATABASE" "etp_dev")
:current-schema (env "DB_SCHEMA" "etp")}
opts)}))

(defn http-server
([] (http-server {}))
([opts]
{:solita.etp/http-server (merge {:port (env "HTTP_SERVER_PORT" 8080)
{:solita.etp/http-server (merge {:port (env "HTTP_SERVER_PORT" 8080)
:max-body (* 1024 1024 50)
:thread 20
:ctx {:db (ig/ref :solita.etp/db)
:aws-s3-client (ig/ref :solita.etp/aws-s3-client)}}
:thread 20
:ctx {:db (ig/ref :solita.etp/db)
:aws-s3-client (ig/ref :solita.etp/aws-s3-client)}}
opts)}))

(defn aws-s3-client
([] (aws-s3-client {}))
([{:keys [client bucket]}]
{:solita.etp/aws-s3-client
{:client (merge
{:api :s3
:region "eu-central-1"}
(when use-local-env-credentials?
{:credentials-provider (credentials/basic-credentials-provider
{:access-key-id "minio"
:secret-access-key "minio123"})
:endpoint-override {:protocol :http
:hostname "localhost"
:port 9000}})
client)
{:api :s3
:region "eu-central-1"}
(when use-local-env-credentials?
{:credentials-provider (credentials/basic-credentials-provider
{:access-key-id "minio"
:secret-access-key "minio123"})
:endpoint-override {:protocol :http
:hostname "localhost"
:port 9000}})
client)
:bucket (or bucket (env "FILES_BUCKET_NAME" "files"))}}))

(defn- prepare-emails [name default]
Expand Down Expand Up @@ -121,7 +130,7 @@

(def suomifi-viestit-proxy? (edn/read-string (env "SUOMIFI_VIESTIT_PROXY" "false")))
(def suomifi-viestit-paperitoimitus? (edn/read-string (env "SUOMIFI_VIESTIT_PAPERITOIMITUS" "false")))
(def suomifi-viestit-laheta-tulostukseen? (edn/read-string (env "SUOMIFI_VIESTIT_LAHETA_TULOSTUKSEEN" "false")))
(def suomifi-viestit-laheta-tulostukseen? (edn/read-string (env "SUOMIFI_VIESTIT_LAHETA_TULOSTUKSEEN" "false")))
(def suomifi-viestit-endpoint-url (env "SUOMIFI_VIESTIT_ENDPOINT_URL" nil))
(def suomifi-viestit-viranomaistunnus (env "SUOMIFI_VIESTIT_VIRANOMAISTUNNUS" nil))
(def suomifi-viestit-palvelutunnus (env "SUOMIFI_VIESTIT_PALVELUTUNNUS" nil))
Expand All @@ -139,3 +148,6 @@
(def url-signing-key-id (env "URL_SIGNING_KEY_ID" "DEVENV_KEY_ID"))
(def url-signing-public-key (env-or-resource "URL_SIGNING_PUBLIC_KEY" "cf-signed-url/example.pub.pem"))
(def url-signing-private-key (env-or-resource "URL_SIGNING_PRIVATE_KEY" "cf-signed-url/example.key.pem"))

;; Feature flags
(def allow-palveluvayla-api (Boolean/parseBoolean (find-configuration "ALLOW_PALVELUVAYLA_API" [env property] "false")))
10 changes: 5 additions & 5 deletions etp-backend/src/main/clj/solita/etp/handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@
["/internal"
(concat (tag "Laskutus API" laskutus-api/routes)
(tag "Laatija Internal API" laatija-api/internal-routes))]
["/palveluvayla" ["/openapi.json" {:get {:no-doc true
:openapi {:info {:title "Energiatodistuspalvelu API" :description "Hae energiatodistuksia pdf tai json muodoissa"}
:id "Palveluväylä"}
:handler (openapi/create-openapi-handler)}}]
(openapi-id "Palveluväylä" palveluvayla/routes)]])
(when config/allow-palveluvayla-api
["/palveluvayla" ["/openapi.json" {:get {:no-doc true :openapi {:info {:title "Energiatodistuspalvelu API" :description "Hae energiatodistuksia pdf tai json muodoissa"}
:id "Palveluväylä"}
:handler (openapi/create-openapi-handler)}}]
(openapi-id "Palveluväylä" palveluvayla/routes)])])

(def default-string-coercion-options-with-project-specific-ones
"Add more schemas that support coercion to default configuration in addition to those supported by schema coercion out of the box"
Expand Down
3 changes: 2 additions & 1 deletion etp-backend/src/main/clj/solita/etp/schema/common.clj
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
(schema/defschema WeightedLocale
"A single locale with weight for Accept-Language header"
[(schema/one (schema/constrained schema/Str #(re-matches #"(?i)([*]|[a-z]{2,3})" %)) "lang") (schema/one (schema/constrained schema/Num #(and (>= % 0) (<= % 1))) "weight")])

(schema/defschema AcceptLanguage
"Define a schema for Accept-Language header. The header can contain multiple locales with preferred weights, for example Accept-Language: fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5"
[WeightedLocale])
Expand All @@ -217,4 +218,4 @@
2 [(-> parts first parse-lang) (Double/parseDouble (second parts))])))

(defn parse-accept-language [s]
(map parse-locale (str/split s #",")))
(map parse-locale (map str/trim (str/split s #","))))
Loading