[Added] Nightly filling up of fresh data (and swapping), Startup filling of DB, sessionToken for bulk gathering
This commit is contained in:
@@ -12,6 +12,7 @@ import com.ninjaws.quiz.models.ApiResponse;
|
||||
import com.ninjaws.quiz.models.CategoryResponse;
|
||||
import com.ninjaws.quiz.models.QuizSettings;
|
||||
import com.ninjaws.quiz.models.Request;
|
||||
import com.ninjaws.quiz.models.SessionTokenDTO;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@@ -28,6 +29,7 @@ public class ApiService {
|
||||
private static final System.Logger logger = System.getLogger(ApiService.class.getName());
|
||||
|
||||
private final String apiUrl = "https://opentdb.com";
|
||||
private String sessionToken;
|
||||
|
||||
public ApiService() {
|
||||
}
|
||||
@@ -55,21 +57,42 @@ public class ApiService {
|
||||
* Store them in the DataService
|
||||
*/
|
||||
public void collectBulk() {
|
||||
// TODO: Make this happen only once, when a new database is generated
|
||||
getCategories();
|
||||
String url = apiUrl + "/api.php?amount=50";
|
||||
String tokenStr = sessionToken == null ? "" : "&token="+sessionToken;
|
||||
String url = apiUrl + "/api.php?amount=50" + tokenStr;
|
||||
try {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
|
||||
String json = response.getBody();
|
||||
ApiResponse apiResponse = cleanerService.extractApiResponse(json);
|
||||
responseHandler.handleResponse(apiResponse);
|
||||
|
||||
/**
|
||||
* A bit dirty, but otherwise it causes circular dependencies
|
||||
* Makes sure the categories are refreshed after finishing to replace the questions, to make sure the ids match
|
||||
* And resets the sessionToken, it will be remade during the next REFRESH
|
||||
*/
|
||||
if(apiResponse.getResponseCode() == 4) {
|
||||
getCategories();
|
||||
sessionToken = null;
|
||||
}
|
||||
|
||||
} catch (RestClientException | IOException e) {
|
||||
logger.log(System.Logger.Level.ERROR, "Critical failure", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void getSessionToken() {
|
||||
String url = apiUrl + "/api_token.php?command=request";
|
||||
try {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
|
||||
String json = response.getBody();
|
||||
SessionTokenDTO tokenDto = cleanerService.extractSessionToken(json);
|
||||
sessionToken = tokenDto.getToken();
|
||||
|
||||
} catch (RestClientException | IOException e) {
|
||||
logger.log(System.Logger.Level.ERROR, "Critical failure", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void getCategories() {
|
||||
String url = apiUrl + "/api_category.php";
|
||||
|
||||
Reference in New Issue
Block a user