Skip to content

Commit

Permalink
feat: adapt source generation
Browse files Browse the repository at this point in the history
  • Loading branch information
tautcony committed Dec 7, 2023
1 parent efb2939 commit 8f5ed59
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
20 changes: 9 additions & 11 deletions OKP.Core/Interface/Acgnx/AcgnxAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Serilog;
using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using static OKP.Core.Interface.TorrentContent;

Expand Down Expand Up @@ -101,11 +100,7 @@ public override async Task<HttpResult> PingAsync()

if (result.IsSuccessStatusCode && !raw.Contains("<html"))
{
var apiContent = await result.Content.ReadFromJsonAsync<AcgnxApiStatus>(new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
NumberHandling = JsonNumberHandling.AllowReadingFromString
});
var apiContent = await result.Content.ReadFromJsonAsync(AcgnxModelsSourceGenerationContext.Default.AcgnxApiStatus);
if (apiContent == null)
{
Log.Error("{Site} api server down", site);
Expand Down Expand Up @@ -160,11 +155,7 @@ public override async Task<HttpResult> PostAsync()
Log.Verbose("{Site} formdata content: {@MultipartFormDataContent}", site, form);
var result = await httpClient.PostAsyncWithRetry(apiUrl, form);
var raw = await result.Content.ReadAsStringAsync();
var apiContent = await result.Content.ReadFromJsonAsync<AcgnxApiStatus>(new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
NumberHandling = JsonNumberHandling.AllowReadingFromString
});
var apiContent = await result.Content.ReadFromJsonAsync(AcgnxModelsSourceGenerationContext.Default.AcgnxApiStatus);

if (result.StatusCode == HttpStatusCode.OK && apiContent != null && !raw.Contains("<html"))
{
Expand Down Expand Up @@ -223,4 +214,11 @@ internal class AcgnxApiStatus
public string? Infohash { get; set; }
public string? Title { get; set; }
}

