@@ -12,20 +12,20 @@ use crate::{
12
12
permissions,
13
13
types:: {
14
14
metadata:: Metadata , BaseId , EndpointId , EndpointSecretInternal , EndpointUid ,
15
- EventChannelSet , EventTypeNameSet , MessageEndpointId , MessageStatus ,
15
+ EventChannelSet , EventTypeName , EventTypeNameSet , MessageEndpointId , MessageStatus ,
16
16
} ,
17
17
} ,
18
18
ctx,
19
- db:: models:: messagedestination,
19
+ db:: models:: { eventtype , messagedestination} ,
20
20
error:: { self , HttpError } ,
21
21
v1:: utils:: {
22
- api_not_implemented , openapi_desc , openapi_tag,
22
+ openapi_tag,
23
23
patch:: {
24
24
patch_field_non_nullable, patch_field_nullable, UnrequiredField ,
25
25
UnrequiredNullableField ,
26
26
} ,
27
27
validate_no_control_characters, validate_no_control_characters_unrequired,
28
- validation_error, ApplicationEndpointPath , ModelIn ,
28
+ validation_error, ApplicationEndpointPath , ModelIn , ValidatedJson ,
29
29
} ,
30
30
AppState ,
31
31
} ;
@@ -53,6 +53,8 @@ use crate::db::models::endpoint;
53
53
54
54
use self :: secrets:: generate_secret;
55
55
56
+ use super :: message:: { create_message_inner, MessageIn , MessageOut , RawPayload } ;
57
+
56
58
pub fn validate_event_types_ids (
57
59
event_types_ids : & EventTypeNameSet ,
58
60
) -> std:: result:: Result < ( ) , ValidationError > {
@@ -730,7 +732,81 @@ async fn endpoint_stats(
730
732
} ) )
731
733
}
732
734
733
- const SEND_EXAMPLE_DESCRIPTION : & str = "Send an example message for event" ;
735
+ #[ derive( Deserialize , JsonSchema , Validate ) ]
736
+ #[ serde( rename_all = "camelCase" ) ]
737
+ struct EventExampleIn {
738
+ event_type : EventTypeName ,
739
+ }
740
+
741
+ const SVIX_PING_EVENT_TYPE_NAME : & str = "svix.ping" ;
742
+ const SVIX_PING_EVENT_TYPE_PAYLOAD : & str = r#"{"success": true}"# ;
743
+
744
+ /// Send an example message for an event
745
+ #[ aide_annotate(
746
+ op_id = "v1.endpoint.send-example" ,
747
+ op_summary = "Send Event Type Example Message"
748
+ ) ]
749
+ async fn send_example (
750
+ state : State < AppState > ,
751
+ Path ( ApplicationEndpointPath { endpoint_id, .. } ) : Path < ApplicationEndpointPath > ,
752
+ permissions:: OrganizationWithApplication { app } : permissions:: OrganizationWithApplication ,
753
+ ValidatedJson ( data) : ValidatedJson < EventExampleIn > ,
754
+ ) -> error:: Result < Json < MessageOut > > {
755
+ let State ( AppState {
756
+ ref db,
757
+ queue_tx,
758
+ cache,
759
+ ..
760
+ } ) = state;
761
+
762
+ let endpoint = ctx ! (
763
+ endpoint:: Entity :: secure_find_by_id_or_uid( app. id. clone( ) , endpoint_id)
764
+ . one( db)
765
+ . await
766
+ ) ?
767
+ . ok_or_else ( || HttpError :: not_found ( None , None ) ) ?;
768
+
769
+ let example = if data. event_type == EventTypeName ( SVIX_PING_EVENT_TYPE_NAME . to_owned ( ) ) {
770
+ SVIX_PING_EVENT_TYPE_PAYLOAD . to_string ( )
771
+ } else {
772
+ let event_type = ctx ! (
773
+ eventtype:: Entity :: secure_find_by_name( app. org_id. clone( ) , data. event_type. clone( ) )
774
+ . one( db)
775
+ . await
776
+ ) ?
777
+ . ok_or_else ( || HttpError :: not_found ( None , None ) ) ?;
778
+
779
+ let example = event_type. schemas . and_then ( |schema| {
780
+ schema
781
+ . example ( )
782
+ . and_then ( |ex| serde_json:: to_string ( ex) . ok ( ) )
783
+ } ) ;
784
+
785
+ match example {
786
+ Some ( example) => example,
787
+ None => {
788
+ return Err ( HttpError :: bad_request (
789
+ Some ( "invalid_scheme" . to_owned ( ) ) ,
790
+ Some ( "Unable to generate example message from event-type schema" . to_owned ( ) ) ,
791
+ )
792
+ . into ( ) ) ;
793
+ }
794
+ }
795
+ } ;
796
+
797
+ let msg_in = MessageIn {
798
+ channels : None ,
799
+ event_type : data. event_type ,
800
+ payload : RawPayload :: from_string ( example) . unwrap ( ) ,
801
+ uid : None ,
802
+ payload_retention_period : 90 ,
803
+ } ;
804
+
805
+ let create_message =
806
+ create_message_inner ( db, queue_tx, cache, false , Some ( endpoint. id ) , msg_in, app) . await ?;
807
+
808
+ Ok ( Json ( create_message) )
809
+ }
734
810
735
811
pub fn router ( ) -> ApiRouter < AppState > {
736
812
let tag = openapi_tag ( "Endpoint" ) ;
@@ -772,7 +848,7 @@ pub fn router() -> ApiRouter<AppState> {
772
848
)
773
849
. api_route_with (
774
850
"/app/:app_id/endpoint/:endpoint_id/send-example/" ,
775
- post_with ( api_not_implemented , openapi_desc ( SEND_EXAMPLE_DESCRIPTION ) ) ,
851
+ post_with ( send_example , send_example_operation ) ,
776
852
& tag,
777
853
)
778
854
. api_route_with (
0 commit comments