adding FetchbillByBillId
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package com.billpaymentservice.bbps.serviceImpl;
|
||||
|
||||
|
||||
import java.net.http.HttpClient;
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.JdkClientHttpRequestFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.time.Duration;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.client.HttpServerErrorException;
|
||||
import org.springframework.web.client.RestClient;
|
||||
|
||||
@Service
|
||||
public class HttpService {
|
||||
private static final Logger log = LoggerFactory.getLogger(HttpService.class);
|
||||
private final RestClient restClient;
|
||||
|
||||
HttpService() {
|
||||
HttpClient httpClient = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(5))
|
||||
.build();
|
||||
|
||||
JdkClientHttpRequestFactory factory = new JdkClientHttpRequestFactory(httpClient);
|
||||
factory.setReadTimeout(Duration.ofSeconds(10));
|
||||
|
||||
this.restClient = RestClient.builder()
|
||||
.requestFactory(factory)
|
||||
.build();
|
||||
}
|
||||
|
||||
public HashMap<String, String> postRequest(String url, String payload) {
|
||||
HashMap<String, String> response = new HashMap<>();
|
||||
try {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
log.info("📡 Bank POST Request to URL: {}", url);
|
||||
|
||||
ResponseEntity<String> restResponse = restClient.post()
|
||||
.uri(url)
|
||||
.headers(h -> h.addAll(headers))
|
||||
.body(payload)
|
||||
.retrieve()
|
||||
.toEntity(String.class);
|
||||
|
||||
response.put("respCode", String.valueOf(restResponse.getStatusCode().value()));
|
||||
response.put("data", restResponse.getBody());
|
||||
|
||||
} catch (HttpClientErrorException | HttpServerErrorException httpException) {
|
||||
response.put("respCode", String.valueOf(httpException.getStatusCode()));
|
||||
response.put("data", "Exception : " + httpException.getResponseBodyAsString());
|
||||
} catch (Exception e) {
|
||||
response.put("data", "Exception :" + e.getMessage());
|
||||
log.error("❌ Exception during POST call to {} -> {}", url, e.getMessage());
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
public HashMap<String, String> getRequest(String url) {
|
||||
HashMap<String, String> response = new HashMap<>();
|
||||
try {
|
||||
log.info("🌐 Bank GET Request to URL: {}", url);
|
||||
|
||||
ResponseEntity<String> restResponse = restClient.get()
|
||||
.uri(url)
|
||||
.retrieve()
|
||||
.toEntity(String.class);
|
||||
|
||||
response.put("data", restResponse.getBody());
|
||||
|
||||
} catch (HttpClientErrorException | HttpServerErrorException httpException) {
|
||||
response.put("data", "Exception : " + httpException.getResponseBodyAsString());
|
||||
} catch (Exception e) {
|
||||
response.put("data", "Exception :" + e.getMessage());
|
||||
log.error("❌ Exception during GET call to {} -> {}", url, e.getMessage());
|
||||
}
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,74 @@
|
||||
package com.billpaymentservice.bbps.serviceImpl;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.billpaymentservice.bbps.dto.BillerDAO;
|
||||
import com.billpaymentservice.bbps.dto.GeneralResp;
|
||||
import com.billpaymentservice.bbps.dto.request.BillerDetailRequest;
|
||||
import com.billpaymentservice.bbps.dto.response.BillerDataResponse;
|
||||
import com.billpaymentservice.bbps.exception.CustomException;
|
||||
import com.billpaymentservice.bbps.service.MasterDataService;
|
||||
import com.billpaymentservice.bbps.serviceImpl.HttpService;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class MasterDataServiceImpl implements MasterDataService {
|
||||
|
||||
@Override
|
||||
public BillerDataResponse getBillerByBillerId(BillerDetailRequest billerDetailRequest) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Service
|
||||
public class MasterDataServiceImpl implements MasterDataService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MasterDataServiceImpl.class);
|
||||
private final HttpService httpService;
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
@Value("${bbps.billerDataFetch.url}")
|
||||
private String billerDataFetchUrl;
|
||||
|
||||
// Inject HttpService
|
||||
public MasterDataServiceImpl(HttpService httpService) {
|
||||
this.httpService = httpService;
|
||||
}
|
||||
@Override
|
||||
public BillerDataResponse getBillerByBillerId(BillerDetailRequest billerDetailRequest) {
|
||||
|
||||
|
||||
String billerId = billerDetailRequest.getBillerId();
|
||||
String url = billerDataFetchUrl + billerId;
|
||||
|
||||
Map<String, String> responseMap = httpService.getRequest(url);
|
||||
String responseData = responseMap.get("data");
|
||||
log.info("data is :{}",responseData);
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
try {
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData, new TypeReference<Map<String,Object>>() {});
|
||||
if (!dataMap.containsKey("generalResp")) {
|
||||
throw new CustomException(
|
||||
String.valueOf(dataMap.get("statusCode")),
|
||||
String.valueOf(dataMap.get("status")),
|
||||
String.valueOf(dataMap.get("message")),
|
||||
String.valueOf(dataMap.get("remarks")));
|
||||
}
|
||||
BillerDataResponse responseWrapper = objectMapper.convertValue(dataMap, BillerDataResponse.class);
|
||||
return responseWrapper;
|
||||
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Error parsing responseData JSON: {}", e.getMessage());
|
||||
throw new CustomException("Invalid JSON received from API");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user