use crate::{
models::request_validators::{CertificationStatusRequest, Paths},
send_certification_status_request as native_send_certification_status_request,
};
use pyo3::{
exceptions::PyRuntimeError, prelude::*, types::PyString, wrap_pyfunction, Py, PyAny, PyResult,
Python,
};
use serde_json;
#[pyfunction]
fn send_certification_request(py: Python, url: String) -> PyResult<Py<PyAny>> {
let request_body = CertificationStatusRequest::new(Paths::default())
.map_err(|e| PyRuntimeError::new_err(format!("failed to create request: {e}")))?;
let response = native_send_certification_status_request(url, &request_body);
match response {
Ok(response_value) => {
let json_str = serde_json::json!(response_value).to_string();
let json = PyString::new(py, &json_str);
let json_module = py.import("json")?;
let json_object: Py<PyAny> = json_module.call_method1("loads", (json,))?.into();
Ok(json_object)
}
Err(e) => Err(PyErr::new::<PyRuntimeError, _>(format!(
"Request failed: {e}"
))),
}
}
#[pymodule]
fn hwlib(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(send_certification_request, m)?)?;
Ok(())
}