Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strict cache added in config database. #20

Merged
merged 7 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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<JsonNode>() {
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -129,9 +152,6 @@ private String replaceUrlPlaceholders(ServiceLocatorEntity serviceLocator, Map<S
String placeholderValue = null;
for (int i = 0; i < urlPlaceholderArr.length; i++) {
String placeholder = urlPlaceholderArr[i];
// if (placeholder.equalsIgnoreCase("{hostAddress}")) {
// placeholderValue = serviceLocator.getHostAddress();
// } else {
String placeholderWithoutCurlyBraces = placeholder.substring(1, placeholder.length() - 1);
if (urlMap.containsKey(placeholderWithoutCurlyBraces)) {
String value = urlMap.get(placeholderWithoutCurlyBraces);
Expand All @@ -141,7 +161,6 @@ private String replaceUrlPlaceholders(ServiceLocatorEntity serviceLocator, Map<S
} else {
placeholderValue = ""; // Assign an empty value if the field is not present in urlMap
}
// }
if (placeholderValue != null) {
urlToModify = urlToModify.replace(placeholder, placeholderValue);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ public class CbServerProperties {

@Value("${coursera.auth.api.url}")
public String courseraAuthApiUrl;

@Value("${coursera.auth.api.cache.ttl}")
public Long courseraAuthApiCacheTtl;
}
2 changes: 2 additions & 0 deletions src/main/java/com/igot/service_locator/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
public class Constants {
public static final String EXTERNAL_SERVICE_CALL_EXCEPTION = "External Service Call Exception";
public static final String ERROR = "ERROR";
public static final String COURSERA_AUTH_API = "coursera-auth-api";

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,50 +26,65 @@
@Slf4j
public class IntegrationFrameworkUtil {

@Autowired
private IntegrationConfig config;

@Autowired
private ObjectMapper mapper;

@Autowired
private CallExternalService callExternalService;

@Autowired
private DataTransformUtility dataTransformUtility;

@Autowired
private ContentPartnerServiceFactory contentPartnerServiceFactory;

@Autowired
private CbServerProperties cbServerProperties;


private final IntegrationConfig config;
private final ObjectMapper mapper;
private final CallExternalService callExternalService;
private final DataTransformUtility dataTransformUtility;
private final ContentPartnerServiceFactory contentPartnerServiceFactory;

public IntegrationFrameworkUtil(IntegrationConfig config,
ObjectMapper mapper,
CallExternalService callExternalService,
DataTransformUtility dataTransformUtility,
ContentPartnerServiceFactory contentPartnerServiceFactory) {
this.config = config;
this.mapper = mapper;
this.callExternalService = callExternalService;
this.dataTransformUtility = dataTransformUtility;
this.contentPartnerServiceFactory = contentPartnerServiceFactory;
}
public Object callExternalServiceApi(
IntegrationModel integrationModel, ServiceLocatorEntity serviceLocator) throws JsonProcessingException {

ObjectNode requestObject = this.createRequestObject(serviceLocator, integrationModel);
log.info("IntegrationFrameworkUtil::requestObject {} ", requestObject);

Object responseObject = callExternalService.fetchResult(dataTransformUtility.getIntegrationFrameWorkUrl(), requestObject);
JsonNode responseJson = mapper.convertValue(responseObject, new TypeReference<JsonNode>() {});
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<Object> contentJson = mapper.convertValue(response.get("transformContentViaApi"), new TypeReference<List<Object>>() {});
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<Object> contentJson = mapper.convertValue(response.get("transformProgressViaApi"), new TypeReference<List<Object>>() {});
Object transformData = dataTransformUtility.transformData(jsonNode, contentJson);
if (transformData != null) {
return transformData;
} else {
return jsonNode;
}
} else {
return jsonNode;
}
} else {
return responseObject;
return jsonNode;
}

}
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
coursera.auth.api.url=https://api.coursera.com/oauth2/client_credentials/token
coursera.auth.api.cache.ttl=25