diff --git a/src/models/client.rs b/src/models/client.rs index 0ca992c..11e9045 100644 --- a/src/models/client.rs +++ b/src/models/client.rs @@ -9,16 +9,24 @@ use crate::utils::parser::{length, Oblivion, OblivionPath}; use p256::ecdh::EphemeralSecret; use p256::PublicKey; -use serde_json::{from_str, json, Value}; use tokio::net::TcpStream; +#[cfg(feature = "python")] +use pyo3::prelude::*; +#[cfg(not(feature = "python"))] +use serde_json::{from_str, json, Value}; +#[cfg(feature = "python")] +use serde_json::{json, Value}; + +#[cfg_attr(feature = "python", pyclass)] pub struct Response { - header: String, - content: Vec, - olps: String, - status_code: i32, + pub header: String, + pub content: Vec, + pub olps: String, + pub status_code: i32, } +#[cfg(not(feature = "python"))] impl Response { pub fn new(header: String, content: Vec, olps: String, status_code: i32) -> Self { Self { @@ -54,6 +62,49 @@ impl Response { } } +#[cfg(feature = "python")] +#[pyclass] +pub struct PyOblivionException { + pub message: String, +} + +#[cfg(feature = "python")] +#[pymethods] +impl PyOblivionException { + #[new] + fn new(message: String) -> Self { + Self { message } + } +} + +#[cfg(feature = "python")] +#[pymethods] +impl Response { + #[new] + fn new(header: String, content: Vec, olps: String, status_code: i32) -> Self { + Self { + header, + content, + olps, + status_code, + } + } + + fn ok(&self) -> bool { + self.status_code < 400 + } + + fn text(&mut self) -> PyResult { + match String::from_utf8(self.content.to_vec()) { + Ok(text) => Ok(text), + Err(_) => Err(PyErr::new::(format!( + "Invalid Oblivion: {}", + self.olps + ))), + } + } +} + pub struct Request { method: String, olps: String,