@@ -587,49 +587,56 @@ fn actions_handler_POST(
587587
588588 let message = message. as_object ( ) . unwrap ( ) ;
589589
590- let mut response: serde_json:: Map < String , serde_json:: Value > = serde_json:: Map :: new ( ) ;
591- for ( action_name, action_params) in message. iter ( ) {
592- let input = action_params. get ( "input" ) ;
590+ let keys: Vec < & String > = message. keys ( ) . collect ( ) ;
591+ if keys. len ( ) != 1 {
592+ return HttpResponse :: BadRequest ( ) . finish ( ) ;
593+ }
593594
594- let action = req. state ( ) . get_action_generator ( ) . generate (
595- Arc :: downgrade ( & thing. clone ( ) ) ,
596- action_name. to_string ( ) ,
597- input,
598- ) ;
595+ let action_name = keys[ 0 ] ;
596+ let action_params = message. get ( action_name) . unwrap ( ) ;
597+ let input = action_params. get ( "input" ) ;
599598
600- if action. is_some ( ) {
601- let action = action. unwrap ( ) ;
602- let id = action. get_id ( ) ;
603- let action = Arc :: new ( RwLock :: new ( action) ) ;
599+ let action = req. state ( ) . get_action_generator ( ) . generate (
600+ Arc :: downgrade ( & thing. clone ( ) ) ,
601+ action_name. to_string ( ) ,
602+ input,
603+ ) ;
604604
605- {
606- let mut thing = thing. write ( ) . unwrap ( ) ;
607- let result = thing. add_action ( action. clone ( ) , input) ;
605+ if action. is_some ( ) {
606+ let action = action. unwrap ( ) ;
607+ let id = action. get_id ( ) ;
608+ let action = Arc :: new ( RwLock :: new ( action) ) ;
608609
609- if result. is_err ( ) {
610- continue ;
611- }
612- }
610+ {
611+ let mut thing = thing. write ( ) . unwrap ( ) ;
612+ let result = thing. add_action ( action. clone ( ) , input) ;
613613
614- response. insert (
615- action_name. to_string ( ) ,
616- action
617- . read ( )
618- . unwrap ( )
619- . as_action_description ( )
620- . get ( action_name)
621- . unwrap ( )
622- . clone ( ) ,
623- ) ;
614+ if result. is_err ( ) {
615+ return HttpResponse :: BadRequest ( ) . finish ( ) ;
616+ }
617+ }
624618
625- thing
626- . write ( )
619+ let mut response: serde_json:: Map < String , serde_json:: Value > = serde_json:: Map :: new ( ) ;
620+ response. insert (
621+ action_name. to_string ( ) ,
622+ action
623+ . read ( )
627624 . unwrap ( )
628- . start_action ( action_name. to_string ( ) , id) ;
629- }
630- }
625+ . as_action_description ( )
626+ . get ( action_name)
627+ . unwrap ( )
628+ . clone ( ) ,
629+ ) ;
630+
631+ thing
632+ . write ( )
633+ . unwrap ( )
634+ . start_action ( action_name. to_string ( ) , id) ;
631635
632- HttpResponse :: Created ( ) . json ( response)
636+ HttpResponse :: Created ( ) . json ( response)
637+ } else {
638+ HttpResponse :: BadRequest ( ) . finish ( )
639+ }
633640}
634641
635642/// Handle a GET request to /actions/<action_name>.
@@ -676,50 +683,59 @@ fn action_handler_POST(
676683
677684 let message = message. as_object ( ) . unwrap ( ) ;
678685
679- let mut response: serde_json:: Map < String , serde_json:: Value > = serde_json:: Map :: new ( ) ;
680- for ( name, action_params) in message. iter ( ) {
681- if name != action_name {
682- continue ;
683- }
686+ let keys: Vec < & String > = message. keys ( ) . collect ( ) ;
687+ if keys. len ( ) != 1 {
688+ return HttpResponse :: BadRequest ( ) . finish ( ) ;
689+ }
690+
691+ if keys[ 0 ] != action_name {
692+ return HttpResponse :: BadRequest ( ) . finish ( ) ;
693+ }
684694
685- let input = action_params. get ( "input" ) ;
695+ let action_params = message. get ( action_name) . unwrap ( ) ;
696+ let input = action_params. get ( "input" ) ;
686697
687- let action = req. state ( ) . get_action_generator ( ) . generate (
688- Arc :: downgrade ( & thing. clone ( ) ) ,
689- name . to_string ( ) ,
690- input,
691- ) ;
698+ let action = req. state ( ) . get_action_generator ( ) . generate (
699+ Arc :: downgrade ( & thing. clone ( ) ) ,
700+ action_name . to_string ( ) ,
701+ input,
702+ ) ;
692703
693- if action. is_some ( ) {
694- let action = action. unwrap ( ) ;
695- let id = action. get_id ( ) ;
696- let action = Arc :: new ( RwLock :: new ( action) ) ;
704+ if action. is_some ( ) {
705+ let action = action. unwrap ( ) ;
706+ let id = action. get_id ( ) ;
707+ let action = Arc :: new ( RwLock :: new ( action) ) ;
697708
698- {
699- let mut thing = thing. write ( ) . unwrap ( ) ;
700- let result = thing. add_action ( action. clone ( ) , input) ;
709+ {
710+ let mut thing = thing. write ( ) . unwrap ( ) ;
711+ let result = thing. add_action ( action. clone ( ) , input) ;
701712
702- if result. is_err ( ) {
703- continue ;
704- }
713+ if result. is_err ( ) {
714+ return HttpResponse :: BadRequest ( ) . finish ( ) ;
705715 }
716+ }
706717
707- response. insert (
708- name. to_string ( ) ,
709- action
710- . read ( )
711- . unwrap ( )
712- . as_action_description ( )
713- . get ( name)
714- . unwrap ( )
715- . clone ( ) ,
716- ) ;
718+ let mut response: serde_json:: Map < String , serde_json:: Value > = serde_json:: Map :: new ( ) ;
719+ response. insert (
720+ action_name. to_string ( ) ,
721+ action
722+ . read ( )
723+ . unwrap ( )
724+ . as_action_description ( )
725+ . get ( action_name)
726+ . unwrap ( )
727+ . clone ( ) ,
728+ ) ;
717729
718- thing. write ( ) . unwrap ( ) . start_action ( name. to_string ( ) , id) ;
719- }
720- }
730+ thing
731+ . write ( )
732+ . unwrap ( )
733+ . start_action ( action_name. to_string ( ) , id) ;
721734
722- HttpResponse :: Created ( ) . json ( response)
735+ HttpResponse :: Created ( ) . json ( response)
736+ } else {
737+ HttpResponse :: BadRequest ( ) . finish ( )
738+ }
723739}
724740
725741/// Handle a GET request to /actions/<action_name>/<action_id>.
0 commit comments