Skip to content

Commit

Permalink
Merge pull request #2261 from hongwei1/develop
Browse files Browse the repository at this point in the history
feature/added the futureWithLimits for all connector methods
  • Loading branch information
simonredfern authored Sep 8, 2023
2 parents b27aff5 + da8cce9 commit accc477
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 119 deletions.
2 changes: 1 addition & 1 deletion obp-api/src/main/scala/code/api/OBPRestHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ trait OBPRestHelper extends RestHelper with MdcLoggable {
val (user, callContext) = OAuth2Login.getUser(cc)
user match {
case Full(u) =>
AuthUser.refreshUser(u, callContext)
AuthUser.refreshUserLegacy(u, callContext)
fn(cc.copy(user = Full(u))) // Authentication is successful
case Empty => fn(cc.copy(user = Empty)) // Anonymous access
case ParamFailure(a, b, c, apiFailure : APIFailure) => ParamFailure(a, b, c, apiFailure : APIFailure)
Expand Down
21 changes: 12 additions & 9 deletions obp-api/src/main/scala/code/api/util/APIUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2180,9 +2180,12 @@ object APIUtil extends MdcLoggable with CustomJsonFormats{



def fullBaseUrl : String = {
val crv = CurrentReq.value
val apiPathZeroFromRequest = crv.path.partPath(0)
def fullBaseUrl(callContext: Option[CallContext]) : String = {
// callContext.map(_.url).getOrElse("") --> eg: /obp/v2.0.0/banks/gh.29.uk/accounts/202309071568
val urlFromRequestArray = callContext.map(_.url).getOrElse("").split("/") //eg: Array("", obp, v2.0.0, banks, gh.29.uk, accounts, 202309071568)

val apiPathZeroFromRequest = if( urlFromRequestArray.length>1) urlFromRequestArray.apply(1) else urlFromRequestArray.head

if (apiPathZeroFromRequest != ApiPathZero) throw new Exception("Configured ApiPathZero is not the same as the actual.")

val path = s"$HostName/$ApiPathZero"
Expand All @@ -2191,7 +2194,7 @@ object APIUtil extends MdcLoggable with CustomJsonFormats{


// Modify URL replacing placeholders for Ids
def contextModifiedUrl(url: String, context: DataContext) = {
def contextModifiedUrl(url: String, context: DataContext, callContext: Option[CallContext]) = {

// Potentially replace BANK_ID
val url2: String = context.bankId match {
Expand Down Expand Up @@ -2224,7 +2227,7 @@ object APIUtil extends MdcLoggable with CustomJsonFormats{
// Add host, port, prefix, version.

// not correct because call could be in other version
val fullUrl = s"$fullBaseUrl$url6"
val fullUrl = s"${fullBaseUrl(callContext)}$url6"

fullUrl
}
Expand Down Expand Up @@ -2280,19 +2283,19 @@ object APIUtil extends MdcLoggable with CustomJsonFormats{


// Returns API links (a list of them) that have placeholders (e.g. BANK_ID) replaced by values (e.g. ulster-bank)
def getApiLinks(callerContext: CallerContext, codeContext: CodeContext, dataContext: DataContext) : List[ApiLink] = {
def getApiLinks(callerContext: CallerContext, codeContext: CodeContext, dataContext: DataContext, callContext: Option[CallContext]) : List[ApiLink] = {
val templates = getApiLinkTemplates(callerContext, codeContext)
// Replace place holders in the urls like BANK_ID with the current value e.g. 'ulster-bank' and return as ApiLinks for external consumption
val links = templates.map(i => ApiLink(i.rel,
contextModifiedUrl(i.requestUrl, dataContext) )
contextModifiedUrl(i.requestUrl, dataContext, callContext))
)
links
}


// Returns links formatted at objects.
def getHalLinks(callerContext: CallerContext, codeContext: CodeContext, dataContext: DataContext) : JValue = {
val links = getApiLinks(callerContext, codeContext, dataContext)
def getHalLinks(callerContext: CallerContext, codeContext: CodeContext, dataContext: DataContext, callContext: Option[CallContext]) : JValue = {
val links = getApiLinks(callerContext, codeContext, dataContext, callContext: Option[CallContext])
getHalLinksFromApiLinks(links)
}

Expand Down
37 changes: 17 additions & 20 deletions obp-api/src/main/scala/code/api/util/NewStyle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -992,17 +992,16 @@ object NewStyle extends MdcLoggable{
validateRequestPayload(callContext)(boxResult)
}

def createUserAuthContext(user: User, key: String, value: String, callContext: Option[CallContext]): OBPReturnType[UserAuthContext] = {
Connector.connector.vend.createUserAuthContext(user.userId, key, value, callContext) map {
i => (connectorEmptyResponse(i._1, callContext), i._2)
} map {
result =>
//We will call the `refreshUserAccountAccess` after we successfully create the UserAuthContext
// because `createUserAuthContext` is a connector method, here is the entry point for OBP to refreshUser
AuthUser.refreshUser(user, callContext)
result
def createUserAuthContext(user: User, key: String, value: String, callContext: Option[CallContext]): OBPReturnType[UserAuthContext] =
for{
(userAuthContext, callContext) <- Connector.connector.vend.createUserAuthContext(user.userId, key, value, callContext) map {
i => (connectorEmptyResponse(i._1, callContext), i._2)
}
_ <- AuthUser.refreshUser(user, callContext)
}yield{
(userAuthContext, callContext)
}
}

def createUserAuthContextUpdate(userId: String, key: String, value: String, callContext: Option[CallContext]): OBPReturnType[UserAuthContextUpdate] = {
Connector.connector.vend.createUserAuthContextUpdate(userId, key, value, callContext) map {
i => (connectorEmptyResponse(i._1, callContext), i._2)
Expand All @@ -1019,17 +1018,15 @@ object NewStyle extends MdcLoggable{
}
}

def deleteUserAuthContextById(user: User, userAuthContextId: String, callContext: Option[CallContext]): OBPReturnType[Boolean] = {
Connector.connector.vend.deleteUserAuthContextById(userAuthContextId, callContext) map {
i => (connectorEmptyResponse(i._1, callContext), i._2)
}map {
result =>
// We will call the `refreshUserAccountAccess` after we successfully delete the UserAuthContext
// because `deleteUserAuthContextById` is a connector method, here is the entry point for OBP to refreshUser
AuthUser.refreshUser(user, callContext)
result
def deleteUserAuthContextById(user: User, userAuthContextId: String, callContext: Option[CallContext]): OBPReturnType[Boolean] =
for {
(userAuthContext, callContext) <- Connector.connector.vend.deleteUserAuthContextById(userAuthContextId, callContext) map {
i => (connectorEmptyResponse(i._1, callContext), i._2)
}
_ <- AuthUser.refreshUser(user, callContext)
} yield {
(userAuthContext, callContext)
}
}

def deleteUser(userPrimaryKey: UserPrimaryKey, callContext: Option[CallContext]): OBPReturnType[Boolean] = Future {
AuthUser.scrambleAuthUser(userPrimaryKey) match {
Expand Down
Loading

0 comments on commit accc477

Please sign in to comment.