|
| 1 | +use crate::backend::ServiceProvider; |
| 2 | +use crate::AppState; |
| 3 | +use axum::http::header; |
| 4 | +use axum::http::StatusCode; |
| 5 | +use axum::response::IntoResponse; |
| 6 | +use axum::routing::get; |
| 7 | +use axum::Router; |
| 8 | +use axum_extra::extract::Query; |
| 9 | +use axum_streams::StreamBodyAs; |
| 10 | +use dicom::object::InMemDicomObject; |
| 11 | +use dicom_json::DicomJson; |
| 12 | +use futures::TryStreamExt; |
| 13 | +use tracing::instrument; |
| 14 | + |
| 15 | +use super::{MwlQueryParameters, MwlRequestHeaderFields, MwlSearchError, MwlSearchRequest}; |
| 16 | + |
| 17 | +/// HTTP Router for the Modality Worklist. |
| 18 | +/// |
| 19 | +/// <https://www.dicomstandard.org/news-dir/current/docs/sups/sup246.pdf> |
| 20 | +#[rustfmt::skip] |
| 21 | +pub fn routes() -> Router<AppState> { |
| 22 | + Router::new() |
| 23 | + .route("/modality-scheduled-procedure-steps", get(all_workitems)) |
| 24 | +} |
| 25 | + |
| 26 | +// MWL-RS implementation |
| 27 | +async fn mwl_handler(provider: ServiceProvider, request: MwlSearchRequest) -> impl IntoResponse { |
| 28 | + if let Some(mwl) = provider.mwl { |
| 29 | + let response = mwl.search(request).await; |
| 30 | + let matches: Result<Vec<InMemDicomObject>, MwlSearchError> = |
| 31 | + response.stream.try_collect().await; |
| 32 | + |
| 33 | + match matches { |
| 34 | + Ok(matches) => { |
| 35 | + if matches.is_empty() { |
| 36 | + StatusCode::NO_CONTENT.into_response() |
| 37 | + } else { |
| 38 | + let json: Vec<DicomJson<InMemDicomObject>> = |
| 39 | + matches.into_iter().map(DicomJson::from).collect(); |
| 40 | + |
| 41 | + axum::response::Response::builder() |
| 42 | + .status(StatusCode::OK) |
| 43 | + .header(header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) |
| 44 | + .body(StreamBodyAs::json_array(futures::stream::iter(json))) |
| 45 | + .unwrap() |
| 46 | + .into_response() |
| 47 | + } |
| 48 | + } |
| 49 | + Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response(), |
| 50 | + } |
| 51 | + } else { |
| 52 | + ( |
| 53 | + StatusCode::SERVICE_UNAVAILABLE, |
| 54 | + "MWL-RS endpoint is disabled", |
| 55 | + ) |
| 56 | + .into_response() |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +#[instrument(skip_all)] |
| 61 | +async fn all_workitems( |
| 62 | + provider: ServiceProvider, |
| 63 | + Query(parameters): Query<MwlQueryParameters>, |
| 64 | +) -> impl IntoResponse { |
| 65 | + let request = MwlSearchRequest { |
| 66 | + parameters, |
| 67 | + headers: MwlRequestHeaderFields::default(), |
| 68 | + }; |
| 69 | + mwl_handler(provider, request).await |
| 70 | +} |
0 commit comments