44using System . Net . Http ;
55using System . Net . Http . Json ;
66using SimApi . Communications ;
7+ using SimApi . Exceptions ;
78
89namespace SimApi . Helpers ;
910
@@ -37,9 +38,7 @@ public class SimApiHttpClient(string? appId, string appKey)
3738 . Aggregate ( path , ( current , q ) => current + $ "&{ q . Key } ={ q . Value } ") ;
3839 }
3940
40- var http = new HttpClient ( ) ;
41- var resp = http . PostAsJsonAsync ( path , body ) . Result ;
42- return resp . Content . ReadFromJsonAsync < T > ( ) . Result ;
41+ return Query < T > ( path , body ) ;
4342 }
4443
4544 public T ? AesQuery < T > ( string url , object body )
@@ -54,9 +53,7 @@ public class SimApiHttpClient(string? appId, string appKey)
5453 {
5554 Data = SimApiAesUtil . Encrypt ( SimApiUtil . Json ( body ) , appKey )
5655 } ;
57- var http = new HttpClient ( ) ;
58- var resp = http . PostAsJsonAsync ( url , req ) . Result ;
59- return resp . Content . ReadFromJsonAsync < T > ( ) . Result ;
56+ return Query < T > ( url , req ) ;
6057 }
6158
6259 public T ? AesSignQuery < T > ( string url , object body , Dictionary < string , string > ? queries = null )
@@ -67,4 +64,17 @@ public class SimApiHttpClient(string? appId, string appKey)
6764 } ;
6865 return SignQuery < T > ( url , req , queries ) ;
6966 }
67+
68+ private T ? Query < T > ( string url , object ? req )
69+ {
70+ var http = new HttpClient ( ) ;
71+ var resp = http . PostAsJsonAsync ( url , req ) . Result ;
72+ var res = resp . Content . ReadFromJsonAsync < SimApiBaseResponse < T > > ( ) . Result ;
73+ if ( res == null )
74+ {
75+ throw new SimApiException ( 500 , "请求发生错误" ) ;
76+ }
77+
78+ return res . Code != 200 ? throw new SimApiException ( res . Code , res . Message ) : res . Data ;
79+ }
7080}
0 commit comments