1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Globalization ;
3
4
using System . Linq ;
4
5
using System . Net ;
5
6
using System . Net . Http ;
@@ -11,10 +12,23 @@ namespace HamnetDbAbstraction
11
12
/// <summary>
12
13
/// Implementation of <see cref="IHamnetDbAccess" /> retrieving data via REST / JSON interface of HamnetDB.
13
14
/// </summary>
14
- internal partial class JsonHamnetDbAccessor : IHamnetDbAccess
15
+ internal class JsonHamnetDbAccessor : IHamnetDbAccess
15
16
{
16
17
private static readonly log4net . ILog log = HamnetDbAbstraction . GetLogger ( System . Reflection . MethodBase . GetCurrentMethod ( ) . DeclaringType ) ;
17
18
19
+ /// <summary>
20
+ /// Culture used by HamnetDB.
21
+ /// </summary>
22
+ private static CultureInfo HamnetDbJsonCultureInfo = CultureInfo . CreateSpecificCulture ( "de-DE" ) ;
23
+
24
+ /// <summary>
25
+ /// HamnetDB is using German formattings.
26
+ /// </summary>
27
+ private static readonly JsonSerializerSettings SerializerSettings = new JsonSerializerSettings
28
+ {
29
+ Converters = { new HamnetDbDoubleConverter ( ) }
30
+ } ;
31
+
18
32
/// <summary>
19
33
/// To detect redundant calls.
20
34
/// </summary>
@@ -30,8 +44,9 @@ internal partial class JsonHamnetDbAccessor : IHamnetDbAccess
30
44
/// </summary>
31
45
/// <param name="hostsApiUrl">The URL to access the hosts of HamnetDB.</param>
32
46
/// <param name="subnetsApiUrl">The URL to access the subnets of HamnetDB.</param>
33
- /// /// <param name="additionalDisposer">An additional Disposer that will be called (if not null) when this obejects gets disposed off.</param>
34
- public JsonHamnetDbAccessor ( string hostsApiUrl , string subnetsApiUrl , IDisposable additionalDisposer )
47
+ /// <param name="sitesApiUrl">The URL to access the sites of HamnetDB.</param>
48
+ /// <param name="additionalDisposer">An additional Disposer that will be called (if not null) when this obejects gets disposed off.</param>
49
+ public JsonHamnetDbAccessor ( string hostsApiUrl , string subnetsApiUrl , string sitesApiUrl , IDisposable additionalDisposer )
35
50
{
36
51
if ( string . IsNullOrWhiteSpace ( hostsApiUrl ) )
37
52
{
@@ -43,8 +58,14 @@ public JsonHamnetDbAccessor(string hostsApiUrl, string subnetsApiUrl, IDisposabl
43
58
throw new ArgumentNullException ( nameof ( subnetsApiUrl ) , "The subnets API URL is null, empty or white-space-only" ) ;
44
59
}
45
60
61
+ if ( string . IsNullOrWhiteSpace ( sitesApiUrl ) )
62
+ {
63
+ throw new ArgumentNullException ( nameof ( sitesApiUrl ) , "The sitesApiUrl API URL is null, empty or white-space-only" ) ;
64
+ }
65
+
46
66
this . HostApiUrl = hostsApiUrl ;
47
67
this . SubnetsApiUrl = subnetsApiUrl ;
68
+ this . SitesApiUrl = sitesApiUrl ;
48
69
this . additionalDisposer = additionalDisposer ;
49
70
}
50
71
@@ -65,6 +86,11 @@ public JsonHamnetDbAccessor(string hostsApiUrl, string subnetsApiUrl, IDisposabl
65
86
/// </summary>
66
87
public string SubnetsApiUrl { get ; }
67
88
89
+ /// <summary>
90
+ /// Gets the URL to access the sites of HamnetDB.
91
+ /// </summary>
92
+ public string SitesApiUrl { get ; }
93
+
68
94
/// <inheritdoc />
69
95
public IHamnetDbHosts QueryBgpRouters ( )
70
96
{
@@ -131,6 +157,16 @@ public IHamnetDbSubnets QuerySubnets()
131
157
return new HamnetDbSubnets ( hosts ) ;
132
158
}
133
159
160
+ /// <inheritdoc />
161
+ public IHamnetDbSites QuerySites ( )
162
+ {
163
+ var responseString = this . SendHttpRequest ( new Uri ( this . SitesApiUrl , UriKind . Absolute ) ) ;
164
+
165
+ var responseData = JsonConvert . DeserializeObject < IEnumerable < JsonSiteDataSet > > ( responseString , SerializerSettings ) ;
166
+
167
+ return new HamnetDbSites ( responseData ) ;
168
+ }
169
+
134
170
/// <summary>
135
171
/// Correctly implement the disposable pattern.
136
172
/// </summary>
@@ -189,5 +225,30 @@ private string SendHttpRequest(Uri uri)
189
225
}
190
226
}
191
227
}
228
+
229
+ /// <summary>
230
+ /// JsonConverter for reading the double values with German formatting as used by HamnetDB REST API.
231
+ /// </summary>
232
+ private class HamnetDbDoubleConverter : JsonConverter < double >
233
+ {
234
+ /// <inheritdoc />
235
+ public override double ReadJson ( JsonReader reader , Type objectType , double existingValue , bool hasExistingValue , JsonSerializer serializer )
236
+ {
237
+ string s = ( string ) reader . Value ? . ToString ( ) ;
238
+
239
+ if ( ! double . TryParse ( s , NumberStyles . Any , HamnetDbJsonCultureInfo , out double parsedDouble ) )
240
+ {
241
+ return double . NaN ;
242
+ }
243
+
244
+ return parsedDouble ;
245
+ }
246
+
247
+ /// <inheritdoc />
248
+ public override void WriteJson ( JsonWriter writer , double value , JsonSerializer serializer )
249
+ {
250
+ writer . WriteValue ( value . ToString ( HamnetDbJsonCultureInfo ) ) ;
251
+ }
252
+ }
192
253
}
193
254
}
0 commit comments