Skip to content

Commit ffe60a7

Browse files
committed
Converted to .Net standard
Replaced JsonConverter, separated parsing into predictable envelope and dynamic payload. Update
1 parent a9cc6c7 commit ffe60a7

21 files changed

+276
-524
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\DuoApi\DuoApi.csproj" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ static int Main(string[] args)
2323
var r = client.JSONApiCall<Dictionary<string, object>>(
2424
"GET", "/admin/v1/info/authentication_attempts", parameters);
2525
var attempts = r["authentication_attempts"] as Dictionary<string, object>;
26-
foreach (KeyValuePair<string, object> info in attempts)
27-
{
26+
foreach (KeyValuePair<string, object> info in attempts) {
2827
var s = String.Format("{0} authentication(s) ended with {1}.",
2928
info.Value,
3029
info.Key);
@@ -35,28 +34,23 @@ static int Main(string[] args)
3534
var users = client.JSONApiCall<System.Collections.ArrayList>(
3635
"GET", "/admin/v1/users", parameters);
3736
System.Console.WriteLine(String.Format("{0} users.", users.Count));
38-
foreach (Dictionary<string, object> user in users)
39-
{
37+
foreach (Dictionary<string, object> user in users) {
4038
System.Console.WriteLine(
4139
"\t" + "Username: " + (user["username"] as string));
4240
}
4341

4442
// paging call
4543
int? offset = 0;
46-
while (offset != null)
47-
{
48-
var jsonResponse = client.JSONPagingApiCall("GET", "/admin/v1/users", parameters, (int)offset, 10);
49-
var pagedUsers = jsonResponse["response"] as System.Collections.ArrayList;
44+
while (offset != null) {
45+
var pagedUsers = client.JSONPagingApiCall<System.Collections.ArrayList>("GET", "/admin/v1/users", parameters, (int)offset, 10, out var metadata);
5046
System.Console.WriteLine(String.Format("{0} users at offset {1}", pagedUsers.Count, offset));
51-
foreach (Dictionary<string, object> user in pagedUsers)
52-
{
47+
foreach (Dictionary<string, object> user in pagedUsers) {
5348
System.Console.WriteLine(
5449
"\t" + "Username: " + (user["username"] as string));
5550
}
56-
var metadata = jsonResponse["metadata"] as Dictionary<string, object>;
57-
if (metadata.ContainsKey("next_offset"))
51+
if (metadata.next_offset.HasValue)
5852
{
59-
offset = metadata["next_offset"] as int?;
53+
offset = metadata.next_offset.Value;
6054
}
6155
else
6256
{
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,10 @@ public void TestValidJsonPagingResponseNoParameters()
386386
return "{\"stat\": \"OK\", \"response\": \"hello, world!\", \"metadata\": {\"next_offset\":10}}";
387387
};
388388
var parameters = new Dictionary<string, string>();
389-
var jsonResponse = api.JSONPagingApiCall("GET", "/json_ok", parameters, 0, 10);
390-
Assert.Equal("hello, world!", jsonResponse["response"]);
391-
var metadata = jsonResponse["metadata"] as Dictionary<string, object>;
392-
Assert.Equal(10, metadata["next_offset"]);
389+
var jsonResponse = api.JSONPagingApiCall<string>("GET", "/json_ok", parameters, 0, 10, out var metadata);
390+
Assert.Equal("hello, world!", jsonResponse);
391+
392+
Assert.Equal(10, metadata.next_offset);
393393
// make sure parameters was not changed as a side-effect
394394
Assert.Empty(parameters);
395395
}
@@ -406,10 +406,10 @@ public void TestValidJsonPagingResponseExistingParameters()
406406
{"offset", "0"},
407407
{"limit", "10"}
408408
};
409-
var jsonResponse = api.JSONPagingApiCall("GET", "/json_ok", parameters, 10, 20);
410-
Assert.Equal("hello, world!", jsonResponse["response"]);
411-
var metadata = jsonResponse["metadata"] as Dictionary<string, object>;
412-
Assert.False(metadata.ContainsKey("next_offset"));
409+
var jsonResponse = api.JSONPagingApiCall<string>("GET", "/json_ok", parameters, 10, 20, out var metadata);
410+
Assert.Equal("hello, world!", jsonResponse);
411+
412+
Assert.NotNull(metadata);
413413
// make sure parameters was not changed as a side-effect
414414
Assert.Equal(2, parameters.Count);
415415
Assert.Equal("0", parameters["offset"]);
@@ -456,7 +456,7 @@ public void TestJsonResponseMissingField()
456456
});
457457

458458
Assert.NotNull(ex);
459-
var e = Assert.IsType<BadResponseException>(ex);
459+
var e = Assert.IsType<ApiException>(ex);
460460

461461
Assert.Equal(400, e.HttpStatus);
462462

DuoApi.Tests/DuoApi.Tests.csproj

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
11+
<PackageReference Include="xunit" Version="2.4.1" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
<PrivateAssets>all</PrivateAssets>
15+
</PackageReference>
16+
<PackageReference Include="coverlet.collector" Version="3.1.0">
17+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
18+
<PrivateAssets>all</PrivateAssets>
19+
</PackageReference>
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<ProjectReference Include="..\DuoApi\DuoApi.csproj" />
24+
</ItemGroup>
25+
26+
</Project>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,4 @@ public void NextOffsetTest()
106106
var expected = "foo=1&next_offset=fjaewoifjew&next_offset=473891274832917498";
107107
Assert.Equal(expected, DuoApi.CanonicalizeParams(parameters));
108108
}
109-
}
109+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ public void HmacSha512()
3131
var expected = "Basic dGVzdF9pa2V5OjA1MDgwNjUwMzVhMDNiMmExZGUyZjQ1M2U2MjllNzkxZDE4MDMyOWUxNTdmNjVkZjZiM2UwZjA4Mjk5ZDQzMjFlMWM1YzdhN2M3ZWU2YjllNWZjODBkMWZiNmZiZjNhZDVlYjdjNDRkZDNiMzk4NWEwMmMzN2FjYTUzZWMzNjk4";
3232
Assert.Equal(expected, actual);
3333
}
34-
}
34+
}

DuoApi/DataEnvelope.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace Duo
4+
{
5+
6+
/// <summary>
7+
///
8+
/// </summary>
9+
/// <typeparam name="T"></typeparam>
10+
public class DataEnvelope<T>
11+
{
12+
/// <summary>
13+
///
14+
/// </summary>
15+
[Required]
16+
public DuoApiResponseStatus stat { get; set; }
17+
18+
/// <summary>
19+
///
20+
/// </summary>
21+
public int? code { get; set; }
22+
23+
/// <summary>
24+
///
25+
/// </summary>
26+
public T response { get; set; }
27+
28+
/// <summary>
29+
/// Upon error, basic error information
30+
/// </summary>
31+
public string message { get; set; }
32+
33+
/// <summary>
34+
/// Upon error, detailed error information
35+
/// </summary>
36+
public string message_detail { get; set; }
37+
38+
/// <summary>
39+
///
40+
/// </summary>
41+
public PagingInfo metadata { get; set; }
42+
}
43+
}

0 commit comments

Comments
 (0)