Skip to content

Commit

Permalink
fix: minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
edytapawlak committed Jul 19, 2024
1 parent e3b7f23 commit bec8bb1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
41 changes: 30 additions & 11 deletions components/watcher/src/watcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,21 @@ impl Watcher {
}
}

pub async fn process_update_tel_requests(&self) {
pub async fn process_update_tel_requests(&self) -> Result<(), ActorError> {
if let Ok((ri, vc_id)) = self.tel_recv.try_recv() {
let who_ask = self.registry_id_mapping.get(&ri).unwrap();
println!("Need to ask about {}, source: {}", ri, who_ask);
let who_to_ask = self
.registry_id_mapping
.get(&ri)
.ok_or(ActorError::GeneralError(format!(
"Can't find TEL fo id: {}",
ri
)))?;

self.watcher_data
.tel_update(&ri, &vc_id, who_ask.clone())
.await
.unwrap();
}
.tel_update(&ri, &vc_id, who_to_ask.clone())
.await?;
};
Ok(())
}

pub fn oobi(&self) -> LocationScheme {
Expand Down Expand Up @@ -117,7 +122,7 @@ impl Watcher {
}
}
WitnessResp::Tel(tel_events) => {
// check tel event?
// check tel event
for ev in tel_events {
let digest = ev.event.get_digest().unwrap();
let issuer_id = match ev.event {
Expand Down Expand Up @@ -209,7 +214,9 @@ impl Watcher {
&self,
input_stream: &[u8],
) -> Result<Vec<TelReplyType>, ActorError> {
let tel_queries = parse_tel_query_stream(input_stream).unwrap().into_iter();
let tel_queries = parse_tel_query_stream(input_stream)
.map_err(|_e| ActorError::GeneralError("Can't parse TEL query stream".to_string()))?
.into_iter();

let mut out = vec![];
for qry in tel_queries {
Expand All @@ -218,8 +225,16 @@ impl Watcher {
TelQueryRoute::Tels {
reply_route: _,
args,
} => (args.ri.unwrap(), args.i.unwrap()),
} => match (args.ri, args.i) {
(Some(ri), Some(i)) => (ri, i),
_ => {
return Err(ActorError::GeneralError(
"Wrong TEL query format. `ri` and `i` field required".to_string(),
))
}
},
};

// Check if you have tel to forward
match self
.watcher_data
Expand All @@ -234,7 +249,11 @@ impl Watcher {
.tel_tx
.send((ri.clone(), vc_id.clone()))
.await
.unwrap();
.map_err(|_e| {
ActorError::GeneralError(
"Internal watcher error: channel problem".to_string(),
)
})?;
}
};
}
Expand Down
14 changes: 7 additions & 7 deletions components/watcher/src/watcher_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ impl WatcherListener {
)
.route(
"/oobi/{id}",
actix_web::web::get().to(http_handlers::get_eid_oobi),
actix_web::web::get().to(http_handlers::resolve_location),
)
.route(
"/oobi/{cid}/{role}/{eid}",
actix_web::web::get().to(http_handlers::get_cid_oobi),
actix_web::web::get().to(http_handlers::resolve_role),
)
.route(
"/process",
Expand Down Expand Up @@ -80,7 +80,7 @@ impl WatcherListener {
pub async fn update_checking(data: Arc<Watcher>) {
loop {
data.process_update_requests().await;
data.process_update_tel_requests().await;
let _ = data.process_update_tel_requests().await;
}
}

Expand Down Expand Up @@ -189,7 +189,7 @@ pub mod http_handlers {
Ok(HttpResponse::Ok().finish())
}

pub async fn get_eid_oobi(
pub async fn resolve_location(
eid: web::Path<IdentifierPrefix>,
data: web::Data<Arc<Watcher>>,
) -> Result<HttpResponse, ApiError> {
Expand All @@ -209,7 +209,7 @@ pub mod http_handlers {
.body(String::from_utf8(oobis).unwrap()))
}

pub async fn get_cid_oobi(
pub async fn resolve_role(
path: web::Path<(IdentifierPrefix, Role, IdentifierPrefix)>,
data: web::Data<Arc<Watcher>>,
) -> Result<HttpResponse, ApiError> {
Expand Down Expand Up @@ -340,7 +340,7 @@ mod test {
}
async fn request_loc_scheme(&self, eid: IdentifierPrefix) -> Result<Vec<Op>, ActorError> {
let data = actix_web::web::Data::new(self.watcher.clone());
let resp = super::http_handlers::get_eid_oobi(eid.into(), data)
let resp = super::http_handlers::resolve_location(eid.into(), data)
.await
.map_err(|err| err.0)?;
let resp = resp.into_body().try_into_bytes().unwrap();
Expand All @@ -354,7 +354,7 @@ mod test {
eid: IdentifierPrefix,
) -> Result<Vec<u8>, ActorError> {
let data = actix_web::web::Data::new(self.watcher.clone());
let resp = super::http_handlers::get_cid_oobi((cid, role, eid).into(), data)
let resp = super::http_handlers::resolve_role((cid, role, eid).into(), data)
.await
.map_err(|err| err.0)?;
let resp = resp.into_body().try_into_bytes().unwrap();
Expand Down

0 comments on commit bec8bb1

Please # to comment.