From c5e6b11e115ba41c114f05cc3a1bd42e8f8e7769 Mon Sep 17 00:00:00 2001 From: hongwei Date: Tue, 20 Jan 2026 12:39:02 +0100 Subject: [PATCH] refactor/(api): centralize API info properties in APIUtil - Introduced centralized properties for hosted organization details, including email, phone, and website. - Updated JSONFactory classes to utilize the new centralized properties instead of direct property retrieval. - Simplified API info JSON generation by reducing redundancy in property access. - Enhanced clarity and maintainability of API information retrieval across different API versions. --- .../main/scala/code/api/util/APIUtil.scala | 11 +++++ .../code/api/v4_0_0/JSONFactory4.0.0.scala | 18 ++++---- .../code/api/v5_1_0/JSONFactory5.1.0.scala | 18 ++++---- .../scala/code/api/v7_0_0/Http4s700.scala | 4 +- .../code/api/v7_0_0/JSONFactory7.0.0.scala | 41 +++++++------------ 5 files changed, 45 insertions(+), 47 deletions(-) diff --git a/obp-api/src/main/scala/code/api/util/APIUtil.scala b/obp-api/src/main/scala/code/api/util/APIUtil.scala index 1847a4e70..41fb39da4 100644 --- a/obp-api/src/main/scala/code/api/util/APIUtil.scala +++ b/obp-api/src/main/scala/code/api/util/APIUtil.scala @@ -331,6 +331,17 @@ object APIUtil extends MdcLoggable with CustomJsonFormats{ commit } + // API info props helpers (keep values centralized) + lazy val hostedByOrganisation: String = getPropsValue("hosted_by.organisation", "TESOBE") + lazy val hostedByEmail: String = getPropsValue("hosted_by.email", "contact@tesobe.com") + lazy val hostedByPhone: String = getPropsValue("hosted_by.phone", "+49 (0)30 8145 3994") + lazy val organisationWebsite: String = getPropsValue("organisation_website", "https://www.tesobe.com") + lazy val hostedAtOrganisation: String = getPropsValue("hosted_at.organisation", "") + lazy val hostedAtOrganisationWebsite: String = getPropsValue("hosted_at.organisation_website", "") + lazy val energySourceOrganisation: String = getPropsValue("energy_source.organisation", "") + lazy val energySourceOrganisationWebsite: String = getPropsValue("energy_source.organisation_website", "") + lazy val resourceDocsRequiresRole: Boolean = getPropsAsBoolValue("resource_docs_requires_role", false) + /** * Caching of unchanged resources diff --git a/obp-api/src/main/scala/code/api/v4_0_0/JSONFactory4.0.0.scala b/obp-api/src/main/scala/code/api/v4_0_0/JSONFactory4.0.0.scala index 75aa0bd5f..5ef812c7d 100644 --- a/obp-api/src/main/scala/code/api/v4_0_0/JSONFactory4.0.0.scala +++ b/obp-api/src/main/scala/code/api/v4_0_0/JSONFactory4.0.0.scala @@ -1095,22 +1095,22 @@ case class JsonCodeTemplateJson( object JSONFactory400 { def getApiInfoJSON(apiVersion : ApiVersion, apiVersionStatus : String) = { - val organisation = APIUtil.getPropsValue("hosted_by.organisation", "TESOBE") - val email = APIUtil.getPropsValue("hosted_by.email", "contact@tesobe.com") - val phone = APIUtil.getPropsValue("hosted_by.phone", "+49 (0)30 8145 3994") - val organisationWebsite = APIUtil.getPropsValue("organisation_website", "https://www.tesobe.com") + val organisation = APIUtil.hostedByOrganisation + val email = APIUtil.hostedByEmail + val phone = APIUtil.hostedByPhone + val organisationWebsite = APIUtil.organisationWebsite val hostedBy = new HostedBy400(organisation, email, phone, organisationWebsite) - val organisationHostedAt = APIUtil.getPropsValue("hosted_at.organisation", "") - val organisationWebsiteHostedAt = APIUtil.getPropsValue("hosted_at.organisation_website", "") + val organisationHostedAt = APIUtil.hostedAtOrganisation + val organisationWebsiteHostedAt = APIUtil.hostedAtOrganisationWebsite val hostedAt = new HostedAt400(organisationHostedAt, organisationWebsiteHostedAt) - val organisationEnergySource = APIUtil.getPropsValue("energy_source.organisation", "") - val organisationWebsiteEnergySource = APIUtil.getPropsValue("energy_source.organisation_website", "") + val organisationEnergySource = APIUtil.energySourceOrganisation + val organisationWebsiteEnergySource = APIUtil.energySourceOrganisationWebsite val energySource = new EnergySource400(organisationEnergySource, organisationWebsiteEnergySource) val connector = code.api.Constant.CONNECTOR.openOrThrowException(s"$MandatoryPropertyIsNotSet. The missing prop is `connector` ") - val resourceDocsRequiresRole = APIUtil.getPropsAsBoolValue("resource_docs_requires_role", false) + val resourceDocsRequiresRole = APIUtil.resourceDocsRequiresRole APIInfoJson400( apiVersion.vDottedApiVersion, diff --git a/obp-api/src/main/scala/code/api/v5_1_0/JSONFactory5.1.0.scala b/obp-api/src/main/scala/code/api/v5_1_0/JSONFactory5.1.0.scala index a5f01717b..f1f36add9 100644 --- a/obp-api/src/main/scala/code/api/v5_1_0/JSONFactory5.1.0.scala +++ b/obp-api/src/main/scala/code/api/v5_1_0/JSONFactory5.1.0.scala @@ -1049,22 +1049,22 @@ object JSONFactory510 extends CustomJsonFormats with MdcLoggable { } def getApiInfoJSON(apiVersion : ApiVersion, apiVersionStatus: String) = { - val organisation = APIUtil.getPropsValue("hosted_by.organisation", "TESOBE") - val email = APIUtil.getPropsValue("hosted_by.email", "contact@tesobe.com") - val phone = APIUtil.getPropsValue("hosted_by.phone", "+49 (0)30 8145 3994") - val organisationWebsite = APIUtil.getPropsValue("organisation_website", "https://www.tesobe.com") + val organisation = APIUtil.hostedByOrganisation + val email = APIUtil.hostedByEmail + val phone = APIUtil.hostedByPhone + val organisationWebsite = APIUtil.organisationWebsite val hostedBy = new HostedBy400(organisation, email, phone, organisationWebsite) - val organisationHostedAt = APIUtil.getPropsValue("hosted_at.organisation", "") - val organisationWebsiteHostedAt = APIUtil.getPropsValue("hosted_at.organisation_website", "") + val organisationHostedAt = APIUtil.hostedAtOrganisation + val organisationWebsiteHostedAt = APIUtil.hostedAtOrganisationWebsite val hostedAt = HostedAt400(organisationHostedAt, organisationWebsiteHostedAt) - val organisationEnergySource = APIUtil.getPropsValue("energy_source.organisation", "") - val organisationWebsiteEnergySource = APIUtil.getPropsValue("energy_source.organisation_website", "") + val organisationEnergySource = APIUtil.energySourceOrganisation + val organisationWebsiteEnergySource = APIUtil.energySourceOrganisationWebsite val energySource = EnergySource400(organisationEnergySource, organisationWebsiteEnergySource) val connector = code.api.Constant.CONNECTOR.openOrThrowException(s"$MandatoryPropertyIsNotSet. The missing prop is `connector` ") - val resourceDocsRequiresRole = APIUtil.getPropsAsBoolValue("resource_docs_requires_role", false) + val resourceDocsRequiresRole = APIUtil.resourceDocsRequiresRole APIInfoJsonV510( version = apiVersion.vDottedApiVersion, diff --git a/obp-api/src/main/scala/code/api/v7_0_0/Http4s700.scala b/obp-api/src/main/scala/code/api/v7_0_0/Http4s700.scala index dd064abff..bc600a50a 100644 --- a/obp-api/src/main/scala/code/api/v7_0_0/Http4s700.scala +++ b/obp-api/src/main/scala/code/api/v7_0_0/Http4s700.scala @@ -77,7 +77,7 @@ object Http4s700 { val root: HttpRoutes[IO] = HttpRoutes.of[IO] { case req @ GET -> `prefixPath` / "root" => val responseJson = convertAnyToJsonString( - JSONFactory700.getApiInfoJSON(implementedInApiVersion, s"Hello") + JSONFactory700.getApiInfoJSON(implementedInApiVersion, versionStatus) ) Ok(responseJson).map(_.withContentType(jsonContentType)) @@ -111,7 +111,7 @@ object Http4s700 { case req @ GET -> `prefixPath` / "banks" => val responseJson = convertAnyToJsonString( - JSONFactory700.getApiInfoJSON(implementedInApiVersion, s"Hello ") + JSONFactory700.getApiInfoJSON(implementedInApiVersion, versionStatus) ) Ok(responseJson).map(_.withContentType(jsonContentType)) } diff --git a/obp-api/src/main/scala/code/api/v7_0_0/JSONFactory7.0.0.scala b/obp-api/src/main/scala/code/api/v7_0_0/JSONFactory7.0.0.scala index a675842e6..8bb51db93 100644 --- a/obp-api/src/main/scala/code/api/v7_0_0/JSONFactory7.0.0.scala +++ b/obp-api/src/main/scala/code/api/v7_0_0/JSONFactory7.0.0.scala @@ -6,20 +6,9 @@ import code.api.util.ErrorMessages.MandatoryPropertyIsNotSet import code.api.v4_0_0.{EnergySource400, HostedAt400, HostedBy400} import code.util.Helper.MdcLoggable import com.openbankproject.commons.util.ApiVersion -import net.liftweb.util.Props object JSONFactory700 extends MdcLoggable { - // Get git commit from build info - lazy val gitCommit: String = { - val commit = try { - Props.get("git.commit.id", "unknown") - } catch { - case _: Throwable => "unknown" - } - commit - } - case class APIInfoJsonV700( version: String, version_status: String, @@ -31,32 +20,31 @@ object JSONFactory700 extends MdcLoggable { hosted_by: HostedBy400, hosted_at: HostedAt400, energy_source: EnergySource400, - resource_docs_requires_role: Boolean, - message: String + resource_docs_requires_role: Boolean ) - def getApiInfoJSON(apiVersion: ApiVersion, message: String): APIInfoJsonV700 = { - val organisation = APIUtil.getPropsValue("hosted_by.organisation", "TESOBE") - val email = APIUtil.getPropsValue("hosted_by.email", "contact@tesobe.com") - val phone = APIUtil.getPropsValue("hosted_by.phone", "+49 (0)30 8145 3994") - val organisationWebsite = APIUtil.getPropsValue("organisation_website", "https://www.tesobe.com") + def getApiInfoJSON(apiVersion: ApiVersion, apiVersionStatus: String): APIInfoJsonV700 = { + val organisation = APIUtil.hostedByOrganisation + val email = APIUtil.hostedByEmail + val phone = APIUtil.hostedByPhone + val organisationWebsite = APIUtil.organisationWebsite val hostedBy = new HostedBy400(organisation, email, phone, organisationWebsite) - val organisationHostedAt = APIUtil.getPropsValue("hosted_at.organisation", "") - val organisationWebsiteHostedAt = APIUtil.getPropsValue("hosted_at.organisation_website", "") + val organisationHostedAt = APIUtil.hostedAtOrganisation + val organisationWebsiteHostedAt = APIUtil.hostedAtOrganisationWebsite val hostedAt = HostedAt400(organisationHostedAt, organisationWebsiteHostedAt) - val organisationEnergySource = APIUtil.getPropsValue("energy_source.organisation", "") - val organisationWebsiteEnergySource = APIUtil.getPropsValue("energy_source.organisation_website", "") + val organisationEnergySource = APIUtil.energySourceOrganisation + val organisationWebsiteEnergySource = APIUtil.energySourceOrganisationWebsite val energySource = EnergySource400(organisationEnergySource, organisationWebsiteEnergySource) val connector = code.api.Constant.CONNECTOR.openOrThrowException(s"$MandatoryPropertyIsNotSet. The missing prop is `connector` ") - val resourceDocsRequiresRole = APIUtil.getPropsAsBoolValue("resource_docs_requires_role", false) + val resourceDocsRequiresRole = APIUtil.resourceDocsRequiresRole APIInfoJsonV700( version = apiVersion.vDottedApiVersion, - version_status = "BLEEDING_EDGE", - git_commit = gitCommit, + version_status = apiVersionStatus, + git_commit = APIUtil.gitCommit, connector = connector, hostname = Constant.HostName, stage = System.getProperty("run.mode"), @@ -64,8 +52,7 @@ object JSONFactory700 extends MdcLoggable { hosted_by = hostedBy, hosted_at = hostedAt, energy_source = energySource, - resource_docs_requires_role = resourceDocsRequiresRole, - message = message + resource_docs_requires_role = resourceDocsRequiresRole ) } }