Skip to content

Commit

Permalink
Merge pull request #19 from ahwm/httpclient-update
Browse files Browse the repository at this point in the history
Httpclient update
  • Loading branch information
ahwm authored Dec 21, 2024
2 parents 59b42df + bf0fd21 commit 92a1be7
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 385 deletions.
34 changes: 21 additions & 13 deletions Akismet.Net/Akismet.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,41 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/ahwm/Akismet.Net</PackageProjectUrl>
<RepositoryUrl>https://github.com/ahwm/Akismet.Net</RepositoryUrl>
<TargetFrameworks>net452;net6.0;netstandard2.0;net7.0</TargetFrameworks>
<TargetFrameworks>net462;netstandard2.0;</TargetFrameworks>
<Copyright>(c) 2021 Adam Humpherys</Copyright>
<PackageTags>akismet spam antispam</PackageTags>
<PackageReleaseNotes>Dropped explicit support for .NET Core 3.1 and .NET 5 and added support for .NET 7</PackageReleaseNotes>
<PackageReleaseNotes>Dropped explicit support for .NET Core; dropped dependency on RestSharp</PackageReleaseNotes>
<PackageId>AkismetApi.Net</PackageId>
<Version>3.0.0.1</Version>
<AssemblyFileVersion>3.0.0.0</AssemblyFileVersion>
<Version>4.0.0</Version>
<AssemblyFileVersion>4.0.0.0</AssemblyFileVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<DefineConstants>NETSTANDARD;NETSTANDARD2_0</DefineConstants>
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="RestSharp">
<Version>106.15.0</Version>
</PackageReference>
<PackageReference Include="Newtonsoft.Json">
<Version>13.0.3</Version>
</PackageReference>
<PackageReference Condition="'$(TargetFramework)' == 'netstandard2.0'" Include="Microsoft.CSharp">
<Version>4.7.0</Version>
</PackageReference>
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Condition="'$(TargetFramework)' == 'netstandard2.0'" Include="Microsoft.CSharp" Version="4.7.0"/>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<Reference Condition="'$(TargetFramework)' == 'net452'" Include="Microsoft.CSharp" />
<None Include="..\README.md" Pack="true" PackagePath="\"/>
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>GodaddyWrapper.Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<PropertyGroup>
<AllowedOutputExtensionsInPackageBuildOutputFolder>
Expand Down
385 changes: 108 additions & 277 deletions Akismet.Net/AkismetClient.cs

Large diffs are not rendered by default.

33 changes: 7 additions & 26 deletions Akismet.Net/AkismetComment.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Akismet.Net.Attributes;
using Akismet.Net.Helpers;
using Newtonsoft.Json;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Akismet.Net
{
Expand Down Expand Up @@ -123,14 +124,14 @@ public override string ToString()
{
var attributes = AttributeHelper.GetAttributes(this);

return JsonConvert.SerializeObject(attributes);
return JsonSerializer.Serialize(attributes);
}
}

/// <summary>
///
/// </summary>
public class CommentTypeConverter : JsonConverter
public class CommentTypeConverter : JsonConverter<AkismentCommentType>
{
/// <summary>
///
Expand All @@ -142,35 +143,15 @@ public override bool CanConvert(Type objectType)
return objectType == typeof(AkismentCommentType);
}

/// <summary>
///
/// </summary>
public override bool CanRead => false;

/// <summary>
///
/// </summary>
/// <param name="reader"></param>
/// <param name="objectType"></param>
/// <param name="existingValue"></param>
/// <param name="serializer"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
public override AkismentCommentType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}

/// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="serializer"></param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
public override void Write(Utf8JsonWriter writer, AkismentCommentType value, JsonSerializerOptions options)
{
string commentType = (AkismentCommentType)value;
writer.WriteValue(commentType);
writer.WriteStringValue(commentType);
}
}
}
3 changes: 1 addition & 2 deletions Akismet.Net/Helpers/AttributeHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Akismet.Net.Attributes;
using RestSharp.Extensions;
using System.Collections.Generic;
using System.Reflection;

