Skip to content

Commit

Permalink
Merge pull request #90 from lumeohq/dmitry/add-response-patcher
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitrySamoylov authored Mar 29, 2022
2 parents ed815ef + 64532c3 commit 9e80c95
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion onvif/src/soap/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use async_trait::async_trait;
use schema::transport::{Error, Transport};
use std::{
fmt::{Debug, Formatter},
sync::Arc,
time::Duration,
};
use url::Url;
Expand Down Expand Up @@ -48,6 +49,7 @@ impl ClientBuilder {
config: Config {
uri: uri.clone(),
credentials: None,
response_patcher: None,
auth_type: AuthType::Any,
timeout: Duration::from_secs(5),
},
Expand All @@ -59,6 +61,11 @@ impl ClientBuilder {
self
}

pub fn response_patcher(mut self, response_patcher: Option<ResponsePatcher>) -> Self {
self.config.response_patcher = response_patcher;
self
}

pub fn auth_type(mut self, auth_type: AuthType) -> Self {
self.config.auth_type = auth_type;
self
Expand Down Expand Up @@ -96,6 +103,7 @@ impl ClientBuilder {
struct Config {
uri: Url,
credentials: Option<Credentials>,
response_patcher: Option<ResponsePatcher>,
auth_type: AuthType,
timeout: Duration,
}
Expand All @@ -122,6 +130,8 @@ impl Debug for Credentials {
}
}

pub type ResponsePatcher = Arc<dyn Fn(&str) -> Result<String, String> + Send + Sync>;

#[derive(Debug)]
enum RequestAuthType {
Digest(Digest),
Expand Down Expand Up @@ -226,7 +236,19 @@ impl Client {
.map_err(|e| Error::Protocol(e.to_string()))
.and_then(|text| {
debug!(self, "Response body: {}", text);
soap::unsoap(&text).map_err(|e| Error::Protocol(format!("{:?}", e)))
let response =
soap::unsoap(&text).map_err(|e| Error::Protocol(format!("{:?}", e)))?;
if let Some(response_patcher) = &self.config.response_patcher {
match response_patcher(&response) {
Ok(patched) => {
debug!(self, "Response (SOAP unwrapped, patched): {}", patched);
Ok(patched)
}
Err(e) => Err(Error::Protocol(format!("Patching failed: {e}"))),

This comment has been minimized.

Copy link
@ngoluuduythai

ngoluuduythai Apr 10, 2022

error: there is no argument named e
--> /Users/zero/.cargo/git/checkouts/onvif-rs-92f68257d184bb96/9e80c95/onvif/src/soap/client.rs:247:85
|
247 | ... Err(e) => Err(Error::Protocol(format!("Patching failed: {e}"))),

we have error here.

This comment has been minimized.

Copy link
@FSMaxB

FSMaxB Apr 12, 2022

That is a rust feature available since version 1.58, see here https://blog.rust-lang.org/2022/01/13/Rust-1.58.0.html#captured-identifiers-in-format-strings

This comment has been minimized.

Copy link
@ngoluuduythai

ngoluuduythai Apr 13, 2022

Thanks, @FSMaxB. I got it, I have an error with rust version 1.56. I will update it.

}
} else {
Ok(response)
}
})
} else if status == reqwest::StatusCode::UNAUTHORIZED {
match auth_type {
Expand Down

0 comments on commit 9e80c95

Please sign in to comment.