Skip to content

Instantly share code, notes, and snippets.

@MdGolam-Kibria
Created August 22, 2024 08:23
Show Gist options
  • Save MdGolam-Kibria/c3f90e1c7f5a8dfc55e2790251e52626 to your computer and use it in GitHub Desktop.
Save MdGolam-Kibria/c3f90e1c7f5a8dfc55e2790251e52626 to your computer and use it in GitHub Desktop.
Api call using RestClient using java 17
public ResponseEntity<?> login(LoginRecord loginRecord) {
try {
RemoteRequest<LoginRecord> remoteRequest = new RemoteRequest<>();
remoteRequest.setRequestId(String.valueOf(System.nanoTime()));
remoteRequest.setChannelId("TERP");
remoteRequest.setRequestTimestamp(String.valueOf(System.currentTimeMillis()));
remoteRequest.setData(loginRecord);
log.info("""
[LOGIN] Going to call login api using
URL : {}
Request : {}
""", loginURL, remoteRequest);
ResponseEntity<RemoteResponse<LoginResponse>> response = RestClient.builder()
.baseUrl(loginURL).build()
.post()
.headers(httpHeaders -> {
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.set("Service-Code", "TERP");
})
.body(remoteRequest)
.retrieve()
.toEntity(new ParameterizedTypeReference<>() {
});
log.info("[LOGIN] login response is : {}", response);
if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) {
RemoteResponse<LoginResponse> actualBody = response.getBody();
if (Objects.equals(actualBody.getResponseCode(), "AU000"))
return ResponseEntity.status(HttpStatus.OK).body(actualBody);
return ResponseEntity.status(HttpStatus.CONFLICT).body(actualBody);
}
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new LoginResponse());
} catch (Exception e) {
log.error("Something went wrong during login :{}", e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Unable to login. Please try again later.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment