Skip to content

Commit

Permalink
Merge pull request #49 from pankajjangid05/prod-devlopment
Browse files Browse the repository at this point in the history
Producation Devlopment
  • Loading branch information
pankajjangid05 authored May 18, 2023
2 parents 85808f4 + eb690d6 commit ca0654e
Showing 1 changed file with 52 additions and 45 deletions.
97 changes: 52 additions & 45 deletions src/main/java/com/uci/utils/BotService.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Mono<JsonNode> getBotNodeFromStartingMessage(String startingMessage) {
.build())
.retrieve().bodyToMono(String.class).map(response -> {
if (response != null) {
log.info(response);
log.info("Call getBotNodeFromStartingMessage : " + response + " cache : " + cache.getIfPresent(cacheKey));
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(response);
Expand Down Expand Up @@ -106,7 +106,7 @@ public Mono<JsonNode> getBotNodeFromName(String botName) {
.build())
.retrieve().bodyToMono(String.class).map(response -> {
if (response != null) {
log.info(response);
log.info("Call getBotNodeFromName : " + response + " cache : " + cache.getIfPresent(cacheKey));
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(response);
Expand Down Expand Up @@ -146,6 +146,7 @@ public Mono<JsonNode> getBotNodeFromId(String botId) {
.bodyToMono(String.class)
.map(response -> {
if (response != null) {
log.info("Call getBotNodeFromId : " + response + " cache : " + cache.getIfPresent(cacheKey));
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode root = mapper.readTree(response);
Expand All @@ -165,48 +166,49 @@ public Mono<JsonNode> getBotNodeFromId(String botId) {
.log("cache");
}

/**
* Get Adapter id by bot name
* @param botName
* @return
*/
public Mono<String> getAdapterIdFromBotName(String botName) {
String cacheKey = "valid-adpater-from-bot-name: " + botName;
return CacheMono.lookup(key -> Mono.justOrEmpty(cache.getIfPresent(cacheKey) != null ? cache.getIfPresent(key).toString() : null)
.map(Signal::next), cacheKey)
.onCacheMissResume(() -> webClient.get()
.uri(builder -> builder.path("admin/bot/search/internal")
.queryParam("perPage", 5)
.queryParam("page", 1)
.queryParam("match", true)
.queryParam("name", botName)
.build())
.retrieve()
.bodyToMono(String.class).map(response -> {
if (response != null) {
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode root = mapper.readTree(response);
if(root.path("result") != null && root.path("result").get(0) != null
&& !root.path("result").get(0).isEmpty()) {
JsonNode botNode = root.path("result").get(0);
return BotUtil.getBotNodeAdapterId(botNode);
}
return null;
} catch (JsonProcessingException jsonMappingException) {
return null;
}

} else {
}
return null;
})
.doOnError(throwable -> log.info("Error in getting adpater: " + throwable.getMessage()))
.onErrorReturn(""))
.andWriteWith((key, signal) -> Mono.fromRunnable(
() -> Optional.ofNullable(signal.get()).ifPresent(value -> cache.put(key, value))))
.log("cache");
}
// /**
// * Get Adapter id by bot name
// * @param botName
// * @return
// */
// public Mono<String> getAdapterIdFromBotName(String botName) {
// String cacheKey = "valid-adpater-from-bot-name: " + botName;
// return CacheMono.lookup(key -> Mono.justOrEmpty(cache.getIfPresent(cacheKey) != null ? cache.getIfPresent(key).toString() : null)
// .map(Signal::next), cacheKey)
// .onCacheMissResume(() -> webClient.get()
// .uri(builder -> builder.path("admin/bot/search/internal")
// .queryParam("perPage", 5)
// .queryParam("page", 1)
// .queryParam("match", true)
// .queryParam("name", botName)
// .build())
// .retrieve()
// .bodyToMono(String.class).map(response -> {
// if (response != null) {
// log.info("Call getAdapterIdFromBotName : " + response);
// ObjectMapper mapper = new ObjectMapper();
// try {
// JsonNode root = mapper.readTree(response);
// if(root.path("result") != null && root.path("result").get(0) != null
// && !root.path("result").get(0).isEmpty()) {
// JsonNode botNode = root.path("result").get(0);
// return BotUtil.getBotNodeAdapterId(botNode);
// }
// return null;
// } catch (JsonProcessingException jsonMappingException) {
// return null;
// }
//
// } else {
// }
// return null;
// })
// .doOnError(throwable -> log.info("Error in getting adpater: " + throwable.getMessage()))
// .onErrorReturn(""))
// .andWriteWith((key, signal) -> Mono.fromRunnable(
// () -> Optional.ofNullable(signal.get()).ifPresent(value -> cache.put(key, value))))
// .log("cache");
// }

/**
* Retrieve bot id from bot name (from validated bot)
Expand All @@ -227,6 +229,7 @@ public Mono<String> getBotIdFromBotName(String botName) {
@Override
public String apply(String response) {
if (response != null) {
log.info("Call getBotIdFromBotName : " + response + " cache : " + cache.getIfPresent(cacheKey));
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode root = mapper.readTree(response);
Expand Down Expand Up @@ -315,6 +318,7 @@ public URI apply(UriBuilder builder) {
}).retrieve().bodyToMono(String.class).map(new Function<String, JsonNode>() {
@Override
public JsonNode apply(String response) {
log.info("Call getAdapterByID cache key : "+ cacheKey + " cache data : "+cache.getIfPresent(cacheKey));
if (response != null) {
ObjectMapper mapper = new ObjectMapper();
try {
Expand Down Expand Up @@ -349,7 +353,7 @@ public Mono<JsonNode> getAdapterCredentials(String adapterID) {
return getAdapterByID(adapterID).map(new Function<JsonNode, Mono<JsonNode>>() {
@Override
public Mono<JsonNode> apply(JsonNode adapter) {
log.info("adapter: "+adapter);
log.info("getAdapterByID : "+adapter);
if(adapter != null) {
String vaultKey;
try{
Expand Down Expand Up @@ -423,6 +427,7 @@ public Mono<JsonNode> getVaultCredentials(String secretKey) {
httpHeaders.set("admin-token", adminToken);
})
.retrieve().bodyToMono(String.class).map(response -> {
log.info("Call getVaultCredentials cache key : "+ cacheKey +" cache data : " + cache.getIfPresent(cacheKey));
if (response != null) {
ObjectMapper mapper = new ObjectMapper();
try {
Expand Down Expand Up @@ -462,6 +467,7 @@ public Mono<String> getFirstFormByBotID(String botId) {
@Override
public String apply(String response) {
if (response != null) {
log.info("Call getFirstFormByBotID : " + response + " cache : " + cache.getIfPresent(cacheKey));
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode root = mapper.readTree(response);
Expand Down Expand Up @@ -503,6 +509,7 @@ public Mono<String> getBotNameByBotID(String botId) {
@Override
public String apply(String response) {
if (response != null) {
log.info("Call getBotNameByBotID : " + response + " cache : " + cache.getIfPresent(cacheKey));
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode root = mapper.readTree(response);
Expand Down

0 comments on commit ca0654e

Please sign in to comment.