Refactor out requests
This commit is contained in:
parent
af0998a719
commit
5f3f417040
@ -1,12 +1,11 @@
|
||||
use crate::{
|
||||
model::{SectionId, Sections},
|
||||
section_runner::SectionRunner,
|
||||
};
|
||||
use eyre::WrapErr;
|
||||
use crate::{model::Sections, section_runner::SectionRunner};
|
||||
|
||||
use futures_util::FutureExt;
|
||||
use num_derive::FromPrimitive;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{fmt, future::Future, pin::Pin, time::Duration};
|
||||
use std::{fmt, future::Future, pin::Pin};
|
||||
|
||||
mod run_section;
|
||||
|
||||
pub struct RequestContext {
|
||||
pub sections: Sections,
|
||||
@ -164,50 +163,6 @@ trait IRequest {
|
||||
fn exec(&mut self, ctx: &mut RequestContext) -> RequestFuture;
|
||||
}
|
||||
|
||||
mod run_section {
|
||||
use super::*;
|
||||
use crate::section_runner::SectionRunHandle;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct RequestData {
|
||||
pub section_id: SectionId,
|
||||
#[serde(with = "crate::serde::duration")]
|
||||
pub duration: Duration,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ResponseData {
|
||||
pub message: String,
|
||||
pub run_id: SectionRunHandle,
|
||||
}
|
||||
|
||||
impl IRequest for RequestData {
|
||||
fn exec(&mut self, ctx: &mut RequestContext) -> RequestFuture {
|
||||
let mut section_runner = ctx.section_runner.clone();
|
||||
let section = ctx.sections.get(&self.section_id).cloned();
|
||||
let duration = self.duration;
|
||||
Box::pin(async move {
|
||||
let section = section.ok_or_else(|| {
|
||||
RequestError::with_name(ErrorCode::NotFound, "section not found", "section")
|
||||
})?;
|
||||
let handle = section_runner
|
||||
.queue_run(section.clone(), duration)
|
||||
.await
|
||||
.wrap_err("could not queue run")?;
|
||||
let res = ResponseData {
|
||||
message: format!("running section '{}' for {:?}", §ion.name, duration),
|
||||
run_id: handle,
|
||||
};
|
||||
let res_value =
|
||||
serde_json::to_value(res).wrap_err("could not serialize response")?;
|
||||
Ok(res_value)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase", tag = "type")]
|
||||
pub enum Request {
|
43
src/mqtt/request/run_section.rs
Normal file
43
src/mqtt/request/run_section.rs
Normal file
@ -0,0 +1,43 @@
|
||||
use super::*;
|
||||
use crate::{model::SectionId, section_runner::SectionRunHandle};
|
||||
use eyre::WrapErr;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::time::Duration;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct RequestData {
|
||||
pub section_id: SectionId,
|
||||
#[serde(with = "crate::serde::duration")]
|
||||
pub duration: Duration,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ResponseData {
|
||||
pub message: String,
|
||||
pub run_id: SectionRunHandle,
|
||||
}
|
||||
|
||||
impl IRequest for RequestData {
|
||||
fn exec(&mut self, ctx: &mut RequestContext) -> RequestFuture {
|
||||
let mut section_runner = ctx.section_runner.clone();
|
||||
let section = ctx.sections.get(&self.section_id).cloned();
|
||||
let duration = self.duration;
|
||||
Box::pin(async move {
|
||||
let section = section.ok_or_else(|| {
|
||||
RequestError::with_name(ErrorCode::NotFound, "section not found", "section")
|
||||
})?;
|
||||
let handle = section_runner
|
||||
.queue_run(section.clone(), duration)
|
||||
.await
|
||||
.wrap_err("could not queue run")?;
|
||||
let res = ResponseData {
|
||||
message: format!("running section '{}' for {:?}", §ion.name, duration),
|
||||
run_id: handle,
|
||||
};
|
||||
let res_value = serde_json::to_value(res).wrap_err("could not serialize response")?;
|
||||
Ok(res_value)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user