diff --git a/src/main/java/com/igot/service_locator/controller/IntegrationController.java b/src/main/java/com/igot/service_locator/controller/IntegrationController.java index 998d616..208f822 100644 --- a/src/main/java/com/igot/service_locator/controller/IntegrationController.java +++ b/src/main/java/com/igot/service_locator/controller/IntegrationController.java @@ -24,4 +24,9 @@ public Object callExternalApiService(@RequestBody IntegrationModel integrationMo public Object callExternalApiServiceByConfigId(@RequestBody(required = false) ServiceRequestDto requestDto, @PathVariable String id) throws IOException { return service.getRequestPayloadByConfigId(requestDto,id); } + + @PostMapping("/v1/callexternal/progressapibyid/{id}") + public Object callExternalProgressApiByConfigId(@RequestBody(required = false) ServiceRequestDto requestDto, @PathVariable String id){ + return service.getProgressRequestPayloadByConfigId(requestDto,id); + } } diff --git a/src/main/java/com/igot/service_locator/entity/ServiceLocatorEntity.java b/src/main/java/com/igot/service_locator/entity/ServiceLocatorEntity.java index 58133bf..b0e77a4 100644 --- a/src/main/java/com/igot/service_locator/entity/ServiceLocatorEntity.java +++ b/src/main/java/com/igot/service_locator/entity/ServiceLocatorEntity.java @@ -77,6 +77,14 @@ public class ServiceLocatorEntity implements Serializable { @JsonProperty("partnerCode") private String partnerCode; + @Column(name = "strict_cache") + @JsonProperty("strictCache") + private boolean strictCache=false; + + @Column(name = "strict_cache_time") + @JsonProperty("strictCacheTimeInMinutes") + private long strictCacheTimeInMinutes; + public enum RequestMethod { GET("GET"), HEAD("HEAD"), diff --git a/src/main/java/com/igot/service_locator/plugins/coursera/CourseraPluginServiceImpl.java b/src/main/java/com/igot/service_locator/plugins/coursera/CourseraPluginServiceImpl.java index 996a5af..f094867 100644 --- a/src/main/java/com/igot/service_locator/plugins/coursera/CourseraPluginServiceImpl.java +++ b/src/main/java/com/igot/service_locator/plugins/coursera/CourseraPluginServiceImpl.java @@ -8,6 +8,7 @@ import com.igot.service_locator.plugins.ContentPartnerPluginService; import com.igot.service_locator.repository.CallExternalService; import com.igot.service_locator.util.CbServerProperties; +import com.igot.service_locator.util.Constants; import com.igot.service_locator.util.DataTransformUtility; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -20,17 +21,20 @@ @Slf4j public class CourseraPluginServiceImpl implements ContentPartnerPluginService { - @Autowired - private ObjectMapper objectMapper; - - @Autowired - private CbServerProperties cbServerProperties; - - @Autowired - private CallExternalService callExternalService; - - @Autowired - private DataTransformUtility dataTransformUtility; + private final ObjectMapper objectMapper; + private final CbServerProperties cbServerProperties; + private final CallExternalService callExternalService; + private final DataTransformUtility dataTransformUtility; + + public CourseraPluginServiceImpl(ObjectMapper objectMapper, + CbServerProperties cbServerProperties, + CallExternalService callExternalService, + DataTransformUtility dataTransformUtility) { + this.objectMapper = objectMapper; + this.cbServerProperties = cbServerProperties; + this.callExternalService = callExternalService; + this.dataTransformUtility = dataTransformUtility; + } @Override public String generateAuthHeader() { @@ -46,11 +50,11 @@ public String generateAuthHeader() { dto.setOperationType("PEER_TO_PEER"); dto.setRequestHeader(requestHeader); dto.setRequestBody(requestBody); - dto.setServiceCode("coursera-auth-api"); - dto.setServiceName("coursera-auth-api"); - dto.setServiceDescription("coursera-auth-api"); + dto.setServiceCode(Constants.COURSERA_AUTH_API); + dto.setServiceName(Constants.COURSERA_AUTH_API); + dto.setServiceDescription(Constants.COURSERA_AUTH_API); dto.setStrictCache(true); - dto.setStrictCacheTimeInMinutes(20); + dto.setStrictCacheTimeInMinutes(cbServerProperties.courseraAuthApiCacheTtl); Object response = callExternalService.fetchResult(dataTransformUtility.getIntegrationFrameWorkUrl(),dto); JsonNode jsonResponse=objectMapper.convertValue(response, new TypeReference() { }); diff --git a/src/main/java/com/igot/service_locator/service/IntegrationModelService.java b/src/main/java/com/igot/service_locator/service/IntegrationModelService.java index bdbc9b5..6db03c7 100644 --- a/src/main/java/com/igot/service_locator/service/IntegrationModelService.java +++ b/src/main/java/com/igot/service_locator/service/IntegrationModelService.java @@ -12,4 +12,5 @@ public interface IntegrationModelService { Object getDetailsFromExternalService(IntegrationModel integrationModel) throws IOException; Object getRequestPayloadByConfigId(ServiceRequestDto requestDto, String id) throws IOException; + Object getProgressRequestPayloadByConfigId(ServiceRequestDto requestDto, String id); } diff --git a/src/main/java/com/igot/service_locator/service/impl/IntegrationModelServiceImpl.java b/src/main/java/com/igot/service_locator/service/impl/IntegrationModelServiceImpl.java index f87a695..233d375 100644 --- a/src/main/java/com/igot/service_locator/service/impl/IntegrationModelServiceImpl.java +++ b/src/main/java/com/igot/service_locator/service/impl/IntegrationModelServiceImpl.java @@ -71,6 +71,29 @@ public Object getRequestPayloadByConfigId(ServiceRequestDto serviceRequestDto, S IntegrationModel model = replaceServiceRequestDtoPlaceholders(jsonNode, serviceRequestDto); model.setServiceCode(serviceLocatorEntity.getServiceCode()); model.setPartnerCode(serviceLocatorEntity.getPartnerCode()); + model.setStrictCache(serviceLocatorEntity.isStrictCache()); + model.setStrictCacheTimeInMinutes(serviceLocatorEntity.getStrictCacheTimeInMinutes()); + log.debug("model::{}", model); + return getDetailsFromExternalService(model); + } else { + throw new CustomException(Constants.ERROR, "requestDto not present in Db with given Id ", HttpStatus.BAD_REQUEST); + } + } catch (Exception e) { + throw new CustomException(Constants.ERROR, e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @Override + public Object getProgressRequestPayloadByConfigId(ServiceRequestDto serviceRequestDto, String id) { + log.info("IntegrationModelServiceImpl::getRequestPayloadByConfigId"); + try { + ServiceLocatorEntity serviceLocatorEntity = serviceLocatorService.readServiceConfig(id, true); + JsonNode jsonNode = serviceLocatorEntity.getRequestPayload(); + if (!jsonNode.isMissingNode()) { + IntegrationModel model = replaceServiceRequestDtoPlaceholders(jsonNode, serviceRequestDto); + model.setServiceCode(serviceLocatorEntity.getServiceCode()); + model.setStrictCache(serviceLocatorEntity.isStrictCache()); + model.setStrictCacheTimeInMinutes(serviceLocatorEntity.getStrictCacheTimeInMinutes()); log.debug("model::{}", model); return getDetailsFromExternalService(model); } else { @@ -129,9 +152,6 @@ private String replaceUrlPlaceholders(ServiceLocatorEntity serviceLocator, Map() {}); + JsonNode jsonNode = responseJson.get("responseData"); log.info("Got successful response from external system service"); if (integrationModel.getPartnerCode() != null) { JsonNode response = dataTransformUtility.callContentPartnerReadAPIByPartnerCode(integrationModel.getPartnerCode()); - if (!response.path("trasformContentJson").isMissingNode()) { + if (!response.path("transformContentViaApi").isMissingNode()) { + log.info("Inside transformContentViaApi: {}", integrationModel.getPartnerCode()); List contentJson = mapper.convertValue(response.get("transformContentViaApi"), new TypeReference>() {}); - Object transformData = dataTransformUtility.transformData(responseObject, contentJson); + Object transformData = dataTransformUtility.transformData(jsonNode, contentJson); if (transformData != null) { return transformData; } else { - return responseObject; + return jsonNode; } + } else { + return jsonNode; } - else { - return responseObject; + } else if(serviceLocator.getPartnerCode() != null) { + log.info("serviceLocator.getPartnerCode(): {}", serviceLocator.getPartnerCode()); + JsonNode response = dataTransformUtility.callContentPartnerReadAPIByPartnerCode(serviceLocator.getPartnerCode()); + if (!response.path("transformProgressViaApi").isMissingNode()) { + log.info("Inside transformProgressViaApi: {}", serviceLocator.getPartnerCode()); + List contentJson = mapper.convertValue(response.get("transformProgressViaApi"), new TypeReference>() {}); + Object transformData = dataTransformUtility.transformData(jsonNode, contentJson); + if (transformData != null) { + return transformData; + } else { + return jsonNode; + } + } else { + return jsonNode; } } else { - return responseObject; + return jsonNode; } } @@ -115,8 +130,8 @@ private ObjectNode createRequestObject(ServiceLocatorEntity serviceLocator, Inte requestObject.put("serviceCode", serviceLocator.getServiceCode()); requestObject.put("serviceName", serviceLocator.getServiceName()); requestObject.put("serviceDescription", serviceLocator.getServiceDescription()); - requestObject.put("strictCache", integrationModel.getStrictCache()); - requestObject.put("strictCacheTimeInMinutes", integrationModel.getStrictCacheTimeInMinutes()); + requestObject.put("strictCache", serviceLocator.isStrictCache()); + requestObject.put("strictCacheTimeInMinutes", serviceLocator.getStrictCacheTimeInMinutes()); requestObject.put("alwaysDataReadFromCache",integrationModel.isAlwaysDataReadFromCache()); return requestObject; } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index b1515cb..c546991 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -36,4 +36,5 @@ cornell.partner.code=CORNELL cornell.url.segment=enrollments coursera.partner.code=COURSERA coursera.auth.client_credentials=SWlIZ1FsSlU0clYzOEtNRU9jRzRiUTp0YnFPMHJrY3hROFhWUldnTDRiSEFB -coursera.auth.api.url=https://api.coursera.com/oauth2/client_credentials/token \ No newline at end of file +coursera.auth.api.url=https://api.coursera.com/oauth2/client_credentials/token +coursera.auth.api.cache.ttl=25 \ No newline at end of file