| 1 |
use crate::{Transport, TransportDecorator}; |
| 2 |
use dromedary::pyo3::PyTransport; |
| 3 |
use pyo3::exceptions::PyValueError; |
| 4 |
use pyo3::prelude::*; |
| 5 |
|
| 6 |
const PREFIX: &str = "unlistable+"; |
| 7 |
|
| 8 |
fn resolve_inner(py: Python, url: &str, decorated: Option<Py<PyAny>>) -> PyResult<Py<PyAny>> { |
| 9 |
if let Some(d) = decorated { |
| 10 |
return Ok(d); |
| 11 |
} |
| 12 |
if !url.starts_with(PREFIX) { |
| 13 |
return Err(PyValueError::new_err(format!( |
| 14 |
"url {:?} doesn't start with decorator prefix {:?}", |
| 15 |
url, PREFIX |
| 16 |
))); |
| 17 |
} |
| 18 |
let rest = &url[PREFIX.len()..]; |
| 19 |
let dromedary = py.import("dromedary")?; |
| 20 |
let urlutils = py.import("dromedary.urlutils")?; |
| 21 |
let is_url: bool = urlutils.call_method1("is_url", (rest,))?.extract()?; |
| 22 |
let func = if is_url { |
| 23 |
dromedary.getattr("get_transport_from_url")? |
| 24 |
} else { |
| 25 |
dromedary.getattr("get_transport_from_path")? |
| 26 |
}; |
| 27 |
Ok(func.call1((rest,))?.unbind()) |
| 28 |
} |
| 29 |
|
| 30 |
fn wrap_inner(decorated: &Py<PyAny>, py: Python) -> Transport { |
| 31 |
let py_inner: Box<dyn dromedary::Transport + Send + Sync> = |
| 32 |
Box::new(PyTransport::from(decorated.clone_ref(py))); |
| 33 |
Transport(Box::new(dromedary::unlistable::UnlistableTransport::new( |
| 34 |
py_inner, |
| 35 |
))) |
| 36 |
} |
| 37 |
|
| 38 |
#[pyclass(extends=TransportDecorator, subclass)] |
| 39 |
pub(crate) struct UnlistableTransportDecorator; |
| 40 |
|
| 41 |
#[pymethods] |
| 42 |
impl UnlistableTransportDecorator { |
| 43 |
#[new] |
| 44 |
#[pyo3(signature = (url, _decorated=None, _from_transport=None))] |
| 45 |
fn new( |
| 46 |
py: Python, |
| 47 |
url: &str, |
| 48 |
_decorated: Option<Py<PyAny>>, |
| 49 |
_from_transport: Option<Py<PyAny>>, |
| 50 |
) -> PyResult<PyClassInitializer<Self>> { |
| 51 |
let _ = _from_transport; |
| 52 |
let decorated = resolve_inner(py, url, _decorated)?; |
| 53 |
let wrapped = wrap_inner(&decorated, py); |
| 54 |
Ok(PyClassInitializer::from(wrapped) |
| 55 |
.add_subclass(TransportDecorator { |
| 56 |
decorated, |
| 57 |
prefix: PREFIX, |
| 58 |
}) |
| 59 |
.add_subclass(UnlistableTransportDecorator)) |
| 60 |
} |
| 61 |
|
| 62 |
#[classmethod] |
| 63 |
fn _get_url_prefix(_cls: &Bound<'_, pyo3::types::PyType>) -> &'static str { |
| 64 |
PREFIX |
| 65 |
} |
| 66 |
|
| 67 |
#[pyo3(signature = (offset=None))] |
| 68 |
fn clone<'a>( |
| 69 |
slf: PyRef<'a, Self>, |
| 70 |
py: Python<'a>, |
| 71 |
offset: Option<Py<PyAny>>, |
| 72 |
) -> PyResult<Bound<'a, UnlistableTransportDecorator>> { |
| 73 |
let decorator: &TransportDecorator = slf.as_super(); |
| 74 |
let decorated = decorator.decorated.clone_ref(py); |
| 75 |
let decorated_clone = match offset { |
| 76 |
Some(o) => decorated.call_method1(py, "clone", (o,))?, |
| 77 |
None => decorated.call_method0(py, "clone")?, |
| 78 |
}; |
| 79 |
let wrapped = wrap_inner(&decorated_clone, py); |
| 80 |
let init = PyClassInitializer::from(wrapped) |
| 81 |
.add_subclass(TransportDecorator { |
| 82 |
decorated: decorated_clone, |
| 83 |
prefix: PREFIX, |
| 84 |
}) |
| 85 |
.add_subclass(UnlistableTransportDecorator); |
| 86 |
Bound::new(py, init) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
pub(crate) fn register(_py: Python, m: &Bound<PyModule>) -> PyResult<()> { |
| 91 |
m.add_class::<UnlistableTransportDecorator>()?; |
| 92 |
Ok(()) |
| 93 |
} |