[JsonSerializable(typeof(AcgnxApiStatus))]
[JsonSourceGenerationOptions(
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
NumberHandling = JsonNumberHandling.AllowReadingFromString)
]
internal partial class AcgnxModelsSourceGenerationContext : JsonSerializerContext;
}
10 changes: 5 additions & 5 deletions OKP.Core/Interface/Bangumi/BangumiAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override async Task<HttpResult> PingAsync()
{
var pingReq = await httpClient.GetAsync(pingUrl);
var raw = await pingReq.Content.ReadAsStringAsync();
var teamList = await pingReq.Content.ReadFromJsonAsync<TeamInfo[]>();
var teamList = await pingReq.Content.ReadFromJsonAsync(BangumiModelsSourceGenerationContext.Default.TeamInfoArray);
if (!pingReq.IsSuccessStatusCode || teamList == null)
{
Log.Error("Cannot connect to {Site}.{NewLine}" +
Expand Down Expand Up @@ -107,9 +107,9 @@ public override async Task<HttpResult> PostAsync()
teamsync = false,
file_id = fileId,
};
var response = await httpClient.PostAsJsonAsyncWithRetry(postUrl, addRequest);
var response = await httpClient.PostAsJsonAsyncWithRetry(postUrl, addRequest, BangumiModelsSourceGenerationContext.Default.AddRequest);
var raw = await response.Content.ReadAsStringAsync();
var result = await response.Content.ReadFromJsonAsync<AddResponse>();
var result = await response.Content.ReadFromJsonAsync(BangumiModelsSourceGenerationContext.Default.AddResponse);
if (!response.IsSuccessStatusCode || result == null)
{
Log.Error("{Site} upload failed. Unknown reson. {NewLine} {Raw}", site, Environment.NewLine, raw);
Expand All @@ -134,13 +134,13 @@ private async Task<string> UploadTorrent(TorrentContent torrent)
{ torrent.Data.ByteArrayContent, "file", torrent.Data.FileInfo.Name }
};
var response = await httpClient.PostAsyncWithRetry(uploadUrl, form);
var result = await response.Content.ReadFromJsonAsync<UploadResponse>();
var result = await response.Content.ReadFromJsonAsync(BangumiModelsSourceGenerationContext.Default.UploadResponse);
if (result == null || !result.success || result.file_id == null)
{
Log.Debug("可能是 {Site} 发布频率限制,先等 1 分钟", site);
Thread.Sleep(60000);
response = await httpClient.PostAsyncWithRetry(uploadUrl, form);
result = await response.Content.ReadFromJsonAsync<UploadResponse>();
result = await response.Content.ReadFromJsonAsync(BangumiModelsSourceGenerationContext.Default.UploadResponse);
if (result == null || !result.success || result.file_id == null)
{
throw new HttpRequestException("Failed to upload torrent file");
Expand Down
17 changes: 16 additions & 1 deletion OKP.Core/Interface/Bangumi/BangumiModels.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// ReSharper disable InconsistentNaming, IdentifierTypo

using System.Text.Json.Serialization;

namespace OKP.Core.Interface.Bangumi
{
#pragma warning disable CS8618
Expand Down Expand Up @@ -75,6 +78,18 @@ public class TeamInfo
public DateTime regDate { get; set; }
public bool approved { get; set; }
}

}

[JsonSerializable(typeof(BangumiModels.UploadResponse))]
[JsonSerializable(typeof(BangumiModels.AddRequest))]
[JsonSerializable(typeof(BangumiModels.AddResponse))]
[JsonSerializable(typeof(BangumiModels.Torrent))]
[JsonSerializable(typeof(BangumiModels.TeamList))]
[JsonSerializable(typeof(BangumiModels.TeamInfo))]
[JsonSerializable(typeof(BangumiModels.TeamInfo[]))]
[JsonSourceGenerationOptions(
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
NumberHandling = JsonNumberHandling.AllowReadingFromString)
]
internal partial class BangumiModelsSourceGenerationContext : JsonSerializerContext;
}
5 changes: 3 additions & 2 deletions OKP.Core/Utils/HttpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Net;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json.Serialization.Metadata;
using System.Text.RegularExpressions;

namespace OKP.Core.Utils
Expand Down Expand Up @@ -37,13 +38,13 @@ public static async Task<HttpResponseMessage> GetAsyncWithRetry(this HttpClient
HandleSetCookie(httpClient, setCookie, res);
return res;
}
public static async Task<HttpResponseMessage> PostAsJsonAsyncWithRetry<TValue>(this HttpClient httpClient, string? uri, TValue content, bool setCookie = true, CancellationToken cancellationToken = default)
public static async Task<HttpResponseMessage> PostAsJsonAsyncWithRetry<TValue>(this HttpClient httpClient, string? uri, TValue content, JsonTypeInfo<TValue> jsonTypeInfo, bool setCookie = true, CancellationToken cancellationToken = default)
{
if (httpClient.BaseAddress is null)
{
throw new NotImplementedException("httpClient.BaseAddress is null");
}
var res = await policy.ExecuteAsync(() => httpClient.PostAsJsonAsync(uri, content, cancellationToken));
var res = await policy.ExecuteAsync(() => httpClient.PostAsJsonAsync(uri, content, jsonTypeInfo, cancellationToken));
HandleSetCookie(httpClient, setCookie, res);
return res;
}
Expand Down
7 changes: 5 additions & 2 deletions OKP.Core/Utils/TagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace OKP.Core.Utils;

public class Tag
{
[JsonConverter(typeof(JsonStringEnumConverter))]
[JsonConverter(typeof(JsonStringEnumConverter<TorrentContent.ContentTypes>))]
public TorrentContent.ContentTypes? Key { get; set; }

public string? Value { get; set; }
Expand Down Expand Up @@ -80,6 +80,9 @@ public Tag()
}
}

[JsonSerializable(typeof(Tag))]
internal partial class TagSourceGenerationContext : JsonSerializerContext;

public static class TagHelper
{
public static Tag LoadTagConfig(string configName)
Expand All @@ -92,7 +95,7 @@ public static Tag LoadTagConfig(string configName)
var content = File.ReadAllText(configPath);
try
{
var ret = JsonSerializer.Deserialize<Tag>(content);
var ret = JsonSerializer.Deserialize(content, TagSourceGenerationContext.Default.Tag);
return ret ?? new Tag();
}
catch (JsonException ex)
Expand Down

0 comments on commit 8f5ed59

Please sign in to comment.