From a37ec55d261ed92bc50c8e3a5f48d244f622fbcb Mon Sep 17 00:00:00 2001 From: Sreerag K S <58926794+sreeragksgh@users.noreply.github.com> Date: Tue, 4 Jun 2024 09:59:18 +0530 Subject: [PATCH] ResponseCode correction cb-ext generate ext OTP API (#579) * Updating user rate limit error to 429 from 500 * review chnages --- .../org/sunbird/common/util/Constants.java | 3 +++ .../service/UserRegistrationServiceImpl.java | 26 ++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/sunbird/common/util/Constants.java b/src/main/java/org/sunbird/common/util/Constants.java index 51bce9a1c..0b3831de4 100644 --- a/src/main/java/org/sunbird/common/util/Constants.java +++ b/src/main/java/org/sunbird/common/util/Constants.java @@ -1028,6 +1028,9 @@ public class Constants { public static final String PAGE_ID = "pageId"; public static final String USERS_LIST = "userList"; public static final String TOTAL_COUNT = "totalCount"; + public static final String TOO_MANY_REQUESTS = "TOO_MANY_REQUESTS"; + public static final String SERVER_ERROR = "serverError"; + public static final String RESOURCE_NOT_FOUND = "RESOURCE_NOT_FOUND"; private Constants() { throw new IllegalStateException("Utility class"); } diff --git a/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java b/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java index d929c9c2f..c6a69643d 100644 --- a/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java +++ b/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java @@ -204,7 +204,8 @@ public SBApiResponse generateOTP(Map otpRequests) { headers.put(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON); Map apiResponse = outboundRequestHandlerService.fetchResultUsingPost(url, otpRequests, headers); - if (Constants.OK.equalsIgnoreCase((String) apiResponse.get(Constants.RESPONSE_CODE))) { + String responseCode = (String) apiResponse.get(Constants.RESPONSE_CODE); + if (Constants.OK.equalsIgnoreCase(responseCode)) { LOGGER.info("OTP Generation successful"); response.setVer("v1"); response.getParams().setStatus(Constants.SUCCESS.toUpperCase()); @@ -213,7 +214,26 @@ public SBApiResponse generateOTP(Map otpRequests) { response.setResult(new HashMap<>()); response.getResult().put(Constants.RESPONSE, Constants.SUCCESS.toUpperCase()); } else { - errMsg = (String) ((Map)apiResponse.get(Constants.PARAMS)).get(Constants.ERROR_MESSAGE); + errMsg = (String) ((Map) apiResponse.get(Constants.PARAMS)).get(Constants.ERROR_MESSAGE); + response.getParams().setStatus(Constants.FAILED); + + switch (responseCode.toUpperCase()) { + case Constants.TOO_MANY_REQUESTS: + response.setResponseCode(HttpStatus.TOO_MANY_REQUESTS); + break; + case Constants.CLIENT_ERROR: + response.setResponseCode(HttpStatus.BAD_REQUEST); + break; + case Constants.SERVER_ERROR: + response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); + break; + case Constants.RESOURCE_NOT_FOUND: + response.setResponseCode(HttpStatus.NOT_FOUND); + break; + default: + response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); + break; + } } } catch (Exception e) { LOGGER.error(String.format("Exception in %s : %s", "generateOTP", e.getMessage()), e); @@ -222,9 +242,7 @@ public SBApiResponse generateOTP(Map otpRequests) { } if (StringUtils.isNotBlank(errMsg)) { LOGGER.error("OTP generation request failed, error message : ",errMsg); - response.getParams().setStatus(Constants.FAILED); response.getParams().setErrmsg(errMsg); - response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); } return response; }