refactor/added createLocalisedResourceDocJsonCached

This commit is contained in:
hongwei 2023-10-30 16:48:34 +01:00
parent 506a924b31
commit 4e82c66cad
7 changed files with 53 additions and 35 deletions

View File

@ -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.

View File

@ -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
/**
*

View File

@ -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
}

View File

@ -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 (<em>))
// 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)))
}
}

View File

@ -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(

View File

@ -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:")
}

View File

@ -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).