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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user