2
2
using System . Net . Http . Json ;
3
3
using System . Text . Json ;
4
4
using System . Text . Json . Serialization . Metadata ;
5
+ using Microsoft . Extensions . Logging ;
5
6
6
7
namespace Catglobe . CgScript . Runtime ;
7
8
8
- internal abstract class ApiClientBase ( HttpClient httpClient ) : ICgScriptApiClient
9
+ internal abstract class ApiClientBase ( HttpClient httpClient , ILogger < ICgScriptApiClient > logger ) : ICgScriptApiClient
9
10
{
10
11
public async Task < ScriptResult < TR > > Execute < TP , TR > ( string scriptName , TP parameter , JsonTypeInfo < TP > callJsonTypeInfo , JsonTypeInfo < TR > resultJsonTypeInfo , CancellationToken cancellationToken ) =>
11
12
await ParseResponse ( await httpClient . PostAsync ( await GetPath ( scriptName ) , await GetJsonContent ( scriptName , parameter , callJsonTypeInfo ) , cancellationToken ) . ConfigureAwait ( false ) , resultJsonTypeInfo , cancellationToken ) ;
12
13
13
14
public async Task < ScriptResult < TR > > Execute < TR > ( string scriptName , JsonTypeInfo < TR > resultJsonTypeInfo , CancellationToken cancellationToken = default ) =>
14
15
await ParseResponse ( await httpClient . PostAsync ( await GetPath ( scriptName , "?expandParameters=true" ) , await GetJsonContent ( scriptName , null , ( JsonTypeInfo < object > ) null ! ) , cancellationToken ) . ConfigureAwait ( false ) , resultJsonTypeInfo , cancellationToken ) ;
15
16
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 )
17
18
{
18
19
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
+ }
20
26
var result = await call . Content . ReadFromJsonAsync ( jsonTypeInfo , cancellationToken ) . ConfigureAwait ( false ) ;
21
27
return result ?? throw new IOException ( "Could not deserialize result" ) ;
22
28
}
@@ -34,10 +40,15 @@ public async Task<ScriptResult<TR>> Execute<TR>(string scriptName, JsonSerialize
34
40
await ParseResponse < TR > ( await httpClient . PostAsync ( await GetPath ( scriptName ) , await GetJsonContent ( scriptName , null , ( JsonTypeInfo < object > ) null ! ) , cancellationToken ) . ConfigureAwait ( false ) , options , cancellationToken ) ;
35
41
36
42
[ 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 )
38
44
{
39
45
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
+ }
41
52
var result = ( ScriptResult < TR > ? ) await call . Content . ReadFromJsonAsync ( typeof ( ScriptResult < TR > ) , retOptions , cancellationToken ) . ConfigureAwait ( false ) ;
42
53
return result ?? throw new IOException ( "Could not deserialize result" ) ;
43
54
}
@@ -47,7 +58,7 @@ private static async Task<ScriptResult<TR>> ParseResponse<TR>(HttpResponseMessag
47
58
protected abstract Task < JsonContent ? > GetJsonContent < TP > ( string scriptName , TP ? parameter , JsonTypeInfo < TP > callJsonTypeInfo ) ;
48
59
49
60
[ 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 ) ;
51
62
52
63
private class DummyResolver < T > ( JsonTypeInfo info ) : IJsonTypeInfoResolver
53
64
{
0 commit comments