Skip to content

Commit

Permalink
Changed Opencrvs Location fetch logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lalithkota committed Mar 7, 2023
1 parent 7e2bc97 commit 718102e
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions mediator/src/main/java/io/mosip/opencrvs/util/OpencrvsDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public class OpencrvsDataUtil {
@Autowired
private RestTokenUtil restTokenUtil;

private JSONObject opencrvsLocationsJson;
// private JSONObject opencrvsLocationsJson;

@PostConstruct
public void initLocations(){
opencrvsLocationsJson = fetchAllAddresses();
}
// @PostConstruct
// public void initLocations(){
// opencrvsLocationsJson = fetchAllAddresses();
// }

public ReceiveDto buildIdJson(DecryptedEventDto opencrvsRequestBody){
List<DecryptedEventDto.Event.Context.Entry> contextEntries;
Expand Down Expand Up @@ -284,23 +284,25 @@ public String getAddressLocationFromAddress(Map<String, Object> address, String

public String fetchAddressValueFromId(String id){
try{
return opencrvsLocationsJson.getJSONObject("data").getJSONObject(id).getString("name");
JSONObject json = new JSONObject(
new RestTemplate().getForObject(locationsUrl + "/" + id, String.class));
return json.getString("name");
} catch(Exception e){
throw ErrorCode.ADDRESS_FETCHING_EXCEPTION.throwUnchecked(e);
}
}

public JSONObject fetchAllAddresses(){
String token = restTokenUtil.getOpencrvsAuthToken("Fetching addresses.");
if(token==null || token.isEmpty()){
throw ErrorCode.ADDRESS_FETCHING_EXCEPTION.throwUnchecked();
}
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + token);
try{
return new JSONObject(new RestTemplate().exchange(locationsUrl, HttpMethod.GET, new HttpEntity<>(headers), String.class).getBody());
} catch (Exception e){
throw ErrorCode.ADDRESS_FETCHING_EXCEPTION.throwUnchecked(e);
}
}
// public JSONObject fetchAllAddresses(){
// String token = restTokenUtil.getOpencrvsAuthToken("Fetching addresses.");
// if(token==null || token.isEmpty()){
// throw ErrorCode.ADDRESS_FETCHING_EXCEPTION.throwUnchecked();
// }
// HttpHeaders headers = new HttpHeaders();
// headers.set("Authorization", "Bearer " + token);
// try{
// return new JSONObject(new RestTemplate().exchange(locationsUrl, HttpMethod.GET, new HttpEntity<>(headers), String.class).getBody());
// } catch (Exception e){
// throw ErrorCode.ADDRESS_FETCHING_EXCEPTION.throwUnchecked(e);
// }
// }
}

0 comments on commit 718102e

Please sign in to comment.