aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2022-11-05 21:36:52 +0100
committerHimbeerserverDE <himbeerserverde@gmail.com>2022-11-05 21:36:52 +0100
commit0fbf72f2a40f8812cdcabd1bad57da01857eaff1 (patch)
tree5bd850a4ca55d5ef9cd2955049bc056fbcdb2ee5
parent249991412bf71615427ad008e19a206f831dbf5b (diff)
simplify response verification
-rw-r--r--Cargo.toml2
-rw-r--r--src/call/account.rs3
-rw-r--r--src/client.rs34
-rw-r--r--src/error.rs2
-rw-r--r--src/response/account.rs20
-rw-r--r--src/response/mod.rs20
-rw-r--r--src/response/nameserver.rs7
7 files changed, 62 insertions, 26 deletions
diff --git a/Cargo.toml b/Cargo.toml
index e99a664..75406fb 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -19,4 +19,4 @@ features = ["blocking", "cookies", "rustls-tls"]
[dependencies.serde_xmlrpc]
version = "0.1.1"
git = "https://github.com/HimbeerserverDE/serde_xml_rpc.git"
-branch = "serializer"
+branch = "option_deserialize"
diff --git a/src/call/account.rs b/src/call/account.rs
index 73d6dab..011c447 100644
--- a/src/call/account.rs
+++ b/src/call/account.rs
@@ -21,7 +21,7 @@ impl Call for Login {
}
}
-impl Response<()> for Login {}
+impl Response<crate::response::account::Login> for Login {}
// Contains no information. This just signals to the server
// that it should end the session.
@@ -32,6 +32,7 @@ impl Call for Logout {
fn method_name(&self) -> String {
String::from("account.logout")
}
+
fn expected(&self) -> Vec<i32> {
vec![1500]
}
diff --git a/src/client.rs b/src/client.rs
index ebe300a..d03e768 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -1,4 +1,5 @@
-use crate::call::{self, Response};
+use crate::call;
+use crate::response::{self, ResponseData};
use crate::{Error, Result};
use std::sync::Arc;
@@ -60,11 +61,13 @@ impl Client {
/// matches one of the expected status codes.
pub fn call<T, U>(&self, call: T) -> Result<U>
where
- T: call::Call + Response<U>,
- U: serde::de::DeserializeOwned,
+ T: call::Call + call::Response<U>,
+ U: response::Response + Clone + serde::de::DeserializeOwned,
{
let expected = call.expected();
let xml = serde_xmlrpc::request_to_str(&call.method_name(), vec![call])?;
+ println!("{}", xml);
+ println!("----- SEPERATOR -----");
let raw_response = self
.inner
@@ -74,29 +77,14 @@ impl Client {
.send()?
.text()?;
- let map = serde_xmlrpc::value_from_str(&raw_response)?;
+ println!("{}", raw_response);
+ let resp: ResponseData<U> = serde_xmlrpc::response_from_str(&raw_response)?;
- let resp = map
- .as_struct()
- .ok_or_else(|| Error::MalformedResponse(map.clone()))?;
-
- let code = resp
- .get("code")
- .ok_or_else(|| Error::MalformedResponse(map.clone()))?
- .as_i32()
- .ok_or_else(|| Error::MalformedResponse(map.clone()))?;
-
- if !expected.contains(&code) {
- return Err(Error::BadStatus(expected, code));
+ if !expected.contains(&resp.status) {
+ return Err(Error::BadStatus(expected, resp.status));
}
- let data = resp
- .get("resData")
- .ok_or_else(|| Error::MalformedResponse(map.clone()))?;
-
- let res_data = serde_xmlrpc::value_to_string(data.clone())?;
-
- Ok(serde_xmlrpc::response_from_str(&res_data)?)
+ Ok(U::unwrap(resp.params))
}
}
diff --git a/src/error.rs b/src/error.rs
index e5238c6..1105968 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -7,7 +7,7 @@ pub enum Error {
Reqwest(reqwest::Error),
SerdeXmlRpc(serde_xmlrpc::Error),
Inexistent(String),
- MalformedResponse(serde_xmlrpc::Value),
+ MalformedResponse(String),
BadStatus(Vec<i32>, i32),
}
diff --git a/src/response/account.rs b/src/response/account.rs
new file mode 100644
index 0000000..b026631
--- /dev/null
+++ b/src/response/account.rs
@@ -0,0 +1,20 @@
+use super::Response;
+
+use serde_derive::{Deserialize, Serialize};
+
+#[derive(Clone, Debug, Deserialize, Serialize)]
+pub(crate) struct Login {
+ #[serde(rename = "customerId")]
+ pub customer_id: i32,
+ #[serde(rename = "customerNo")]
+ pub customer_number: i32,
+ #[serde(rename = "accountId")]
+ pub account_id: i32,
+ pub tfa: String,
+}
+
+impl Response for Login {
+ fn unwrap(wrapped: Option<Self>) -> Self {
+ wrapped.unwrap()
+ }
+}
diff --git a/src/response/mod.rs b/src/response/mod.rs
index aa5854f..0479dd1 100644
--- a/src/response/mod.rs
+++ b/src/response/mod.rs
@@ -1 +1,21 @@
+use serde_derive::{Deserialize, Serialize};
+
+#[derive(Clone, Debug, Deserialize, Serialize)]
+#[serde(bound(deserialize = "T: Clone + serde::de::DeserializeOwned"))]
+pub struct ResponseData<T> {
+ #[serde(rename = "code")]
+ pub status: i32,
+ #[serde(rename = "resData")]
+ pub params: Option<T>,
+}
+
+pub trait Response: Sized {
+ fn unwrap(_: Option<Self>) -> Self;
+}
+
+impl Response for () {
+ fn unwrap(_: Option<Self>) -> Self {}
+}
+
+pub mod account;
pub mod nameserver;
diff --git a/src/response/nameserver.rs b/src/response/nameserver.rs
index 76e7c85..e4e86df 100644
--- a/src/response/nameserver.rs
+++ b/src/response/nameserver.rs
@@ -1,3 +1,4 @@
+use super::Response;
use crate::common::nameserver::SlaveDns;
use serde_derive::{Deserialize, Serialize};
@@ -49,3 +50,9 @@ pub struct RecordInfo {
#[serde(rename = "record")]
pub records: Option<Vec<Record>>,
}
+
+impl Response for RecordInfo {
+ fn unwrap(wrapped: Option<Self>) -> Self {
+ wrapped.unwrap()
+ }
+}