Skip to content

Commit

Permalink
[Rahul] | BAH-3417 | Refactor. Change Login Caller Logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rahu1ramesh committed Jan 3, 2024
1 parent 6b7d664 commit 8421c81
Showing 1 changed file with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,31 @@ public RestClient(String baseURL, String username, String password, int connecti
}

private void login() {
if (isSessionInvalid()) {
String requestBody = buildLoginRequest();
WebClient client = getWebClient(baseURL);
HttpHeaders headers = getHttpHeaders();
Consumer<HttpHeaders> consumer = httpHeaders -> httpHeaders.addAll(headers);
try {
client.post().uri("web/session/authenticate").headers(consumer).bodyValue(requestBody).exchangeToMono(loginResponse -> {
if (loginResponse.statusCode().is2xxSuccessful()) {
try {
session = loginResponse.cookies().get("session_id").get(0);
} catch (Exception e) {
throw new OpenERPException(String.format("Failed to login. The login user is : %s, no session cookie set.", username));
}
String requestBody = buildLoginRequest();
WebClient client = getWebClient(baseURL);
HttpHeaders headers = getHttpHeaders();
Consumer<HttpHeaders> consumer = httpHeaders -> httpHeaders.addAll(headers);
try {
client.post().uri("web/session/authenticate").headers(consumer).bodyValue(requestBody).exchangeToMono(loginResponse -> {
if (loginResponse.statusCode().is2xxSuccessful()) {
try {
session = loginResponse.cookies().get("session_id").get(0);
} catch (Exception e) {
throw new OpenERPException(String.format("Failed to login. The login user is : %s, no session cookie set.", username));
}
return loginResponse.bodyToMono(String.class);
}).timeout(Duration.ofMillis(connectionTimeout)).block();
logger.debug("\n-----------------------------------------------------Login Initiated-----------------------------------------------------\n* Request : {}\n* Session Id : {}\n-----------------------------------------------------End of Login-----------------------------------------------------", requestBody, session);
} catch (Exception e) {
logger.warn("Failed to login for user {}", username);
}
}
return loginResponse.bodyToMono(String.class);
}).timeout(Duration.ofMillis(connectionTimeout)).block();
logger.debug("\n-----------------------------------------------------Login Initiated-----------------------------------------------------\n* Request : {}\n* Session Id : {}\n-----------------------------------------------------End of Login-----------------------------------------------------", requestBody, session);
} catch (Exception e) {
logger.warn("Failed to login for user {}", username);
}
}

public String post(String URL, String requestBody) {
try {
if(isSessionInvalid())
login();
try {
logger.debug("Post Data: {}", requestBody);
WebClient client = getWebClient(baseURL);
HttpHeaders headers = getHttpHeaders();
Expand Down

0 comments on commit 8421c81

Please sign in to comment.