Skip to content

Commit 4dd652c

Browse files
committed
fix: reddit feed user-agent set as raw header so the refresh job no longer fails
1 parent b1be127 commit 4dd652c

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

api/API/Discovery/RedditFeedClient.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ namespace API.Discovery;
55
/// <summary>Reddit's public Atom feeds. Reddit rate-limits aggressively (429), so callers cache and degrade.</summary>
66
public class RedditFeedClient(HttpClient http) : IRedditFeedClient
77
{
8+
/// <summary>Reddit's recommended User-Agent shape (platform:appid (comment)).</summary>
9+
public const string UserAgent = "kenku:discovery (self-hosted manga manager)";
10+
11+
/// <summary>Sets the reddit User-Agent as a raw header — the typed <c>UserAgent.ParseAdd</c> rejects
12+
/// this otherwise-valid value and throws at client construction.</summary>
13+
public static void ConfigureClient(HttpClient client) =>
14+
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", UserAgent);
15+
16+
817
public async Task<List<DiscoveryEntry>> GetHotAsync(string subreddit, int limit, CancellationToken ct)
918
{
1019
using HttpResponseMessage response = await http.GetAsync(

api/API/Extensions/ApplicationServiceCollectionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ public static IServiceCollection AddKenkuServices(this IServiceCollection servic
141141
// Discovery rails: edge clients own their HTTP; results cache for an hour per rail.
142142
services.AddSingleton<API.Discovery.DiscoveryCache>();
143143
services.AddHttpClient<API.Discovery.IAniListClient, API.Discovery.AniListClient>();
144-
services.AddHttpClient<API.Discovery.IRedditFeedClient, API.Discovery.RedditFeedClient>(c =>
145-
c.DefaultRequestHeaders.UserAgent.ParseAdd("kenku:discovery (self-hosted manga manager)"));
144+
services.AddHttpClient<API.Discovery.IRedditFeedClient, API.Discovery.RedditFeedClient>(
145+
API.Discovery.RedditFeedClient.ConfigureClient);
146146
services.AddSingleton<Kenku>();
147147
return services;
148148
}

api/Tests/Unit/Discovery/RedditFeedClientTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ namespace API.Tests.Unit.Discovery;
66
/// <summary>Atom shape pinned against a captured live r/manga feed (2026-06-11).</summary>
77
public class RedditFeedClientTests
88
{
9+
[Fact]
10+
public void ConfigureClient_SetsTheUserAgent_WithoutTrippingTheStrictHeaderParser()
11+
{
12+
// Reddit's recommended UA ("kenku:discovery (self-hosted manga manager)") is rejected by the
13+
// typed UserAgent parser, which previously threw at client construction and wedged the feed
14+
// job in NeedsAttention. It must be set as a raw header instead.
15+
using var client = new HttpClient();
16+
17+
RedditFeedClient.ConfigureClient(client);
18+
19+
Assert.True(client.DefaultRequestHeaders.TryGetValues("User-Agent", out var values));
20+
Assert.Equal(RedditFeedClient.UserAgent, Assert.Single(values!));
21+
}
22+
923
private const string Feed = """
1024
<?xml version="1.0" encoding="UTF-8"?>
1125
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">

0 commit comments

Comments
 (0)