|
1 | 1 | namespace Belin.Akismet;
|
2 | 2 |
|
| 3 | +using System.Net.Http.Headers; |
| 4 | + |
3 | 5 | /// <summary>
|
4 | 6 | /// Submits comments to the Akismet service.
|
5 | 7 | /// </summary>
|
@@ -41,5 +43,85 @@ public class Client(string apiKey, Blog blog, string baseUrl = "https://rest.aki
|
41 | 43 | /// <summary>
|
42 | 44 | /// The user agent string to use when making requests.
|
43 | 45 | /// </summary>
|
44 |
| - public string UserAgent { get; init; } = $".NET/{Environment.Version} | Akismet/{Version}"; |
| 46 | + public string UserAgent { get; init; } = $".NET/{Environment.Version.ToString(3)} | Akismet/{Version}"; |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Checks the specified comment against the service database, and returns a value indicating whether it is spam. |
| 50 | + /// </summary> |
| 51 | + /// <param name="comment">The comment to be submitted.</param> |
| 52 | + /// <param name="cancellationToken">The token to cancel the operation.</param> |
| 53 | + /// <returns>A value indicating whether the specified comment is spam.</returns> |
| 54 | + /// <exception cref="HttpRequestException">The remote server returned an invalid response.</exception> |
| 55 | + public async Task<CheckResult> CheckComment(Comment comment, CancellationToken cancellationToken = default) { |
| 56 | + using var response = await Fetch("comment-check", comment.ToJson(), cancellationToken); |
| 57 | + if (await response.Content.ReadAsStringAsync(cancellationToken) == "false") return CheckResult.Ham; |
| 58 | + if (!response.Headers.TryGetValues("X-akismet-pro-tip", out var proTips)) return CheckResult.Spam; |
| 59 | + return proTips.First() == "discard" ? CheckResult.PervasiveSpam : CheckResult.Spam; |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Submits the specified comment that was incorrectly marked as spam but should not have been. |
| 64 | + /// </summary> |
| 65 | + /// <param name="comment">The comment to be submitted.</param> |
| 66 | + /// <param name="cancellationToken">The token to cancel the operation.</param> |
| 67 | + /// <returns>Completes once the comment has been submitted.</returns> |
| 68 | + /// <exception cref="HttpRequestException">The remote server returned an invalid response.</exception> |
| 69 | + public async Task SubmitHam(Comment comment, CancellationToken cancellationToken = default) { |
| 70 | + using var response = await Fetch("1.1/submit-ham", comment.ToJson(), cancellationToken); |
| 71 | + var body = await response.Content.ReadAsStringAsync(cancellationToken); |
| 72 | + if (body != Success) throw new HttpRequestException("Invalid server response."); |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Submits the specified comment that was not marked as spam but should have been. |
| 77 | + /// </summary> |
| 78 | + /// <param name="comment">The comment to be submitted.</param> |
| 79 | + /// <param name="cancellationToken">The token to cancel the operation.</param> |
| 80 | + /// <returns>Completes once the comment has been submitted.</returns> |
| 81 | + /// <exception cref="HttpRequestException">The remote server returned an invalid response.</exception> |
| 82 | + public async Task SubmitSpam(Comment comment, CancellationToken cancellationToken = default) { |
| 83 | + using var response = await Fetch("1.1/submit-spam", comment.ToJson(), cancellationToken); |
| 84 | + var body = await response.Content.ReadAsStringAsync(cancellationToken); |
| 85 | + if (body != Success) throw new HttpRequestException("Invalid server response."); |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Checks the API key against the service database, and returns a value indicating whether it is valid. |
| 90 | + /// </summary> |
| 91 | + /// <param name="cancellationToken">The token to cancel the operation.</param> |
| 92 | + /// <returns><see langword="true"/> if the specified API key is valid, otherwise <see langword="false"/>.</returns> |
| 93 | + public async Task<bool> VerifyKey(CancellationToken cancellationToken = default) { |
| 94 | + try { |
| 95 | + using var response = await Fetch("1.1/verify-key", cancellationToken: cancellationToken); |
| 96 | + return await response.Content.ReadAsStringAsync(cancellationToken) == "valid"; |
| 97 | + } |
| 98 | + catch { |
| 99 | + return false; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Queries the service by posting the specified fields to a given end point, and returns the response. |
| 105 | + /// </summary> |
| 106 | + /// <param name="endpoint">The relative URL of the end point to query.</param> |
| 107 | + /// <param name="fields">The fields describing the query body.</param> |
| 108 | + /// <param name="cancellationToken">The token to cancel the operation.</param> |
| 109 | + /// <returns>The server response.</returns> |
| 110 | + /// <exception cref="HttpRequestException">An error occurred while querying the end point.</exception> |
| 111 | + private async Task<HttpResponseMessage> Fetch(string endpoint, IDictionary<string, string>? fields = null, CancellationToken cancellationToken = default) { |
| 112 | + var postFields = Blog.ToJson(); |
| 113 | + postFields.Add("api_key", ApiKey); |
| 114 | + if (IsTest) postFields.Add("is_test", "1"); |
| 115 | + if (fields is not null) foreach (var item in fields) postFields.Add(item.Key, item.Value); |
| 116 | + |
| 117 | + using var request = new HttpRequestMessage(HttpMethod.Post, new Uri(BaseUrl, endpoint)) { Content = new FormUrlEncodedContent(postFields) }; |
| 118 | + request.Headers.Add("User-Agent", UserAgent); |
| 119 | + |
| 120 | + using var httpClient = new HttpClient(); |
| 121 | + var response = await httpClient.SendAsync(request, cancellationToken); |
| 122 | + response.EnsureSuccessStatusCode(); |
| 123 | + if (response.Headers.TryGetValues("X-akismet-alert-msg", out var alertMessages)) throw new HttpRequestException(alertMessages.First()); |
| 124 | + if (response.Headers.TryGetValues("X-akismet-debug-help", out var debugHelps)) throw new HttpRequestException(debugHelps.First()); |
| 125 | + return response; |
| 126 | + } |
45 | 127 | }
|
0 commit comments