1414using System . Collections . Generic ;
1515using System . Text . Json ;
1616using System . Text . Json . Serialization ;
17+ using System . Threading ;
1718using System . Threading . Tasks ;
1819
1920namespace Keyfactor . Extensions . CAPlugin . HashicorpVault . Client
@@ -36,12 +37,12 @@ public VaultHttp(string host, string mountPoint, string authToken, string nameSp
3637 _serializerOptions = new ( )
3738 {
3839 DefaultIgnoreCondition = JsonIgnoreCondition . WhenWritingDefault ,
39- RespectNullableAnnotations = true ,
4040 PropertyNameCaseInsensitive = true ,
41- PreferredObjectCreationHandling = JsonObjectCreationHandling . Replace ,
41+ RespectNullableAnnotations = true ,
42+ PreferredObjectCreationHandling = JsonObjectCreationHandling . Replace
4243 } ;
4344
44- var restClientOptions = new RestClientOptions ( $ "{ host . TrimEnd ( '/' ) } /v1") { ThrowOnAnyError = true } ;
45+ var restClientOptions = new RestClientOptions ( $ "{ host . TrimEnd ( '/' ) } /v1") { ThrowOnAnyError = true } ;
4546 _restClient = new RestClient ( restClientOptions , configureSerialization : s => s . UseSystemTextJson ( _serializerOptions ) ) ;
4647
4748 _mountPoint = mountPoint . TrimStart ( '/' ) . TrimEnd ( '/' ) ; // remove leading and trailing slashes
@@ -69,19 +70,32 @@ public VaultHttp(string host, string mountPoint, string authToken, string nameSp
6970 public async Task < T > GetAsync < T > ( string path , Dictionary < string , string > parameters = null )
7071 {
7172 logger . MethodEntry ( ) ;
72- logger . LogTrace ( $ "preparing to send GET request to { path } with parameters { JsonSerializer . Serialize ( parameters ) } ") ;
73- logger . LogTrace ( $ "will attempt to deserialize the response into a { typeof ( T ) } " ) ;
73+ logger . LogTrace ( $ "preparing to send GET request to { _mountPoint } / { path } with parameters { JsonSerializer . Serialize ( parameters ) } ") ;
74+
7475 try
7576 {
7677 var request = new RestRequest ( $ "{ _mountPoint } /{ path } ", Method . Get ) ;
77- if ( parameters != null ) { request . AddJsonBody ( parameters ) ; }
78+ if ( parameters != null && parameters . Keys . Count > 0 ) { request . AddJsonBody ( parameters ) ; }
79+ var response = await _restClient . ExecuteGetAsync ( request ) ;
80+
81+ logger . LogTrace ( $ "raw response: { JsonSerializer . Serialize ( response ) } ") ;
82+
83+ logger . LogTrace ( $ "response content: { response . Content } ") ;
84+
85+ logger . LogTrace ( $ "response status: { response . StatusCode } ") ;
7886
79- var response = await _restClient . ExecuteGetAsync < T > ( request ) ;
80- logger . LogTrace ( $ "raw response: { response . Content } ") ;
87+ logger . LogTrace ( $ "response error msg: { response . ErrorMessage } ") ;
8188
8289 response . ThrowIfError ( ) ;
90+ if ( string . IsNullOrEmpty ( response . Content ) ) throw new Exception ( response . ErrorMessage ?? "no content returned from Vault" ) ;
8391
84- return response . Data ;
92+ logger . LogTrace ( $ "deserializing the response into a { typeof ( T ) } ") ;
93+
94+ var deserialized = JsonSerializer . Deserialize < T > ( response . Content , _serializerOptions ) ;
95+
96+ logger . LogTrace ( $ "successfully deserialized the response") ;
97+
98+ return deserialized ;
8599 }
86100 catch ( Exception ex )
87101 {
@@ -108,8 +122,8 @@ public async Task<T> PostAsync<T>(string path, dynamic parameters = default)
108122 var request = new RestRequest ( resourcePath , Method . Post ) ;
109123 if ( parameters != null )
110124 {
111- string serializedParams = JsonSerializer . Serialize ( parameters , _serializerOptions ) ;
112- logger . LogTrace ( $ "serialized parameters (from { parameters . GetType ( ) ? . Name } ): { serializedParams } ") ;
125+ string serializedParams = JsonSerializer . Serialize ( parameters ) ;
126+ logger . LogTrace ( $ "deserialized parameters (from { parameters . GetType ( ) ? . Name } ): { serializedParams } ") ;
113127 request . AddJsonBody ( serializedParams ) ;
114128 }
115129
@@ -127,7 +141,7 @@ public async Task<T> PostAsync<T>(string path, dynamic parameters = default)
127141
128142 if ( response . StatusCode == System . Net . HttpStatusCode . BadRequest )
129143 {
130- errorResponse = JsonSerializer . Deserialize < ErrorResponse > ( response . Content ! ) ;
144+ errorResponse = JsonSerializer . Deserialize < ErrorResponse > ( response . Content ?? "no content" ) ;
131145 string allErrors = "(Bad Request)" ;
132146 if ( errorResponse ? . Errors . Count > 0 )
133147 {
0 commit comments