From 4e82c66cadb230cf372a7cfeeb43dbac8136bb3e Mon Sep 17 00:00:00 2001 From: hongwei Date: Mon, 30 Oct 2023 16:48:34 +0100 Subject: [PATCH] refactor/added createLocalisedResourceDocJsonCached --- .../resources/props/sample.props.template | 3 +- .../ResourceDocsAPIMethods.scala | 2 +- .../main/scala/code/api/util/APIUtil.scala | 3 ++ .../code/api/v1_4_0/JSONFactory1_4_0.scala | 51 +++++++++---------- .../scala/code/api/v4_0_0/APIMethods400.scala | 25 +++++++-- .../api/v1_4_0/JSONFactory1_4_0Test.scala | 2 +- release_notes.md | 2 +- 7 files changed, 53 insertions(+), 35 deletions(-) diff --git a/obp-api/src/main/resources/props/sample.props.template b/obp-api/src/main/resources/props/sample.props.template index 9f5a199bd..a89727201 100644 --- a/obp-api/src/main/resources/props/sample.props.template +++ b/obp-api/src/main/resources/props/sample.props.template @@ -95,7 +95,8 @@ connectorMethod.cache.ttl.seconds=40 ## this value also represent how many seconds before the new endpoints will be shown after upload a new DynamicEntity. ## So if you want the new endpoints shown timely, set this value to a small number. dynamicResourceDocsObp.cache.ttl.seconds=3600 -staticResourceDocsObp.cache.ttl.seconds=86400 +staticResourceDocsObp.cache.ttl.seconds=3600 +createLocalisedResourceDocJson.cache.ttl.seconds=3600 ## This can change the behavior of `Get Resource Docs`/`Get API Glossary`. If we set it to `true`, OBP will check the authentication and CanReadResourceDoc/CanReadGlossary Role # the default value is false, so the `Get Resource Docs`/`Get API Glossary` is anonymous as default. diff --git a/obp-api/src/main/scala/code/api/ResourceDocs1_4_0/ResourceDocsAPIMethods.scala b/obp-api/src/main/scala/code/api/ResourceDocs1_4_0/ResourceDocsAPIMethods.scala index 79e800d09..a042344fc 100644 --- a/obp-api/src/main/scala/code/api/ResourceDocs1_4_0/ResourceDocsAPIMethods.scala +++ b/obp-api/src/main/scala/code/api/ResourceDocs1_4_0/ResourceDocsAPIMethods.scala @@ -225,7 +225,7 @@ trait ResourceDocsAPIMethods extends MdcLoggable with APIMethods220 with APIMeth // if upload DynamicEntity, will generate corresponding endpoints, when current cache timeout, the new endpoints will be shown. // so if you want the new generated endpoints shown timely, set this value to a small number, or set to a big number val getDynamicResourceDocsTTL : Int = APIUtil.getPropsValue(s"dynamicResourceDocsObp.cache.ttl.seconds", "3600").toInt - val getStaticResourceDocsTTL : Int = APIUtil.getPropsValue(s"staticResourceDocsObp.cache.ttl.seconds", "86400").toInt + val getStaticResourceDocsTTL : Int = APIUtil.getPropsValue(s"staticResourceDocsObp.cache.ttl.seconds", "3600").toInt /** * 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 06c6cb106..b73d1f132 100644 --- a/obp-api/src/main/scala/code/api/util/APIUtil.scala +++ b/obp-api/src/main/scala/code/api/util/APIUtil.scala @@ -4863,4 +4863,7 @@ object APIUtil extends MdcLoggable with CustomJsonFormats{ jField => (jField.name, jField.value.asInstanceOf[JString].s) ) } + + val createLocalisedResourceDocJsonTTL : Int = APIUtil.getPropsValue(s"createLocalisedResourceDocJson.cache.ttl.seconds", "3600").toInt + } diff --git a/obp-api/src/main/scala/code/api/v1_4_0/JSONFactory1_4_0.scala b/obp-api/src/main/scala/code/api/v1_4_0/JSONFactory1_4_0.scala index a365e49d5..a77e2bbf1 100644 --- a/obp-api/src/main/scala/code/api/v1_4_0/JSONFactory1_4_0.scala +++ b/obp-api/src/main/scala/code/api/v1_4_0/JSONFactory1_4_0.scala @@ -3,8 +3,7 @@ package code.api.v1_4_0 import code.api.berlin.group.v1_3.JvalueCaseClass import code.api.cache.Caching import java.util.Date - -import code.api.util.APIUtil.{EmptyBody, PrimaryDataBody, ResourceDoc} +import code.api.util.APIUtil.{EmptyBody, PrimaryDataBody, ResourceDoc, createLocalisedResourceDocJsonTTL} import code.api.util.ApiTag.ResourceDocTag import code.api.util.Glossary.glossaryItems import code.api.util.{APIUtil, ApiRole, ConnectorField, CustomJsonFormats, ExampleValue, I18NUtil, PegdownOptions} @@ -518,17 +517,15 @@ object JSONFactory1_4_0 extends MdcLoggable{ jsonFieldsDescription.mkString(jsonTitleType,"","\n") } - - val createResourceDocJsonTTL : Int = APIUtil.getPropsValue(s"createResourceDocJson.cache.ttl.seconds", "86400").toInt - - def createResourceDocJsonCached( + //cache key will only contain "operationId + locale" + def createLocalisedResourceDocJsonCached( + operationId: String, // this will be in the cacheKey + locale: Option[String],// this will be in the cacheKey @cacheKeyExclude resourceDocUpdatedTags: ResourceDoc, @cacheKeyExclude isVersion4OrHigher:Boolean, - locale: Option[String], - urlParametersI18n:String , - jsonRequestBodyFieldsI18n:String, - jsonResponseBodyFieldsI18n:String, - cacheKey:String + @cacheKeyExclude urlParametersI18n:String , + @cacheKeyExclude jsonRequestBodyFieldsI18n:String, + @cacheKeyExclude jsonResponseBodyFieldsI18n:String ): ResourceDocJson = { /** * Please note that "var cacheKey = (randomUUID().toString, randomUUID().toString, randomUUID().toString)" @@ -538,7 +535,7 @@ object JSONFactory1_4_0 extends MdcLoggable{ */ var cacheKey = (randomUUID().toString, randomUUID().toString, randomUUID().toString) CacheKeyFromArguments.buildCacheKey { - Caching.memoizeSyncWithProvider(Some(cacheKey.toString()))(createResourceDocJsonTTL second) { + Caching.memoizeSyncWithProvider(Some(cacheKey.toString()))(createLocalisedResourceDocJsonTTL second) { // There are multiple flavours of markdown. For instance, original markdown emphasises underscores (surrounds _ with ()) // But we don't want to have to escape underscores (\_) in our documentation // Thus we use a flavour of markdown that ignores underscores in words. (Github markdown does this too) @@ -607,21 +604,21 @@ object JSONFactory1_4_0 extends MdcLoggable{ } } - def createResourceDocJson(rd: ResourceDoc, isVersion4OrHigher:Boolean, locale: Option[String], urlParametersI18n:String ,jsonRequestBodyFieldsI18n:String, jsonResponseBodyFieldsI18n:String) : ResourceDocJson = { - // We MUST recompute all resource doc values due to translation via Web UI props - val endpointTags = getAllEndpointTagsBox(rd.operationId).map(endpointTag =>ResourceDocTag(endpointTag.tagName)) - val updatedTagsResourceDoc: ResourceDoc = rd.copy(tags = endpointTags++ rd.tags) - val cacheKey = updatedTagsResourceDoc.operationId + updatedTagsResourceDoc.tags + updatedTagsResourceDoc + def createLocalisedResourceDocJson(rd: ResourceDoc, isVersion4OrHigher:Boolean, locale: Option[String], urlParametersI18n:String ,jsonRequestBodyFieldsI18n:String, jsonResponseBodyFieldsI18n:String) : ResourceDocJson = { + // We MUST recompute all resource doc values due to translation via Web UI props --> now need to wait $createLocalisedResourceDocJsonTTL seconds + val userDefinedEndpointTags = getAllEndpointTagsBox(rd.operationId).map(endpointTag =>ResourceDocTag(endpointTag.tagName)) + val resourceDocWithUserDefinedEndpointTags: ResourceDoc = rd.copy(tags = userDefinedEndpointTags++ rd.tags) - createResourceDocJsonCached( - updatedTagsResourceDoc, - isVersion4OrHigher:Boolean, - locale: Option[String], - urlParametersI18n:String, - jsonRequestBodyFieldsI18n:String, - jsonResponseBodyFieldsI18n:String, - cacheKey:String + createLocalisedResourceDocJsonCached( + resourceDocWithUserDefinedEndpointTags.operationId, + locale: Option[String], + resourceDocWithUserDefinedEndpointTags, + isVersion4OrHigher: Boolean, + urlParametersI18n: String, + jsonRequestBodyFieldsI18n: String, + jsonResponseBodyFieldsI18n: String ) + } def createResourceDocsJson(resourceDocList: List[ResourceDoc], isVersion4OrHigher:Boolean, locale: Option[String]) : ResourceDocsJson = { @@ -647,11 +644,11 @@ object JSONFactory1_4_0 extends MdcLoggable{ if(isVersion4OrHigher){ ResourceDocsJson( - resourceDocList.map(createResourceDocJson(_,isVersion4OrHigher, locale, urlParametersI18n, jsonRequestBodyFields, jsonResponseBodyFields)), + resourceDocList.map(createLocalisedResourceDocJson(_,isVersion4OrHigher, locale, urlParametersI18n, jsonRequestBodyFields, jsonResponseBodyFields)), meta=Some(ResourceDocMeta(new Date(), resourceDocList.length)) ) } else { - ResourceDocsJson(resourceDocList.map(createResourceDocJson(_,false, locale, urlParametersI18n, jsonRequestBodyFields, jsonResponseBodyFields))) + ResourceDocsJson(resourceDocList.map(createLocalisedResourceDocJson(_,false, locale, urlParametersI18n, jsonRequestBodyFields, jsonResponseBodyFields))) } } diff --git a/obp-api/src/main/scala/code/api/v4_0_0/APIMethods400.scala b/obp-api/src/main/scala/code/api/v4_0_0/APIMethods400.scala index 1aee2019f..cdb00ec12 100644 --- a/obp-api/src/main/scala/code/api/v4_0_0/APIMethods400.scala +++ b/obp-api/src/main/scala/code/api/v4_0_0/APIMethods400.scala @@ -11818,7 +11818,11 @@ trait APIMethods400 { "POST", "/management/endpoints/OPERATION_ID/tags", "Create System Level Endpoint Tag", - s"""Create System Level Endpoint Tag""", + s"""Create System Level Endpoint Tag + | + |Note: Resource Docs are cached, TTL is ${createLocalisedResourceDocJsonTTL} + | + |""".stripMargin, endpointTagJson400, bankLevelEndpointTagResponseJson400, List( @@ -11858,7 +11862,11 @@ trait APIMethods400 { "PUT", "/management/endpoints/OPERATION_ID/tags/ENDPOINT_TAG_ID", "Update System Level Endpoint Tag", - s"""Update System Level Endpoint Tag, you can only update the tag_name here, operation_id can not be updated.""", + s"""Update System Level Endpoint Tag, you can only update the tag_name here, operation_id can not be updated. + | + |Note: Resource Docs are cached, TTL is ${createLocalisedResourceDocJsonTTL} + | + |""".stripMargin, endpointTagJson400, bankLevelEndpointTagResponseJson400, List( @@ -11962,7 +11970,12 @@ trait APIMethods400 { "POST", "/management/banks/BANK_ID/endpoints/OPERATION_ID/tags", "Create Bank Level Endpoint Tag", - s"""Create Bank Level Endpoint Tag""", + s"""Create Bank Level Endpoint Tag + | + |Note: Resource Docs are cached, TTL is ${createLocalisedResourceDocJsonTTL} + | + | + |""".stripMargin, endpointTagJson400, bankLevelEndpointTagResponseJson400, List( @@ -12004,7 +12017,11 @@ trait APIMethods400 { "PUT", "/management/banks/BANK_ID/endpoints/OPERATION_ID/tags/ENDPOINT_TAG_ID", "Update Bank Level Endpoint Tag", - s"""Update Endpoint Tag, you can only update the tag_name here, operation_id can not be updated.""", + s"""Update Endpoint Tag, you can only update the tag_name here, operation_id can not be updated. + | + |Note: Resource Docs are cached, TTL is ${createLocalisedResourceDocJsonTTL} + | + |""".stripMargin, endpointTagJson400, bankLevelEndpointTagResponseJson400, List( diff --git a/obp-api/src/test/scala/code/api/v1_4_0/JSONFactory1_4_0Test.scala b/obp-api/src/test/scala/code/api/v1_4_0/JSONFactory1_4_0Test.scala index 81f3311b2..c5be0d68c 100644 --- a/obp-api/src/test/scala/code/api/v1_4_0/JSONFactory1_4_0Test.scala +++ b/obp-api/src/test/scala/code/api/v1_4_0/JSONFactory1_4_0Test.scala @@ -115,7 +115,7 @@ class JSONFactory1_4_0Test extends V140ServerSetup with DefaultUsers { scenario("createResourceDocJson should work well, no exception is good enough") { val resourceDoc: ResourceDoc = OBPAPI3_0_0.allResourceDocs(5) - val result: ResourceDocJson = JSONFactory1_4_0.createResourceDocJson(resourceDoc,false, None, + val result: ResourceDocJson = JSONFactory1_4_0.createLocalisedResourceDocJson(resourceDoc,false, None, urlParameters, "JSON request body fields:", "JSON response body fields:") } diff --git a/release_notes.md b/release_notes.md index c5e21fc07..9c68e2504 100644 --- a/release_notes.md +++ b/release_notes.md @@ -43,7 +43,7 @@ Date Commit Action Added props: webui_privacy_policy_url, default is https://openbankproject.com/privacy-policy 30/06/2021 cf2dd987 Changed props, static will cache 24 hours, dynamic only 1 hour as default. dynamicResourceDocsObp.cache.ttl.seconds=3600 - staticResourceDocsObp.cache.ttl.seconds=86400 + staticResourceDocsObp.cache.ttl.seconds=3600 30/06/2021 cf2dd987 Added props: email_domain_to_entitlement_mappings, default is empty We can automatically grant the Entitlements required to the User has access to via their validated email domain. Entitlements are generated /refreshed both following manual locin and Direct Login token generation (POST).