Skip to content

Commit

Permalink
Converted to .Net standard
Browse files Browse the repository at this point in the history
Replaced JsonConverter, separated parsing into predictable envelope and dynamic payload. Update
  • Loading branch information
Atrejoe committed Dec 6, 2022
1 parent a9cc6c7 commit ffe60a7
Show file tree
Hide file tree
Showing 21 changed files with 276 additions and 524 deletions.
12 changes: 12 additions & 0 deletions DuoApi.Examples/DuoApi.Examples.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\DuoApi\DuoApi.csproj" />
</ItemGroup>

</Project>
20 changes: 7 additions & 13 deletions examples/Program.cs → DuoApi.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ static int Main(string[] args)
var r = client.JSONApiCall<Dictionary<string, object>>(
"GET", "/admin/v1/info/authentication_attempts", parameters);
var attempts = r["authentication_attempts"] as Dictionary<string, object>;
foreach (KeyValuePair<string, object> info in attempts)
{
foreach (KeyValuePair<string, object> info in attempts) {
var s = String.Format("{0} authentication(s) ended with {1}.",
info.Value,
info.Key);
Expand All @@ -35,28 +34,23 @@ static int Main(string[] args)
var users = client.JSONApiCall<System.Collections.ArrayList>(
"GET", "/admin/v1/users", parameters);
System.Console.WriteLine(String.Format("{0} users.", users.Count));
foreach (Dictionary<string, object> user in users)
{
foreach (Dictionary<string, object> user in users) {
System.Console.WriteLine(
"\t" + "Username: " + (user["username"] as string));
}

// paging call
int? offset = 0;
while (offset != null)
{
var jsonResponse = client.JSONPagingApiCall("GET", "/admin/v1/users", parameters, (int)offset, 10);
var pagedUsers = jsonResponse["response"] as System.Collections.ArrayList;
while (offset != null) {
var pagedUsers = client.JSONPagingApiCall<System.Collections.ArrayList>("GET", "/admin/v1/users", parameters, (int)offset, 10, out var metadata);
System.Console.WriteLine(String.Format("{0} users at offset {1}", pagedUsers.Count, offset));
foreach (Dictionary<string, object> user in pagedUsers)
{
foreach (Dictionary<string, object> user in pagedUsers) {
System.Console.WriteLine(
"\t" + "Username: " + (user["username"] as string));
}
var metadata = jsonResponse["metadata"] as Dictionary<string, object>;
if (metadata.ContainsKey("next_offset"))
if (metadata.next_offset.HasValue)
{
offset = metadata["next_offset"] as int?;
offset = metadata.next_offset.Value;
}
else
{
Expand Down
18 changes: 9 additions & 9 deletions test/ApiCallTest.cs → DuoApi.Tests/ApiCallTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,10 @@ public void TestValidJsonPagingResponseNoParameters()
return "{\"stat\": \"OK\", \"response\": \"hello, world!\", \"metadata\": {\"next_offset\":10}}";
};
var parameters = new Dictionary<string, string>();
var jsonResponse = api.JSONPagingApiCall("GET", "/json_ok", parameters, 0, 10);
Assert.Equal("hello, world!", jsonResponse["response"]);
var metadata = jsonResponse["metadata"] as Dictionary<string, object>;
Assert.Equal(10, metadata["next_offset"]);
var jsonResponse = api.JSONPagingApiCall<string>("GET", "/json_ok", parameters, 0, 10, out var metadata);
Assert.Equal("hello, world!", jsonResponse);

Assert.Equal(10, metadata.next_offset);
// make sure parameters was not changed as a side-effect
Assert.Empty(parameters);
}
Expand All @@ -406,10 +406,10 @@ public void TestValidJsonPagingResponseExistingParameters()
{"offset", "0"},
{"limit", "10"}
};
var jsonResponse = api.JSONPagingApiCall("GET", "/json_ok", parameters, 10, 20);
Assert.Equal("hello, world!", jsonResponse["response"]);
var metadata = jsonResponse["metadata"] as Dictionary<string, object>;
Assert.False(metadata.ContainsKey("next_offset"));
var jsonResponse = api.JSONPagingApiCall<string>("GET", "/json_ok", parameters, 10, 20, out var metadata);
Assert.Equal("hello, world!", jsonResponse);

Assert.NotNull(metadata);
// make sure parameters was not changed as a side-effect
Assert.Equal(2, parameters.Count);
Assert.Equal("0", parameters["offset"]);
Expand Down Expand Up @@ -456,7 +456,7 @@ public void TestJsonResponseMissingField()
});

Assert.NotNull(ex);
var e = Assert.IsType<BadResponseException>(ex);
var e = Assert.IsType<ApiException>(ex);

Assert.Equal(400, e.HttpStatus);

Expand Down
26 changes: 26 additions & 0 deletions DuoApi.Tests/DuoApi.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DuoApi\DuoApi.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@ public void NextOffsetTest()
var expected = "foo=1&next_offset=fjaewoifjew&next_offset=473891274832917498";
Assert.Equal(expected, DuoApi.CanonicalizeParams(parameters));
}
}
}
2 changes: 1 addition & 1 deletion test/SigningTest.cs → DuoApi.Tests/SigningTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ public void HmacSha512()
var expected = "Basic dGVzdF9pa2V5OjA1MDgwNjUwMzVhMDNiMmExZGUyZjQ1M2U2MjllNzkxZDE4MDMyOWUxNTdmNjVkZjZiM2UwZjA4Mjk5ZDQzMjFlMWM1YzdhN2M3ZWU2YjllNWZjODBkMWZiNmZiZjNhZDVlYjdjNDRkZDNiMzk4NWEwMmMzN2FjYTUzZWMzNjk4";
Assert.Equal(expected, actual);
}
}
}
43 changes: 43 additions & 0 deletions DuoApi/DataEnvelope.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.ComponentModel.DataAnnotations;

namespace Duo
{

/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
public class DataEnvelope<T>
{
/// <summary>
///
/// </summary>
[Required]
public DuoApiResponseStatus stat { get; set; }

/// <summary>
///
/// </summary>
public int? code { get; set; }

/// <summary>
///
/// </summary>
public T response { get; set; }

/// <summary>
/// Upon error, basic error information
/// </summary>
public string message { get; set; }

/// <summary>
/// Upon error, detailed error information
/// </summary>
public string message_detail { get; set; }

/// <summary>
///
/// </summary>
public PagingInfo metadata { get; set; }
}
}
Loading

0 comments on commit ffe60a7

Please sign in to comment.