reactive_graph_type_system_rest/
flows.rs

1use std::str::FromStr;
2use std::sync::Arc;
3
4use actix_web::HttpResponse;
5use actix_web::get;
6use actix_web::web;
7use mime::APPLICATION_JSON;
8
9use crate::json_schema_response;
10use reactive_graph_graph::FlowTypeId;
11use reactive_graph_type_system_api::FlowTypeManager;
12use reactive_graph_type_system_json_schema::flows::schema_flow_types;
13
14#[get("/types/flows")]
15pub async fn get_flow_types(flow_type_manager: web::Data<Arc<dyn FlowTypeManager + Send + Sync>>) -> HttpResponse {
16    HttpResponse::Ok().content_type(APPLICATION_JSON.to_string()).json(flow_type_manager.get_all())
17}
18
19#[get("/types/flows/{namespace:.*}")]
20pub async fn get_flow_type(path: web::Path<String>, flow_type_manager: web::Data<Arc<dyn FlowTypeManager + Send + Sync>>) -> HttpResponse {
21    let namespace = path.into_inner();
22    let ty = match FlowTypeId::from_str(&namespace) {
23        Ok(ty) => ty,
24        Err(e) => {
25            return HttpResponse::NotFound().content_type(APPLICATION_JSON.to_string()).body(e.to_string());
26        }
27    };
28    match flow_type_manager.get(&ty) {
29        Some(flow_type) => HttpResponse::Ok().content_type(APPLICATION_JSON.to_string()).json(&flow_type),
30        None => HttpResponse::NotFound()
31            .content_type(APPLICATION_JSON.to_string())
32            .body(format!("Flow Type {ty} not found")),
33    }
34}
35
36#[get("/types/flows/schema")]
37pub async fn json_schema_flow_types() -> HttpResponse {
38    json_schema_response(schema_flow_types())
39}