77using System . Threading . Tasks ;
88using System . Xml . Serialization ;
99using System . Text . Json ;
10+ using System . Globalization ;
11+ using TE . FileVerification . Net ;
1012
11- namespace TE . FileVerification . Configuration . Notifications
13+ namespace TE . FileVerification . Configuration
1214{
1315 public class Notification
1416 {
1517 // The message to send with the request.
16- private StringBuilder _message ;
18+ private readonly StringBuilder _message ;
1719
1820 /// <summary>
1921 /// Gets or sets the URL of the request.
2022 /// </summary>
2123 [ XmlElement ( "url" ) ]
22- public string Url { get ; set ; }
24+ public string ? Url { get ; set ; }
2325
2426 /// <summary>
2527 /// Gets the URI value of the string URL.
2628 /// </summary>
2729 [ XmlIgnore ]
28- public Uri Uri
30+ public Uri ? Uri
2931 {
3032 get
3133 {
@@ -46,12 +48,12 @@ public Uri Uri
4648 }
4749 }
4850 }
49-
51+
5052 /// <summary>
5153 /// Gets or sets the string representation of the request method.
5254 /// </summary>
5355 [ XmlElement ( "method" ) ]
54- public string MethodString { get ; set ; }
56+ public string MethodString { get ; set ; } = "Post" ;
5557
5658 /// <summary>
5759 /// Gets the request method.
@@ -69,7 +71,7 @@ public HttpMethod Method
6971
7072 try
7173 {
72- method = ( HttpMethod ) Enum . Parse ( typeof ( HttpMethod ) , MethodString . ToUpper ( ) , true ) ;
74+ method = ( HttpMethod ) Enum . Parse ( typeof ( HttpMethod ) , MethodString . ToUpper ( CultureInfo . CurrentCulture ) , true ) ;
7375 }
7476 catch ( Exception ex )
7577 when ( ex is ArgumentNullException || ex is ArgumentException || ex is OverflowException )
@@ -85,7 +87,7 @@ public HttpMethod Method
8587 /// Gets or sets the data to send for the request.
8688 /// </summary>
8789 [ XmlElement ( "data" ) ]
88- public Data Data { get ; set ; }
90+ public Data ? Data { get ; set ; }
8991
9092 /// <summary>
9193 /// Returns a value indicating if there is a message waiting to be sent
@@ -121,36 +123,44 @@ public Notification()
121123 /// </param>
122124 internal void QueueRequest ( string message )
123125 {
124- //_message.Append(CleanMessage(message) + @"\n");
125- _message . Append ( message ) ;
126+ _message . Append ( CleanMessage ( message ) + @"\n" ) ;
126127 }
127128
128129 /// <summary>
129130 /// Send the notification request.
130131 /// </summary>
131- /// <exception cref="NullReferenceException ">
132+ /// <exception cref="InvalidOperationException ">
132133 /// Thrown when the URL is null or empty.
133134 /// </exception>
134- internal HttpResponseMessage Send ( )
135+ internal Response ? Send ( )
135136 {
136137 // If there isn't a message to be sent, then just return
137- if ( _message ? . Length <= 0 )
138+ if ( _message == null || _message . Length <= 0 )
138139 {
139140 return null ;
140141 }
141142
142- if ( Uri == null )
143+ if ( GetUri ( ) == null )
144+ {
145+ throw new InvalidOperationException ( "The URL is null or empty." ) ;
146+ }
147+
148+ if ( Data == null )
143149 {
144- throw new NullReferenceException ( "The URL is null or empty .") ;
150+ throw new InvalidOperationException ( "Data for the request was not provided .") ;
145151 }
146152
147- string content = Data . Body . Replace ( "[message]" , cleanForJSON ( _message . ToString ( ) ) ) ;
153+ string content = string . Empty ;
154+ if ( Data . Body != null )
155+ {
156+ content = Data . Body . Replace ( "[message]" , _message . ToString ( ) , StringComparison . OrdinalIgnoreCase ) ;
157+ }
148158
149- HttpResponseMessage response =
159+ Response response =
150160 Request . Send (
151161 Method ,
152- Uri ,
153- Data . Headers . HeaderList ,
162+ GetUri ( ) ,
163+ Data . Headers ,
154164 content ,
155165 Data . MimeType ) ;
156166
@@ -161,37 +171,46 @@ internal HttpResponseMessage Send()
161171 /// <summary>
162172 /// Send the notification request.
163173 /// </summary>
164- /// <exception cref="NullReferenceException ">
174+ /// <exception cref="InvalidOperationException ">
165175 /// Thrown when the URL is null or empty.
166176 /// </exception>
167- internal async Task < HttpResponseMessage > SendAsync ( )
177+ internal async Task < Response ? > SendAsync ( )
168178 {
169179 // If there isn't a message to be sent, then just return
170- if ( _message ? . Length <= 0 )
180+ if ( _message == null || _message . Length <= 0 )
171181 {
172182 return null ;
173183 }
174184
175- if ( Uri == null )
185+ if ( GetUri ( ) == null )
186+ {
187+ throw new InvalidOperationException ( "The URL is null or empty." ) ;
188+ }
189+
190+ if ( Data == null )
191+ {
192+ throw new InvalidOperationException ( "Data for the request was not provided." ) ;
193+ }
194+
195+ string content = string . Empty ;
196+ if ( Data . Body != null )
176197 {
177- throw new NullReferenceException ( "The URL is null or empty." ) ;
198+ content = Data . Body . Replace ( "[message]" , _message . ToString ( ) , StringComparison . OrdinalIgnoreCase ) ;
178199 }
179-
180- string content = Data . Body . Replace ( "[message]" , _message . ToString ( ) ) ;
181200
182- HttpResponseMessage response =
201+ Response response =
183202 await Request . SendAsync (
184203 Method ,
185- Uri ,
186- Data . Headers . HeaderList ,
204+ GetUri ( ) ,
205+ Data . Headers ,
187206 content ,
188- Data . MimeType ) ;
207+ Data . MimeType ) . ConfigureAwait ( false ) ;
189208
190209 _message . Clear ( ) ;
191- return response ;
210+ return response ;
192211 }
193212
194- public static string cleanForJSON ( string s )
213+ public static string CleanMessage ( string s )
195214 {
196215 if ( s == null || s . Length == 0 )
197216 {
@@ -201,8 +220,8 @@ public static string cleanForJSON(string s)
201220 char c = '\0 ' ;
202221 int i ;
203222 int len = s . Length ;
204- StringBuilder sb = new StringBuilder ( len + 4 ) ;
205- String t ;
223+ StringBuilder sb = new ( len + 4 ) ;
224+ string t ;
206225
207226 for ( i = 0 ; i < len ; i += 1 )
208227 {
@@ -236,8 +255,8 @@ public static string cleanForJSON(string s)
236255 default :
237256 if ( c < ' ' )
238257 {
239- t = "000" + String . Format ( "X ", c ) ;
240- sb . Append ( "\\ u" + t . Substring ( t . Length - 4 ) ) ;
258+ t = "000" + string . Format ( CultureInfo . CurrentCulture , "{0:X} ", c ) ;
259+ sb . Append ( string . Concat ( "\\ u" , t . AsSpan ( t . Length - 4 ) ) ) ;
241260 }
242261 else
243262 {
@@ -248,5 +267,22 @@ public static string cleanForJSON(string s)
248267 }
249268 return sb . ToString ( ) ;
250269 }
270+
271+ /// <summary>
272+ /// Gets the URI value of the string URL.
273+ /// </summary>
274+ /// <exception cref="UriFormatException">
275+ /// Thrown if the URL is not in a valid format.
276+ /// </exception>
277+ private Uri GetUri ( )
278+ {
279+ if ( string . IsNullOrWhiteSpace ( Url ) )
280+ {
281+ throw new UriFormatException ( ) ;
282+ }
283+
284+ Uri uri = new ( Url ) ;
285+ return uri ;
286+ }
251287 }
252288}
0 commit comments