I have an api written in Rust but since a lot of colleagues only know Python I am trying to make it possible to build data pipelines in Python within the rust program.
The idea would be make a Python interface to the rust code and vice versa. Then from specific endpoints I would call a python module that could interact with the rust repositories etc.
A rough sketch of how I would like it to work (I am not too familiar yet with all the macros etc of pyo3 but I think the intent is clear):
Rust code:
struct DocumentRepository {
database: postgres_database
}
#[pyclass]
impl DocumentRepository {
#[pymethod]
fn document(&self, name: String) -> Document {
self.database.document(name)
}
}
struct Application {
document_repository: DocumentRepository
}
impl Application {
get_document_in_python(&self, name: String) -> Document {
let code = include_str!("../python_code");
pyo3::prepare_freethreaded_python();
Python::with_gil::<_, String>(|py| {
let get_document: Py<PyAny> = PyModule::from_code(py, code, "example", "example")
let document: Document = get_document.call(py, (), None).unwrap().extract(py).unwrap();
document
})
}
}
Python code:
def get_document(name):
// somehow interact with the rust application and get the document
// return document
Since the application owns the connections to the database through the documentrepository (which is something I'd rather not change), the python code would need to interact with that specific instance of the struct to actually get the documents. Is this possible? Can I somehow pass a reference to the instance of the application struct and get the documents through this?