2020
2121//! This module implements Agama's HTTP API.
2222
23+ use crate :: server:: config_schema;
2324use agama_lib:: error:: ServiceError ;
2425use agama_manager:: { self as manager, message} ;
2526use agama_utils:: {
@@ -39,14 +40,18 @@ use axum::{
3940} ;
4041use hyper:: StatusCode ;
4142use serde:: Serialize ;
42- use serde_json:: { json, value:: RawValue } ;
43+ use serde_json:: { json, value:: RawValue , Value } ;
4344
4445#[ derive( thiserror:: Error , Debug ) ]
4546pub enum Error {
4647 #[ error( transparent) ]
4748 Manager ( #[ from] manager:: service:: Error ) ,
4849 #[ error( transparent) ]
4950 Questions ( #[ from] question:: service:: Error ) ,
51+ #[ error( transparent) ]
52+ ConfigSchema ( #[ from] config_schema:: Error ) ,
53+ #[ error( transparent) ]
54+ Json ( #[ from] serde_json:: Error ) ,
5055}
5156
5257impl IntoResponse for Error {
@@ -179,13 +184,12 @@ async fn get_config(State(state): State<ServerState>) -> ServerResult<Json<Confi
179184 ( status = 400 , description = "Not possible to replace the configuration." )
180185 ) ,
181186 params(
182- ( "config" = Config , description = "Configuration to apply." )
187+ ( "config" = Value , description = "Configuration to apply." )
183188 )
184189) ]
185- async fn put_config (
186- State ( state) : State < ServerState > ,
187- Json ( config) : Json < Config > ,
188- ) -> ServerResult < ( ) > {
190+ async fn put_config ( State ( state) : State < ServerState > , Json ( json) : Json < Value > ) -> ServerResult < ( ) > {
191+ config_schema:: check ( & json) ?;
192+ let config = serde_json:: from_value ( json) ?;
189193 state. manager . call ( message:: SetConfig :: new ( config) ) . await ?;
190194 Ok ( ( ) )
191195}
@@ -202,14 +206,16 @@ async fn put_config(
202206 ( status = 400 , description = "Not possible to patch the configuration." )
203207 ) ,
204208 params(
205- ( "config" = Config , description = "Changes in the configuration." )
209+ ( "config" = config :: Patch , description = "Changes in the configuration." )
206210 )
207211) ]
208212async fn patch_config (
209213 State ( state) : State < ServerState > ,
210214 Json ( patch) : Json < config:: Patch > ,
211215) -> ServerResult < ( ) > {
212- if let Some ( config) = patch. update {
216+ if let Some ( json) = patch. update {
217+ config_schema:: check ( & json) ?;
218+ let config = serde_json:: from_value ( json) ?;
213219 state
214220 . manager
215221 . call ( message:: UpdateConfig :: new ( config) )
0 commit comments