1
+ // Copyright © 2024-Present The Synapse Authors
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License"),
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ // http://www.apache.org/licenses/LICENSE-2.0
7
+ //
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
13
+
14
+ using Microsoft . Extensions . Logging ;
15
+ using Microsoft . Extensions . Options ;
16
+ using Neuroglia . Eventing . CloudEvents ;
17
+ using Neuroglia . Serialization ;
18
+ using Synapse . Api . Application . Configuration ;
19
+ using System . Text ;
20
+
21
+ namespace Synapse . Api . Application . Commands . Events ;
22
+
23
+ /// <summary>
24
+ /// Represents the <see cref="ICommand"/> used to publish a <see cref="Neuroglia.Eventing.CloudEvents.CloudEvent"/> to the configured sink
25
+ /// </summary>
26
+ /// <param name="e">The <see cref="Neuroglia.Eventing.CloudEvents.CloudEvent"/> to publish</param>
27
+ public class PublishCloudEventCommand ( CloudEvent e )
28
+ : Command
29
+ {
30
+
31
+ /// <summary>
32
+ /// Gets the <see cref="Neuroglia.Eventing.CloudEvents.CloudEvent"/> to publish
33
+ /// </summary>
34
+ public virtual CloudEvent CloudEvent { get ; } = e ;
35
+
36
+ }
37
+
38
+ /// <summary>
39
+ /// Represents the service used to handle <see cref="PublishCloudEventCommand"/>s
40
+ /// </summary>
41
+ /// <param name="logger">The service used to perform logging</param>
42
+ /// <param name="options">The service used to access the current <see cref="ApiServerOptions"/></param>
43
+ /// <param name="jsonSerializer">The service used to serialize/deserialize data to/from JSON</param>
44
+ /// <param name="httpClient">The service used to perform HTTP requests</param>
45
+ public class PublishCloudEventCommandHandler ( ILogger < PublishCloudEventCommandHandler > logger , IOptions < ApiServerOptions > options , IJsonSerializer jsonSerializer , HttpClient httpClient )
46
+ : ICommandHandler < PublishCloudEventCommand >
47
+ {
48
+
49
+ /// <inheritdoc/>
50
+ public virtual async Task < IOperationResult > HandleAsync ( PublishCloudEventCommand command , CancellationToken cancellationToken = default )
51
+ {
52
+ if ( options . Value . Events ? . Endpoint == null ) return this . Ok ( ) ;
53
+ var json = jsonSerializer . SerializeToText ( command . CloudEvent ) ;
54
+ using var content = new StringContent ( json , Encoding . UTF8 , CloudEventContentType . Json ) ;
55
+ using var request = new HttpRequestMessage ( HttpMethod . Post , options . Value . Events . Endpoint ) { Content = content } ;
56
+ using var response = await httpClient . SendAsync ( request , cancellationToken ) . ConfigureAwait ( false ) ;
57
+ json = await response . Content . ReadAsStringAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
58
+ if ( ! response . IsSuccessStatusCode )
59
+ {
60
+ logger . LogError ( "An error occurred while publishing the cloud event with id '{eventId}' to the configure endpoint '{endpoint}': {ex}" , command . CloudEvent . Id , options . Value . Events . Endpoint , json ) ;
61
+ response . EnsureSuccessStatusCode ( ) ;
62
+ }
63
+ return this . Ok ( ) ;
64
+ }
65
+
66
+ }
0 commit comments