Skip to content

Commit c8a5557

Browse files
committed
fix: add missing implementation of calls without source generator
1 parent 002f40a commit c8a5557

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

Catglobe.CgScript.Runtime/ApiClientBase.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,27 @@
22
using System.Net.Http.Json;
33
using System.Text.Json;
44
using System.Text.Json.Serialization.Metadata;
5+
using Microsoft.Extensions.Logging;
56

67
namespace Catglobe.CgScript.Runtime;
78

8-
internal abstract class ApiClientBase(HttpClient httpClient) : ICgScriptApiClient
9+
internal abstract class ApiClientBase(HttpClient httpClient, ILogger<ICgScriptApiClient> logger) : ICgScriptApiClient
910
{
1011
public async Task<ScriptResult<TR>> Execute<TP, TR>(string scriptName, TP parameter, JsonTypeInfo<TP> callJsonTypeInfo, JsonTypeInfo<TR> resultJsonTypeInfo, CancellationToken cancellationToken) =>
1112
await ParseResponse(await httpClient.PostAsync(await GetPath(scriptName), await GetJsonContent(scriptName, parameter, callJsonTypeInfo), cancellationToken).ConfigureAwait(false), resultJsonTypeInfo, cancellationToken);
1213

1314
public async Task<ScriptResult<TR>> Execute<TR>(string scriptName, JsonTypeInfo<TR> resultJsonTypeInfo, CancellationToken cancellationToken = default) =>
1415
await ParseResponse(await httpClient.PostAsync(await GetPath(scriptName, "?expandParameters=true"), await GetJsonContent(scriptName, null, (JsonTypeInfo<object>)null!), cancellationToken).ConfigureAwait(false), resultJsonTypeInfo, cancellationToken);
1516

16-
private static async Task<ScriptResult<TR>> ParseResponse<TR>(HttpResponseMessage call, JsonTypeInfo<TR> resultJsonTypeInfo, CancellationToken cancellationToken)
17+
private async Task<ScriptResult<TR>> ParseResponse<TR>(HttpResponseMessage call, JsonTypeInfo<TR> resultJsonTypeInfo, CancellationToken cancellationToken)
1718
{
1819
var jsonTypeInfo = JsonMetadataServices.CreateValueInfo<ScriptResult<TR>>(new(){TypeInfoResolver = new DummyResolver<TR>(resultJsonTypeInfo)}, new ScriptResultConverterWithTypeInfo<TR>(resultJsonTypeInfo));
19-
call.EnsureSuccessStatusCode();
20+
//if not successful, log the error the server sent
21+
if (!call.IsSuccessStatusCode)
22+
{
23+
logger.LogInformation(await call.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false));
24+
call.EnsureSuccessStatusCode();
25+
}
2026
var result = await call.Content.ReadFromJsonAsync(jsonTypeInfo, cancellationToken).ConfigureAwait(false);
2127
return result ?? throw new IOException("Could not deserialize result");
2228
}
@@ -34,10 +40,15 @@ public async Task<ScriptResult<TR>> Execute<TR>(string scriptName, JsonSerialize
3440
await ParseResponse<TR>(await httpClient.PostAsync(await GetPath(scriptName), await GetJsonContent(scriptName, null, (JsonTypeInfo<object>)null!), cancellationToken).ConfigureAwait(false), options, cancellationToken);
3541

3642
[RequiresUnreferencedCode("JSON")]
37-
private static async Task<ScriptResult<TR>> ParseResponse<TR>(HttpResponseMessage call, JsonSerializerOptions? options, CancellationToken cancellationToken)
43+
private async Task<ScriptResult<TR>> ParseResponse<TR>(HttpResponseMessage call, JsonSerializerOptions? options, CancellationToken cancellationToken)
3844
{
3945
var retOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web) { Converters = { new ScriptResultConverterFactory<TR>(options) } };
40-
call.EnsureSuccessStatusCode();
46+
//if not successful, log the error the server sent
47+
if (!call.IsSuccessStatusCode)
48+
{
49+
logger.LogInformation(await call.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false));
50+
call.EnsureSuccessStatusCode();
51+
}
4152
var result = (ScriptResult<TR>?)await call.Content.ReadFromJsonAsync(typeof(ScriptResult<TR>), retOptions, cancellationToken).ConfigureAwait(false);
4253
return result ?? throw new IOException("Could not deserialize result");
4354
}
@@ -47,7 +58,7 @@ private static async Task<ScriptResult<TR>> ParseResponse<TR>(HttpResponseMessag
4758
protected abstract Task<JsonContent?> GetJsonContent<TP>(string scriptName, TP? parameter, JsonTypeInfo<TP> callJsonTypeInfo);
4859

4960
[RequiresUnreferencedCode("JSON")]
50-
protected abstract Task<JsonContent?> GetJsonContent<TP>(string scriptName, TP? parameter, JsonSerializerOptions? callJsonTypeInfo);
61+
protected abstract Task<JsonContent?> GetJsonContent<TP>(string scriptName, TP? parameter, JsonSerializerOptions? jsonOptions);
5162

5263
private class DummyResolver<T>(JsonTypeInfo info) : IJsonTypeInfoResolver
5364
{

0 commit comments

Comments
 (0)