1
- using System . Diagnostics . CodeAnalysis ;
1
+ using System . Diagnostics ;
2
+ using System . Diagnostics . CodeAnalysis ;
2
3
using System . Net . Http . Json ;
3
4
using System . Text . Json ;
4
5
using System . Text . Json . Serialization . Metadata ;
6
+ using Catglobe . CgScript . Common ;
5
7
using Microsoft . Extensions . Logging ;
6
8
7
9
namespace Catglobe . CgScript . Runtime ;
8
10
9
11
internal abstract class ApiClientBase ( HttpClient httpClient , ILogger < ICgScriptApiClient > logger ) : ICgScriptApiClient
10
12
{
11
- public async Task < ScriptResult < TR > > Execute < TP , TR > ( string scriptName , TP parameter , JsonTypeInfo < TP > callJsonTypeInfo , JsonTypeInfo < TR > resultJsonTypeInfo , CancellationToken cancellationToken ) =>
12
- await ParseResponse ( await httpClient . PostAsync ( await GetPath ( scriptName ) , await GetJsonContent ( scriptName , parameter , callJsonTypeInfo ) , cancellationToken ) . ConfigureAwait ( false ) , resultJsonTypeInfo , cancellationToken ) ;
13
+ public async Task < ScriptResult < TR > > Execute < TP , TR > ( string scriptName , TP parameter , JsonTypeInfo < TP > callJsonTypeInfo , JsonTypeInfo < TR > resultJsonTypeInfo , CancellationToken cancellationToken )
14
+ {
15
+ using var activity = CgScriptTelemetry . Source . StartActivity ( scriptName ) ;
16
+ return await ParseResponse ( await httpClient . PostAsync ( await GetPath ( scriptName ) , await GetJsonContent ( scriptName , parameter , callJsonTypeInfo ) , cancellationToken ) . ConfigureAwait ( false ) , resultJsonTypeInfo , cancellationToken ) ;
17
+ }
13
18
14
- public async Task < ScriptResult < TR > > ExecuteArray < TP , TR > ( string scriptName , TP parameter , JsonTypeInfo < TP > callJsonTypeInfo , JsonTypeInfo < TR > resultJsonTypeInfo , CancellationToken cancellationToken ) =>
15
- await ParseResponse ( await httpClient . PostAsync ( await GetPath ( scriptName , "?expandParameters=true" ) , await GetJsonContent ( scriptName , parameter , callJsonTypeInfo ) , cancellationToken ) . ConfigureAwait ( false ) , resultJsonTypeInfo , cancellationToken ) ;
19
+ public async Task < ScriptResult < TR > > ExecuteArray < TP , TR > ( string scriptName , TP parameter , JsonTypeInfo < TP > callJsonTypeInfo , JsonTypeInfo < TR > resultJsonTypeInfo , CancellationToken cancellationToken )
20
+ {
21
+ using var activity = CgScriptTelemetry . Source . StartActivity ( scriptName ) ;
22
+ return await ParseResponse ( await httpClient . PostAsync ( await GetPath ( scriptName , "?expandParameters=true" ) , await GetJsonContent ( scriptName , parameter , callJsonTypeInfo ) , cancellationToken ) . ConfigureAwait ( false ) , resultJsonTypeInfo , cancellationToken ) ;
23
+ }
16
24
17
- public async Task < ScriptResult < TR > > Execute < TR > ( string scriptName , JsonTypeInfo < TR > resultJsonTypeInfo , CancellationToken cancellationToken = default ) =>
18
- await ParseResponse ( await httpClient . PostAsync ( await GetPath ( scriptName , "?expandParameters=true" ) , await GetJsonContent ( scriptName , null , ( JsonTypeInfo < object > ) null ! ) , cancellationToken ) . ConfigureAwait ( false ) , resultJsonTypeInfo , cancellationToken ) ;
25
+ public async Task < ScriptResult < TR > > Execute < TR > ( string scriptName , JsonTypeInfo < TR > resultJsonTypeInfo , CancellationToken cancellationToken = default )
26
+ {
27
+ using var activity = CgScriptTelemetry . Source . StartActivity ( scriptName ) ;
28
+ return await ParseResponse ( await httpClient . PostAsync ( await GetPath ( scriptName , "?expandParameters=true" ) , await GetJsonContent ( scriptName , null , ( JsonTypeInfo < object > ) null ! ) , cancellationToken ) . ConfigureAwait ( false ) , resultJsonTypeInfo , cancellationToken ) ;
29
+ }
19
30
20
31
private async Task < ScriptResult < TR > > ParseResponse < TR > ( HttpResponseMessage call , JsonTypeInfo < TR > resultJsonTypeInfo , CancellationToken cancellationToken )
21
32
{
22
33
var jsonTypeInfo = JsonMetadataServices . CreateValueInfo < ScriptResult < TR > > ( new ( ) { TypeInfoResolver = new DummyResolver < TR > ( resultJsonTypeInfo ) } , new ScriptResultConverterWithTypeInfo < TR > ( resultJsonTypeInfo ) ) ;
23
34
//if not successful, log the error the server sent
24
35
if ( ! call . IsSuccessStatusCode )
25
36
{
37
+ Activity . Current ? . SetStatus ( ActivityStatusCode . Error ) ;
26
38
logger . LogInformation ( await call . Content . ReadAsStringAsync ( cancellationToken ) . ConfigureAwait ( false ) ) ;
27
39
call . EnsureSuccessStatusCode ( ) ;
28
40
}
29
41
var result = await call . Content . ReadFromJsonAsync ( jsonTypeInfo , cancellationToken ) . ConfigureAwait ( false ) ;
42
+ if ( result ? . Error is not null )
43
+ {
44
+ Activity . Current ? . SetStatus ( ActivityStatusCode . Error ) ;
45
+ }
30
46
return result ?? throw new IOException ( "Could not deserialize result" ) ;
31
47
}
32
48
33
49
[ RequiresUnreferencedCode ( "JSON" ) ]
34
- public async Task < ScriptResult < TR > > Execute < TP , TR > ( string scriptName , TP parameter , JsonSerializerOptions ? options , CancellationToken cancellationToken = default ) =>
35
- await ParseResponse < TR > ( await httpClient . PostAsync ( await GetPath ( scriptName ) , await GetJsonContent ( scriptName , parameter , options ) , cancellationToken ) . ConfigureAwait ( false ) , options , cancellationToken ) ;
50
+ public async Task < ScriptResult < TR > > Execute < TP , TR > ( string scriptName , TP parameter , JsonSerializerOptions ? options , CancellationToken cancellationToken = default )
51
+ {
52
+ using var activity = CgScriptTelemetry . Source . StartActivity ( scriptName ) ;
53
+ return await ParseResponse < TR > ( await httpClient . PostAsync ( await GetPath ( scriptName ) , await GetJsonContent ( scriptName , parameter , options ) , cancellationToken ) . ConfigureAwait ( false ) , options , cancellationToken ) ;
54
+ }
55
+
36
56
[ RequiresUnreferencedCode ( "JSON" ) ]
37
- public async Task < ScriptResult < TR > > ExecuteArray < TP , TR > ( string scriptName , TP parameter , JsonSerializerOptions ? options , CancellationToken cancellationToken = default ) =>
38
- await ParseResponse < TR > ( await httpClient . PostAsync ( await GetPath ( scriptName , "?expandParameters=true" ) , await GetJsonContent ( scriptName , parameter , options ) , cancellationToken ) . ConfigureAwait ( false ) , options , cancellationToken ) ;
57
+ public async Task < ScriptResult < TR > > ExecuteArray < TP , TR > ( string scriptName , TP parameter , JsonSerializerOptions ? options , CancellationToken cancellationToken = default )
58
+ {
59
+ using var activity = CgScriptTelemetry . Source . StartActivity ( scriptName ) ;
60
+ return await ParseResponse < TR > ( await httpClient . PostAsync ( await GetPath ( scriptName , "?expandParameters=true" ) , await GetJsonContent ( scriptName , parameter , options ) , cancellationToken ) . ConfigureAwait ( false ) , options , cancellationToken ) ;
61
+ }
39
62
40
63
[ RequiresUnreferencedCode ( "JSON" ) ]
41
- public async Task < ScriptResult < TR > > Execute < TR > ( string scriptName , IReadOnlyCollection < object > parameters , JsonSerializerOptions ? options = null , CancellationToken cancellationToken = default ) =>
42
- await ParseResponse < TR > ( await httpClient . PostAsync ( await GetPath ( scriptName , "?expandParameters=true" ) , await GetJsonContent ( scriptName , parameters , options ) , cancellationToken ) . ConfigureAwait ( false ) , options , cancellationToken ) ;
64
+ public async Task < ScriptResult < TR > > Execute < TR > ( string scriptName , IReadOnlyCollection < object > parameters , JsonSerializerOptions ? options = null , CancellationToken cancellationToken = default )
65
+ {
66
+ using var activity = CgScriptTelemetry . Source . StartActivity ( scriptName ) ;
67
+ return await ParseResponse < TR > ( await httpClient . PostAsync ( await GetPath ( scriptName , "?expandParameters=true" ) , await GetJsonContent ( scriptName , parameters , options ) , cancellationToken ) . ConfigureAwait ( false ) , options , cancellationToken ) ;
68
+ }
43
69
44
70
[ RequiresUnreferencedCode ( "JSON" ) ]
45
- public async Task < ScriptResult < TR > > Execute < TR > ( string scriptName , JsonSerializerOptions ? options = null , CancellationToken cancellationToken = default ) =>
46
- await ParseResponse < TR > ( await httpClient . PostAsync ( await GetPath ( scriptName ) , await GetJsonContent ( scriptName , null , ( JsonTypeInfo < object > ) null ! ) , cancellationToken ) . ConfigureAwait ( false ) , options , cancellationToken ) ;
71
+ public async Task < ScriptResult < TR > > Execute < TR > ( string scriptName , JsonSerializerOptions ? options = null , CancellationToken cancellationToken = default )
72
+ {
73
+ using var activity = CgScriptTelemetry . Source . StartActivity ( scriptName ) ;
74
+ return await ParseResponse < TR > ( await httpClient . PostAsync ( await GetPath ( scriptName ) , await GetJsonContent ( scriptName , null , ( JsonTypeInfo < object > ) null ! ) , cancellationToken ) . ConfigureAwait ( false ) , options , cancellationToken ) ;
75
+ }
47
76
48
77
[ RequiresUnreferencedCode ( "JSON" ) ]
49
78
private async Task < ScriptResult < TR > > ParseResponse < TR > ( HttpResponseMessage call , JsonSerializerOptions ? options , CancellationToken cancellationToken )
@@ -52,10 +81,15 @@ private async Task<ScriptResult<TR>> ParseResponse<TR>(HttpResponseMessage call,
52
81
//if not successful, log the error the server sent
53
82
if ( ! call . IsSuccessStatusCode )
54
83
{
84
+ Activity . Current ? . SetStatus ( ActivityStatusCode . Error ) ;
55
85
logger . LogInformation ( await call . Content . ReadAsStringAsync ( cancellationToken ) . ConfigureAwait ( false ) ) ;
56
86
call . EnsureSuccessStatusCode ( ) ;
57
87
}
58
88
var result = ( ScriptResult < TR > ? ) await call . Content . ReadFromJsonAsync ( typeof ( ScriptResult < TR > ) , retOptions , cancellationToken ) . ConfigureAwait ( false ) ;
89
+ if ( result ? . Error is not null )
90
+ {
91
+ Activity . Current ? . SetStatus ( ActivityStatusCode . Error ) ;
92
+ }
59
93
return result ?? throw new IOException ( "Could not deserialize result" ) ;
60
94
}
61
95
0 commit comments