Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion rnet.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1399,9 +1399,19 @@ class HeaderMap:
def __contains__(self, key: str) -> builtins.bool: ...
def __len__(self) -> builtins.int: ...
def __iter__(self) -> HeaderMapKeysIter: ...
def items(self) -> HeaderMapItemsIter: ...
def __str__(self) -> builtins.str: ...
def __repr__(self) -> builtins.str: ...
def get_all(self, key: str) -> HeaderMapValuesIter:
r"""
Returns multiple value sequences of key mapping
"""
...

def items(self) -> HeaderMapItemsIter:
r"""
Returns key-value pairs in the order they were added.
"""
...

class HeaderMapItemsIter:
r"""
Expand All @@ -1421,6 +1431,14 @@ class HeaderMapKeysIter:
def __iter__(self) -> HeaderMapKeysIter: ...
def __next__(self) -> typing.Optional[typing.Any]: ...

class HeaderMapValuesIter:
r"""
An iterator over the values in a HeaderMap.
"""

def __iter__(self) -> HeaderMapValuesIter: ...
def __next__(self) -> typing.Optional[typing.Any]: ...

class ImpersonateOption:
r"""
A struct to represent the `ImpersonateOption` class.
Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use pyo3_async_runtimes::tokio::future_into_py;
use pyo3_stub_gen::{define_stub_info_gatherer, derive::*};
use typing::param::{RequestParams, WebSocketParams};
use typing::{
Cookie, HeaderMap, HeaderMapItemsIter, HeaderMapKeysIter, Impersonate, ImpersonateOS,
ImpersonateOption, LookupIpStrategy, Method, Multipart, Part, Proxy, SameSite, SocketAddr,
StatusCode, TlsVersion, Version,
Cookie, HeaderMap, HeaderMapItemsIter, HeaderMapKeysIter, HeaderMapValuesIter, Impersonate,
ImpersonateOS, ImpersonateOption, LookupIpStrategy, Method, Multipart, Part, Proxy, SameSite,
SocketAddr, StatusCode, TlsVersion, Version,
};

#[cfg(not(target_env = "msvc"))]
Expand Down Expand Up @@ -323,6 +323,7 @@ fn rnet(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<HeaderMap>()?;
m.add_class::<HeaderMapItemsIter>()?;
m.add_class::<HeaderMapKeysIter>()?;
m.add_class::<HeaderMapValuesIter>()?;
m.add_class::<Impersonate>()?;
m.add_class::<ImpersonateOS>()?;
m.add_class::<ImpersonateOption>()?;
Expand Down
53 changes: 46 additions & 7 deletions src/typing/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ impl HeaderMap {
Self(headers)
}

/// Returns multiple value sequences of key mapping
fn get_all(&self, key: PyBackedStr) -> HeaderMapValuesIter {
HeaderMapValuesIter {
inner: self
.0
.get_all(key.as_ref() as &str)
.iter()
.cloned()
.collect(),
}
}

/// Returns key-value pairs in the order they were added.
#[inline]
fn items(&self) -> HeaderMapItemsIter {
HeaderMapItemsIter {
inner: self.0.iter().map(|(k, v)| (k.clone(), v.clone())).collect(),
}
}
}

#[cfg_attr(feature = "docs", gen_stub_pymethods)]
#[pymethods]
impl HeaderMap {
#[inline]
fn __getitem__<'py>(&self, py: Python<'py>, key: PyBackedStr) -> Option<Bound<'py, PyAny>> {
let value = self.0.get(key.as_ref() as &str)?;
Expand Down Expand Up @@ -89,13 +113,6 @@ impl HeaderMap {
}
}

#[inline]
fn items(&self) -> HeaderMapItemsIter {
HeaderMapItemsIter {
inner: self.0.iter().map(|(k, v)| (k.clone(), v.clone())).collect(),
}
}

#[inline]
fn __str__(&self) -> String {
format!("{:?}", self.0)
Expand Down Expand Up @@ -130,6 +147,28 @@ impl HeaderMapKeysIter {
}
}

/// An iterator over the values in a HeaderMap.
#[cfg_attr(feature = "docs", gen_stub_pyclass)]
#[pyclass]
pub struct HeaderMapValuesIter {
inner: Vec<HeaderValue>,
}
#[cfg_attr(feature = "docs", gen_stub_pymethods)]
#[pymethods]
impl HeaderMapValuesIter {
#[inline]
fn __iter__(slf: PyRefMut<'_, Self>) -> PyRefMut<'_, Self> {
slf
}

#[inline]
fn __next__(mut slf: PyRefMut<Self>) -> Option<Bound<'_, PyAny>> {
slf.inner
.pop()
.and_then(|v| HeaderValueBuffer::new(v).into_bytes_ref(slf.py()).ok())
}
}

/// An iterator over the items in a HeaderMap.
#[cfg_attr(feature = "docs", gen_stub_pyclass)]
#[pyclass]
Expand Down
3 changes: 2 additions & 1 deletion src/typing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ pub use self::{
cookie::{Cookie, CookieExtractor},
enums::{Impersonate, ImpersonateOS, LookupIpStrategy, Method, SameSite, TlsVersion, Version},
headers::{
HeaderMap, HeaderMapExtractor, HeaderMapItemsIter, HeaderMapKeysIter, HeadersOrderExtractor,
HeaderMap, HeaderMapExtractor, HeaderMapItemsIter, HeaderMapKeysIter, HeaderMapValuesIter,
HeadersOrderExtractor,
},
ipaddr::{IpAddr, SocketAddr},
json::Json,
Expand Down
Loading