Adding code for getting contact
catapult PR / Clippy (pull_request) Successful in 58s
catapult PR / Rustfmt (pull_request) Failing after 1m16s
catapult PR / Check (pull_request) Has been cancelled

This commit is contained in:
2026-06-25 21:19:35 -04:00
parent c3f93cce3c
commit ea71e6bfa2
4 changed files with 83 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
pub struct Contact {
pub app: crate::app::App,
pub token: textsender_models::token::LoginResult,
}
impl Contact {
pub async fn get(
&self,
contact_id: &uuid::Uuid,
) -> Result<Vec<textsender_models::contact::Contact>, std::io::Error>
{
let client = reqwest::Client::new();
let endpoint =
format!("api/v1/contact?id={contact_id}");
let api_url = format!("{}/{endpoint}", self.app.api_url);
println!("Url: {api_url:?}");
let (key, header) = super::auth_header(&self.token.access_token);
match client.get(api_url).header(key, header).send().await {
Ok(response) => match response.status() {
reqwest::StatusCode::OK => match response.text().await {
Ok(val) => match parse_response(&val).await {
Ok(login_result) => Ok(login_result),
Err(err) => Err(err),
},
Err(err) => Err(std::io::Error::other(err)),
},
_ => Err(std::io::Error::other("Error getting token")),
},
Err(err) => Err(std::io::Error::other(err.to_string())),
}
}
}
async fn parse_response(
response: &str,
) -> Result<Vec<textsender_models::contact::Contact>, std::io::Error> {
match serde_json::from_str::<serde_json::Value>(response) {
Ok(j) => match j.get("data") {
Some(serde_json::Value::Array(lrs)) => {
if lrs.is_empty() {
Err(std::io::Error::other("Error response is empty"))
} else {
let mut events: Vec<
textsender_models::contact::Contact,
> = Vec::new();
for event in lrs.iter() {
let ll: textsender_models::contact::Contact =
match serde_json::from_value(event.clone()) {
Ok(lr) => lr,
Err(err) => {
return Err(std::io::Error::other(err.to_string()));
}
};
events.push(ll);
}
Ok(events)
}
}
_ => Err(std::io::Error::other("Error parsing response")),
},
Err(err) => Err(std::io::Error::other(err.to_string())),
}
}
+13
View File
@@ -51,9 +51,22 @@ impl Service {
} }
}; };
let contacts = super::contact::Contact {
app: self.app.clone(),
token: self.token.clone()
};
let events = events.unwrap_or_default(); let events = events.unwrap_or_default();
for event in events { for event in events {
println!("Event: {event:?}"); println!("Event: {event:?}");
let contact = &match contacts.get(&event.contact_id).await {
Ok(contact) => contact,
Err(err) => {
eprintln!("Error: {err:?}");
continue;
}
}[0];
println!("Contact Id: {:?}", contact.id);
} }
todo!( todo!(
View File
+1
View File
@@ -1,3 +1,4 @@
pub mod contact;
pub mod core; pub mod core;
pub mod event; pub mod event;
pub mod queue; pub mod queue;