Skip to main content

eduboardapi/services/
result.rs

1use crate::exceptions::{AppError, AppResult};
2use crate::models::{RequestData, ResultData};
3use crate::services::client::HttpClient;
4use crate::services::parser::parse_result;
5use crate::utils::captcha::solve_captcha;
6use serde::Serialize;
7
8#[derive(Serialize)]
9struct ResultForm<'a> {
10    sr: &'static str,
11    et: &'static str,
12    exam: &'a str,
13    year: &'a str,
14    board: &'a str,
15    roll: &'a str,
16    reg: &'a str,
17    value_s: &'a str,
18    button2: &'static str,
19}
20
21pub async fn fetch_result(client: &HttpClient, request: &RequestData) -> AppResult<ResultData> {
22    let (session, home) = client
23        .get_homepage()
24        .await
25        .map_err(|e| AppError::Network(e.to_string()))?;
26    let captcha = solve_captcha(&home)?;
27
28    let form = ResultForm {
29        sr: "3",
30        et: "2",
31        exam: request.exam.as_str(),
32        year: request.year.as_str(),
33        board: request.board.as_str(),
34        roll: request.roll.as_str(),
35        reg: request.reg.as_str(),
36        value_s: captcha.as_str(),
37        button2: "Submit",
38    };
39
40    let body = client
41        .submit_result_form(&session, &form)
42        .await
43        .map_err(|e| AppError::Network(e.to_string()))?;
44    let mut result = parse_result(&body)?;
45    if result.reg.is_none() {
46        result.reg = Some(request.reg.clone());
47    }
48    Ok(result)
49}