initial commit
This commit is contained in:
105
src/main/java/com/rit/whatsapp/service/ImsApiService.java
Normal file
105
src/main/java/com/rit/whatsapp/service/ImsApiService.java
Normal file
@@ -0,0 +1,105 @@
|
||||
package com.rit.whatsapp.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class ImsApiService {
|
||||
|
||||
private final String BASE_URL = "https://ims-api.sidharthprabhu.co.in";
|
||||
private final RestTemplate restTemplate;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public ImsApiService() {
|
||||
this.restTemplate = new RestTemplate();
|
||||
this.objectMapper = new ObjectMapper();
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticates the user against the real IMS backend.
|
||||
* @param imsId The user ID
|
||||
* @param passcode The passcode
|
||||
* @return The API session token if successful, null otherwise.
|
||||
*/
|
||||
public String authenticate(String imsId, String passcode) {
|
||||
String url = BASE_URL + "/api/auth/login";
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
|
||||
Map<String, String> requestBody = new HashMap<>();
|
||||
requestBody.put("username", imsId);
|
||||
requestBody.put("password", passcode);
|
||||
|
||||
HttpEntity<Map<String, String>> request = new HttpEntity<>(requestBody, headers);
|
||||
|
||||
try {
|
||||
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
|
||||
if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) {
|
||||
JsonNode rootNode = objectMapper.readTree(response.getBody());
|
||||
if (rootNode.has("success") && rootNode.get("success").asBoolean()) {
|
||||
if (rootNode.has("session")) {
|
||||
return rootNode.get("session").asText();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to authenticate with IMS API: " + e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private String fetchFromIms(String endpoint, String token) {
|
||||
String url = BASE_URL + endpoint;
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setBearerAuth(token);
|
||||
|
||||
HttpEntity<String> request = new HttpEntity<>(headers);
|
||||
|
||||
try {
|
||||
ResponseEntity<String> response = restTemplate.exchange(url, org.springframework.http.HttpMethod.GET, request, String.class);
|
||||
if (response.getStatusCode().is2xxSuccessful()) {
|
||||
return response.getBody();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to fetch from IMS API (" + endpoint + "): " + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getAttendance(String token) {
|
||||
return fetchFromIms("/api/student/attendance", token);
|
||||
}
|
||||
|
||||
public String getTimetable(String token) {
|
||||
return fetchFromIms("/api/student/timetable", token);
|
||||
}
|
||||
|
||||
public String getResults(String token, int semester) {
|
||||
return fetchFromIms("/api/student/results?semester=" + semester, token);
|
||||
}
|
||||
|
||||
public void logout(String token) {
|
||||
String url = BASE_URL + "/api/auth/logout";
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setBearerAuth(token);
|
||||
|
||||
HttpEntity<String> request = new HttpEntity<>(headers);
|
||||
|
||||
try {
|
||||
restTemplate.postForEntity(url, request, String.class);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to logout from IMS API: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user