7
7
using System . Threading . Tasks ;
8
8
using System . Xml . Serialization ;
9
9
using System . Text . Json ;
10
+ using System . Globalization ;
11
+ using TE . FileVerification . Net ;
10
12
11
- namespace TE . FileVerification . Configuration . Notifications
13
+ namespace TE . FileVerification . Configuration
12
14
{
13
15
public class Notification
14
16
{
15
17
// The message to send with the request.
16
- private StringBuilder _message ;
18
+ private readonly StringBuilder _message ;
17
19
18
20
/// <summary>
19
21
/// Gets or sets the URL of the request.
20
22
/// </summary>
21
23
[ XmlElement ( "url" ) ]
22
- public string Url { get ; set ; }
24
+ public string ? Url { get ; set ; }
23
25
24
26
/// <summary>
25
27
/// Gets the URI value of the string URL.
26
28
/// </summary>
27
29
[ XmlIgnore ]
28
- public Uri Uri
30
+ public Uri ? Uri
29
31
{
30
32
get
31
33
{
@@ -46,12 +48,12 @@ public Uri Uri
46
48
}
47
49
}
48
50
}
49
-
51
+
50
52
/// <summary>
51
53
/// Gets or sets the string representation of the request method.
52
54
/// </summary>
53
55
[ XmlElement ( "method" ) ]
54
- public string MethodString { get ; set ; }
56
+ public string MethodString { get ; set ; } = "Post" ;
55
57
56
58
/// <summary>
57
59
/// Gets the request method.
@@ -69,7 +71,7 @@ public HttpMethod Method
69
71
70
72
try
71
73
{
72
- method = ( HttpMethod ) Enum . Parse ( typeof ( HttpMethod ) , MethodString . ToUpper ( ) , true ) ;
74
+ method = ( HttpMethod ) Enum . Parse ( typeof ( HttpMethod ) , MethodString . ToUpper ( CultureInfo . CurrentCulture ) , true ) ;
73
75
}
74
76
catch ( Exception ex )
75
77
when ( ex is ArgumentNullException || ex is ArgumentException || ex is OverflowException )
@@ -85,7 +87,7 @@ public HttpMethod Method
85
87
/// Gets or sets the data to send for the request.
86
88
/// </summary>
87
89
[ XmlElement ( "data" ) ]
88
- public Data Data { get ; set ; }
90
+ public Data ? Data { get ; set ; }
89
91
90
92
/// <summary>
91
93
/// Returns a value indicating if there is a message waiting to be sent
@@ -121,36 +123,44 @@ public Notification()
121
123
/// </param>
122
124
internal void QueueRequest ( string message )
123
125
{
124
- //_message.Append(CleanMessage(message) + @"\n");
125
- _message . Append ( message ) ;
126
+ _message . Append ( CleanMessage ( message ) + @"\n" ) ;
126
127
}
127
128
128
129
/// <summary>
129
130
/// Send the notification request.
130
131
/// </summary>
131
- /// <exception cref="NullReferenceException ">
132
+ /// <exception cref="InvalidOperationException ">
132
133
/// Thrown when the URL is null or empty.
133
134
/// </exception>
134
- internal HttpResponseMessage Send ( )
135
+ internal Response ? Send ( )
135
136
{
136
137
// If there isn't a message to be sent, then just return
137
- if ( _message ? . Length <= 0 )
138
+ if ( _message == null || _message . Length <= 0 )
138
139
{
139
140
return null ;
140
141
}
141
142
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 )
143
149
{
144
- throw new NullReferenceException ( "The URL is null or empty .") ;
150
+ throw new InvalidOperationException ( "Data for the request was not provided .") ;
145
151
}
146
152
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
+ }
148
158
149
- HttpResponseMessage response =
159
+ Response response =
150
160
Request . Send (
151
161
Method ,
152
- Uri ,
153
- Data . Headers . HeaderList ,
162
+ GetUri ( ) ,
163
+ Data . Headers ,
154
164
content ,
155
165
Data . MimeType ) ;
156
166
@@ -161,37 +171,46 @@ internal HttpResponseMessage Send()
161
171
/// <summary>
162
172
/// Send the notification request.
163
173
/// </summary>
164
- /// <exception cref="NullReferenceException ">
174
+ /// <exception cref="InvalidOperationException ">
165
175
/// Thrown when the URL is null or empty.
166
176
/// </exception>
167
- internal async Task < HttpResponseMessage > SendAsync ( )
177
+ internal async Task < Response ? > SendAsync ( )
168
178
{
169
179
// If there isn't a message to be sent, then just return
170
- if ( _message ? . Length <= 0 )
180
+ if ( _message == null || _message . Length <= 0 )
171
181
{
172
182
return null ;
173
183
}
174
184
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 )
176
197
{
177
- throw new NullReferenceException ( "The URL is null or empty." ) ;
198
+ content = Data . Body . Replace ( "[message]" , _message . ToString ( ) , StringComparison . OrdinalIgnoreCase ) ;
178
199
}
179
-
180
- string content = Data . Body . Replace ( "[message]" , _message . ToString ( ) ) ;
181
200
182
- HttpResponseMessage response =
201
+ Response response =
183
202
await Request . SendAsync (
184
203
Method ,
185
- Uri ,
186
- Data . Headers . HeaderList ,
204
+ GetUri ( ) ,
205
+ Data . Headers ,
187
206
content ,
188
- Data . MimeType ) ;
207
+ Data . MimeType ) . ConfigureAwait ( false ) ;
189
208
190
209
_message . Clear ( ) ;
191
- return response ;
210
+ return response ;
192
211
}
193
212
194
- public static string cleanForJSON ( string s )
213
+ public static string CleanMessage ( string s )
195
214
{
196
215
if ( s == null || s . Length == 0 )
197
216
{
@@ -201,8 +220,8 @@ public static string cleanForJSON(string s)
201
220
char c = '\0 ' ;
202
221
int i ;
203
222
int len = s . Length ;
204
- StringBuilder sb = new StringBuilder ( len + 4 ) ;
205
- String t ;
223
+ StringBuilder sb = new ( len + 4 ) ;
224
+ string t ;
206
225
207
226
for ( i = 0 ; i < len ; i += 1 )
208
227
{
@@ -236,8 +255,8 @@ public static string cleanForJSON(string s)
236
255
default :
237
256
if ( c < ' ' )
238
257
{
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 ) ) ) ;
241
260
}
242
261
else
243
262
{
@@ -248,5 +267,22 @@ public static string cleanForJSON(string s)
248
267
}
249
268
return sb . ToString ( ) ;
250
269
}
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
+ }
251
287
}
252
288
}
0 commit comments