Expand All @@ -25,7 +24,7 @@ public static List<KeyValuePair<string, string>> GetAttributes(object model)
}
else
{
if (property.GetAttribute<AkismetNameAttribute>() is AkismetNameAttribute attribute)
if (property.GetCustomAttribute(typeof(AkismetNameAttribute)) is AkismetNameAttribute attribute)
l.Add(new KeyValuePair<string, string>(attribute.AkismetName, property.GetValue(model).ToString()));
else
l.Add(new KeyValuePair<string, string>(property.Name, property.GetValue(model).ToString()));
Expand Down
36 changes: 36 additions & 0 deletions Akismet.Net/Services.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#if NETSTANDARD
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Reflection;

namespace Akismet.Net
{
public static class ServicesExtension
{
/// <summary>
///
/// </summary>
/// <param name="services"></param>
/// <param name="key"></param>
/// <param name="username"></param>
/// <param name="test"></param>
/// <returns></returns>
public static IServiceCollection AddAkismet(this IServiceCollection services, string key, string applicationName, string blogUrl)
{
// https://stackoverflow.com/a/79111722/1892993
services.AddOptions<AkismetClientOptions>()
.Configure(options => {
options.Key = key;
options.BlogUrl = blogUrl;
});
services.AddHttpClient<AkismetClient>(client =>
{
client.BaseAddress = new Uri($"https://{key}.rest.akismet.com/1.1/");
client.DefaultRequestHeaders.Add("User-Agent", $"{applicationName} | Akismet.NET/{Assembly.GetExecutingAssembly().GetName().Version} (https://github.com/ahwm/Akismet.Net)");
});

return services;
}
}
}
#endif
14 changes: 7 additions & 7 deletions Akismet.Net/SpamStatistics.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Newtonsoft.Json;
using System;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;

namespace Akismet.Net
{
Expand All @@ -23,13 +23,13 @@ public partial class SpamStats
/// <summary>
///
/// </summary>
[JsonProperty("missed_spam")]
[JsonPropertyName("missed_spam")]
public long MissedSpam { get; set; }

/// <summary>
///
/// </summary>
[JsonProperty("false_positives")]
[JsonPropertyName("false_positives")]
public long FalsePositives { get; set; }

/// <summary>
Expand All @@ -45,7 +45,7 @@ public partial class SpamStats
/// <summary>
///
/// </summary>
[JsonProperty("time_saved")]
[JsonPropertyName("time_saved")]
public long TimeSaved { get; set; }
}

Expand All @@ -67,13 +67,13 @@ public partial class Breakdown
/// <summary>
///
/// </summary>
[JsonProperty("missed_spam")]
[JsonPropertyName("missed_spam")]
public long MissedSpam { get; set; }

/// <summary>
///
/// </summary>
[JsonProperty("false_positives")]
[JsonPropertyName("false_positives")]
public long FalsePositives { get; set; }

/// <summary>
Expand Down
8 changes: 7 additions & 1 deletion Akismet.Tests/Akismet.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0;net6.0;net462</TargetFrameworks>
<TargetFrameworks>net9.0;net8.0;net6.0;net462</TargetFrameworks>

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

<PropertyGroup Condition=" '$(TargetFramework)' != 'net462'">
<DefineConstants>NETCORE</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Shouldly" Version="4.2.1" />
Expand All @@ -19,6 +23,8 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<ProjectReference Include="../Akismet.Net/Akismet.Net.csproj" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
<PackageReference Include="Xunit.DependencyInjection" Version="9.7.0" />
</ItemGroup>

</Project>
66 changes: 8 additions & 58 deletions Akismet.Tests/AkismetTests.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
using Akismet.Net;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Akismet.Tests
{
public class AkismetTests
{
private readonly string ApiKey;
private readonly string ApiKeyUrl;

Check warning on line 11 in Akismet.Tests/AkismetTests.cs

View workflow job for this annotation

GitHub Actions / build

Field 'AkismetTests.ApiKeyUrl' is never assigned to, and will always have its default value null

Check warning on line 11 in Akismet.Tests/AkismetTests.cs

View workflow job for this annotation

GitHub Actions / build

Field 'AkismetTests.ApiKeyUrl' is never assigned to, and will always have its default value null

Check warning on line 11 in Akismet.Tests/AkismetTests.cs

View workflow job for this annotation

GitHub Actions / build

Field 'AkismetTests.ApiKeyUrl' is never assigned to, and will always have its default value null

Check warning on line 11 in Akismet.Tests/AkismetTests.cs

View workflow job for this annotation

GitHub Actions / build

Field 'AkismetTests.ApiKeyUrl' is never assigned to, and will always have its default value null

Check warning on line 11 in Akismet.Tests/AkismetTests.cs

View workflow job for this annotation

GitHub Actions / build

Field 'AkismetTests.ApiKeyUrl' is never assigned to, and will always have its default value null

Check warning on line 11 in Akismet.Tests/AkismetTests.cs

View workflow job for this annotation

GitHub Actions / build

Field 'AkismetTests.ApiKeyUrl' is never assigned to, and will always have its default value null
private readonly AkismetClient Client;
#if NETCORE
public AkismetTests(AkismetClient akismetClient)
{
Client = akismetClient;
}
#else
private readonly string ApiKey;

public AkismetTests()
{
Expand All @@ -23,12 +25,7 @@ public AkismetTests()

Client = new AkismetClient(ApiKey, new Uri(ApiKeyUrl), "Akismet Test Application");
}

[Fact]
public void VerifyKeyTest()
{
Client.VerifyKey().ShouldBe(true);
}
#endif

[Fact]
public async Task VerifyKeyAsyncTest()
Expand All @@ -38,29 +35,6 @@ public async Task VerifyKeyAsyncTest()
isValid.ShouldBe(true);
}

[Fact]
public void CheckSpamComment()
{
AkismetComment comment = new AkismetComment
{
CommentAuthor = "viagra-test-123",
CommentAuthorEmail = "[email protected]",
CommentAuthorUrl = "http://www.spamwebsite.com",
Referrer = "https://www.google.com",
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36",
CommentContent = "This is a test spam comment",
CommentType = AkismentCommentType.ContactForm, // multiple defined values, or use new AkismetCommentType("new-comment-type") for a custom option
Permalink = $"{ApiKeyUrl}/contact",
IsTest = "true",
BlogCharset = "UTF-8",
BlogLanguage = "en-US",
CommentDate = DateTime.UtcNow.ToString("s"), // ISO-8601 format
CommentPostModified = DateTime.UtcNow.ToString("s") // ISO-8601 format
};
var spamResult = Client.Check(comment);
spamResult.SpamStatus.ShouldBe(SpamStatus.Spam);
}

[Fact]
public async Task CheckSpamCommentAsync()
{
Expand All @@ -84,30 +58,6 @@ public async Task CheckSpamCommentAsync()
spamResult.SpamStatus.ShouldBe(SpamStatus.Spam);
}

[Fact]
public void CheckHamComment()
{
AkismetComment comment = new AkismetComment
{
CommentAuthor = "Test",
CommentAuthorEmail = "[email protected]",
CommentAuthorUrl = "http://www.spamwebsite.com",
Referrer = "https://www.google.com",
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36",
CommentContent = "This is a test ham comment",
CommentType = AkismentCommentType.ContactForm,
Permalink = $"{ApiKeyUrl}/contact",
IsTest = "true",
BlogCharset = "UTF-8",
BlogLanguage = "en-US",
CommentDate = DateTime.UtcNow.ToString("s"),
CommentPostModified = DateTime.UtcNow.ToString("s"),
UserRole = "administrator"
};
var spamResult = Client.Check(comment);
spamResult.SpamStatus.ShouldBe(SpamStatus.Ham);
}

[Fact]
public async Task CheckHamCommentAsync()
{
Expand Down
18 changes: 18 additions & 0 deletions Akismet.Tests/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#if NETCORE
using Akismet.Net;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace Akismet.Tests
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
string apiKey = Environment.GetEnvironmentVariable("AKISMET_API_KEY").Trim();
string blogUrl = Environment.GetEnvironmentVariable("AKISMET_API_KEY_URL").Trim();
services.AddAkismet(apiKey, "Akismet Test Application", blogUrl);
}
}
}
#endif
Loading

0 comments on commit 92a1be7

Please sign in to comment.