Skip to content

Commit

Permalink
Cache web call for fetching federated user data. (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmoy12c authored Sep 12, 2023
1 parent cd33925 commit b7aac8a
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.uci.transformer.odk.services;

import com.github.benmanes.caffeine.cache.Cache;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Component
Expand All @@ -30,10 +31,17 @@ public class SurveyService {
@Value("${nlapp.userauth}")
private String SURVEY_AUTH;

@Autowired
Cache<Object, Object> cache;


public JSONObject getUserByPhoneFromFederatedServers(String hiddenFieldsStr, String phone) {

String baseURL = SURVEY_URL + "?queryString=(username:\"" + phone + "\", mobilePhone: \"" + phone + "\")";
String cacheKey = String.format("FEDERATED USERS: SURVEY SERVICE: %s HIDDEN FIELDS: %s", baseURL, hiddenFieldsStr);
if (cache.getIfPresent(cacheKey) != null) {
return (JSONObject) cache.getIfPresent(cacheKey);
}
String hiddenName = null;
if (hiddenFieldsStr != null) {
hiddenName = new JSONArray(hiddenFieldsStr).getJSONObject(0).get("name").toString();
Expand Down Expand Up @@ -66,6 +74,7 @@ public JSONObject getUserByPhoneFromFederatedServers(String hiddenFieldsStr, Str
String value = users.getJSONObject("result").getJSONArray("users").getJSONObject(0).get(hiddenName).toString();
JSONObject user = new JSONObject();
user.put(hiddenName, value);
cache.put(cacheKey, user);
return user;
} else {
JSONObject user = new JSONObject();
Expand Down

0 comments on commit b7aac8a

Please sign in to comment.