-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add apiCredentialController and JupyterController (#31)
* add apiCredentialController and JupyterController * add queryTODOList
- Loading branch information
1 parent
e372119
commit 09ed567
Showing
15 changed files
with
526 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
wedpr-adm/src/main/java/com/webank/wedpr/adm/controller/ApiCredentialController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* | ||
* Copyright 2017-2025 [webank-wedpr] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
*/ | ||
package com.webank.wedpr.adm.controller; | ||
|
||
import com.github.pagehelper.PageInfo; | ||
import com.webank.wedpr.adm.controller.request.CredentialRequest; | ||
import com.webank.wedpr.adm.controller.response.CredentialResponse; | ||
import com.webank.wedpr.components.api.credential.dao.ApiCredentialDO; | ||
import com.webank.wedpr.components.api.credential.service.ApiCredentialService; | ||
import com.webank.wedpr.components.mybatis.PageHelperWrapper; | ||
import com.webank.wedpr.components.token.auth.TokenUtils; | ||
import com.webank.wedpr.components.token.auth.model.UserToken; | ||
import com.webank.wedpr.core.utils.Constant; | ||
import com.webank.wedpr.core.utils.WeDPRResponse; | ||
import java.util.List; | ||
import javax.servlet.http.HttpServletRequest; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping( | ||
path = Constant.WEDPR_API_PREFIX + "/credential", | ||
produces = {"application/json"}) | ||
public class ApiCredentialController { | ||
private static final Logger logger = LoggerFactory.getLogger(ApiCredentialController.class); | ||
|
||
@Autowired private ApiCredentialService apiCredentialService; | ||
|
||
/** | ||
* apply for new credential | ||
* | ||
* @param credentialDO the basic information for the credential | ||
* @param request the request(for obtain user information) | ||
* @return the id | ||
*/ | ||
@PostMapping("/applyFor") | ||
public WeDPRResponse applyFor( | ||
@RequestBody ApiCredentialDO credentialDO, HttpServletRequest request) { | ||
try { | ||
UserToken userToken = TokenUtils.getLoginUser(request); | ||
String id = | ||
apiCredentialService.applyForCredential(userToken.getUsername(), credentialDO); | ||
WeDPRResponse response = | ||
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG); | ||
response.setData(id); | ||
return response; | ||
} catch (Exception e) { | ||
logger.warn( | ||
"applyFor api credential exception, credential: {}, error: ", | ||
credentialDO.toString(), | ||
e); | ||
return new WeDPRResponse( | ||
Constant.WEDPR_FAILED, "apply for credential failed for " + e.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* query credentials by condition | ||
* | ||
* @param credentialRequest the condition | ||
* @param request the request(for obtain user information) | ||
* @return the queried credentials | ||
*/ | ||
@PostMapping("/query") | ||
public WeDPRResponse queryCredentials( | ||
@RequestBody CredentialRequest credentialRequest, HttpServletRequest request) { | ||
try (PageHelperWrapper pageHelperWrapper = new PageHelperWrapper(credentialRequest)) { | ||
UserToken userToken = TokenUtils.getLoginUser(request); | ||
List<ApiCredentialDO> credentialList = | ||
apiCredentialService.queryCredentials( | ||
userToken.getUsername(), credentialRequest.getCondition()); | ||
WeDPRResponse response = | ||
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG); | ||
response.setData( | ||
new CredentialResponse( | ||
credentialList, | ||
new PageInfo<ApiCredentialDO>(credentialList).getTotal())); | ||
return response; | ||
} catch (Exception e) { | ||
logger.warn( | ||
"queryCredentials exception, request: {}, error: ", | ||
credentialRequest.toString(), | ||
e); | ||
return new WeDPRResponse( | ||
Constant.WEDPR_FAILED, | ||
"query credentials failed for " | ||
+ e.getMessage() | ||
+ ", request detail: " | ||
+ credentialRequest.toString()); | ||
} | ||
} | ||
|
||
/** | ||
* delete credentials by condition | ||
* | ||
* @param condition the condition to delete the credential | ||
* @param request the request(for obtain user information) | ||
* @return success/failed | ||
*/ | ||
@PostMapping("/delete") | ||
public WeDPRResponse deleteCredential( | ||
@RequestBody ApiCredentialDO condition, HttpServletRequest request) { | ||
try { | ||
UserToken userToken = TokenUtils.getLoginUser(request); | ||
boolean success = | ||
apiCredentialService.deleteCredential(userToken.getUsername(), condition); | ||
WeDPRResponse response = | ||
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG); | ||
response.setData(success); | ||
return response; | ||
} catch (Exception e) { | ||
logger.warn( | ||
"deleteCredential exception, condition: {}, error: ", condition.toString(), e); | ||
return new WeDPRResponse( | ||
Constant.WEDPR_FAILED, | ||
"deleteCredential failed for " | ||
+ e.getMessage() | ||
+ ", condition: " | ||
+ condition.toString()); | ||
} | ||
} | ||
|
||
/** | ||
* update credentials by condition | ||
* | ||
* @param condition the condition to update the credential | ||
* @param request the request(for obtain user information) | ||
* @return success/failed | ||
*/ | ||
@PostMapping("/update") | ||
public WeDPRResponse updateCredentials( | ||
@RequestBody ApiCredentialDO condition, HttpServletRequest request) { | ||
try { | ||
UserToken userToken = TokenUtils.getLoginUser(request); | ||
boolean success = | ||
apiCredentialService.updateCredential(userToken.getUsername(), condition); | ||
WeDPRResponse response = | ||
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG); | ||
response.setData(success); | ||
return response; | ||
} catch (Exception e) { | ||
logger.warn( | ||
"updateCredentials exception, condition: {}, error: ", condition.toString(), e); | ||
return new WeDPRResponse( | ||
Constant.WEDPR_FAILED, | ||
"updateCredentials failed for " | ||
+ e.getMessage() | ||
+ ", condition: " | ||
+ condition.toString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
wedpr-adm/src/main/java/com/webank/wedpr/adm/controller/JupyterController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/* | ||
* Copyright 2017-2025 [webank-wedpr] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
*/ | ||
|
||
package com.webank.wedpr.adm.controller; | ||
|
||
import com.github.pagehelper.PageInfo; | ||
import com.webank.wedpr.adm.controller.request.JupyterRequest; | ||
import com.webank.wedpr.adm.controller.response.JupyterResponse; | ||
import com.webank.wedpr.components.integration.jupyter.dao.JupyterInfoDO; | ||
import com.webank.wedpr.components.integration.jupyter.service.JupyterService; | ||
import com.webank.wedpr.components.mybatis.PageHelperWrapper; | ||
import com.webank.wedpr.components.token.auth.TokenUtils; | ||
import com.webank.wedpr.components.token.auth.model.UserToken; | ||
import com.webank.wedpr.core.config.WeDPRCommonConfig; | ||
import com.webank.wedpr.core.utils.Constant; | ||
import com.webank.wedpr.core.utils.WeDPRResponse; | ||
import java.util.List; | ||
import javax.servlet.http.HttpServletRequest; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping( | ||
path = Constant.WEDPR_API_PREFIX + "/jupyter", | ||
produces = {"application/json"}) | ||
public class JupyterController { | ||
private static final Logger logger = LoggerFactory.getLogger(JupyterController.class); | ||
|
||
@Autowired private JupyterService jupyterService; | ||
|
||
/** | ||
* allocate the jupyter | ||
* | ||
* @param request the request to obtain user information | ||
* @return the allocated jupyterID | ||
*/ | ||
@GetMapping("/allocate") | ||
public WeDPRResponse allocate(HttpServletRequest request) { | ||
try { | ||
UserToken userToken = TokenUtils.getLoginUser(request); | ||
String jupyterID = | ||
this.jupyterService.allocate( | ||
userToken.getUsername(), WeDPRCommonConfig.getAgency()); | ||
WeDPRResponse response = | ||
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG); | ||
response.setData(jupyterID); | ||
return response; | ||
} catch (Exception e) { | ||
logger.warn("allocate jupyter failed, error: ", e); | ||
return new WeDPRResponse( | ||
Constant.WEDPR_FAILED, "allocate jupyter failed for " + e.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* query jupyter by condition | ||
* | ||
* @param queryRequest the query request | ||
* @param request the request to obtain user information | ||
* @return | ||
*/ | ||
@PostMapping("/query") | ||
public WeDPRResponse queryJupyters( | ||
@RequestBody JupyterRequest queryRequest, HttpServletRequest request) { | ||
try (PageHelperWrapper pageHelperWrapper = new PageHelperWrapper(queryRequest)) { | ||
UserToken userToken = TokenUtils.getLoginUser(request); | ||
List<JupyterInfoDO> result = | ||
this.jupyterService.queryJupyters( | ||
userToken.isAdmin(), | ||
userToken.getUsername(), | ||
queryRequest.getCondition()); | ||
WeDPRResponse response = | ||
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG); | ||
response.setData( | ||
new JupyterResponse(new PageInfo<JupyterInfoDO>(result).getTotal(), result)); | ||
return response; | ||
} catch (Exception e) { | ||
logger.warn( | ||
"queryJupyters jupyter failed, condition: {}, error: ", | ||
queryRequest.toString(), | ||
e); | ||
return new WeDPRResponse( | ||
Constant.WEDPR_FAILED, | ||
"queryJupyters failed for " | ||
+ e.getMessage() | ||
+ ", requestDetail: " | ||
+ queryRequest.toString()); | ||
} | ||
} | ||
|
||
/** | ||
* open jupyter | ||
* | ||
* @param id the jupyter id | ||
* @param request the request to obtain user information | ||
* @return the opened jupyter information | ||
*/ | ||
@GetMapping("/open") | ||
public WeDPRResponse open(@RequestParam String id, HttpServletRequest request) { | ||
try { | ||
UserToken userToken = TokenUtils.getLoginUser(request); | ||
JupyterInfoDO jupyterInfo = this.jupyterService.open(userToken.getUsername(), id); | ||
WeDPRResponse response = | ||
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG); | ||
response.setData(jupyterInfo); | ||
return response; | ||
} catch (Exception e) { | ||
logger.warn("open jupyter failed, id: {}, error: ", id, e); | ||
return new WeDPRResponse( | ||
Constant.WEDPR_FAILED, | ||
"open failed for " + id + " failed, reason:" + e.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* close the jupyter | ||
* | ||
* @param id the jupyter id | ||
* @param request the request to obtain user information | ||
* @return the closed jupyter information | ||
*/ | ||
@GetMapping("/close") | ||
public WeDPRResponse close(@RequestParam String id, HttpServletRequest request) { | ||
try { | ||
UserToken userToken = TokenUtils.getLoginUser(request); | ||
JupyterInfoDO jupyterInfo = this.jupyterService.close(userToken.getUsername(), id); | ||
WeDPRResponse response = | ||
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG); | ||
response.setData(jupyterInfo); | ||
return response; | ||
} catch (Exception e) { | ||
logger.warn("close jupyter failed, id: {}, error: ", id, e); | ||
return new WeDPRResponse( | ||
Constant.WEDPR_FAILED, | ||
"close failed for " + id + " failed, reason:" + e.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* destroy the jupyter | ||
* | ||
* @param id the jupyter id | ||
* @param request the request to obtain user information | ||
* @return | ||
*/ | ||
@DeleteMapping("/destroy") | ||
public WeDPRResponse destroy(@RequestParam String id, HttpServletRequest request) { | ||
try { | ||
UserToken userToken = TokenUtils.getLoginUser(request); | ||
boolean success = | ||
this.jupyterService.destroy(userToken.isAdmin(), userToken.getUsername(), id); | ||
WeDPRResponse response = | ||
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG); | ||
response.setData(success); | ||
return response; | ||
} catch (Exception e) { | ||
logger.warn("destroy jupyter failed, id: {}, error: ", id, e); | ||
return new WeDPRResponse( | ||
Constant.WEDPR_FAILED, | ||
"destroy failed for " + id + " failed, reason:" + e.getMessage()); | ||
} | ||
} | ||
} |
Oops, something went wrong.