11using Newtonsoft . Json ;
2- using Newtonsoft . Json . Linq ;
3- using System ;
42using System . Collections . Generic ;
5- using System . Linq ;
63using System . Net . Http ;
74using System . Text ;
85using System . Threading . Tasks ;
@@ -18,93 +15,62 @@ public CustomerWrapper(string apiKey, string apiVersion = "v2") : base(apiKey, a
1815 public async Task < SearchResult < Customer > > ListAsync ( Dictionary < string , object > query = null )
1916 {
2017 var response = await client . GetAsync ( Router . ListCustomers ( query ) ) ;
18+ await this . ThrowIfErrorAsync ( response ) ;
2119 var resultString = await response . Content . ReadAsStringAsync ( ) ;
2220
23- if ( ! response . IsSuccessStatusCode )
24- {
25- var error = JsonConvert . DeserializeObject < JObject > ( resultString ) ;
26- throw new FacturapiException ( error [ "message" ] . ToString ( ) ) ;
27- }
28-
2921 var searchResult = JsonConvert . DeserializeObject < SearchResult < Customer > > ( resultString , this . jsonSettings ) ;
3022 return searchResult ;
3123 }
3224
3325 public async Task < Customer > CreateAsync ( Dictionary < string , object > data , Dictionary < string , object > queryParams = null )
3426 {
3527 var response = await client . PostAsync ( Router . CreateCustomer ( queryParams ) , new StringContent ( JsonConvert . SerializeObject ( data ) , Encoding . UTF8 , "application/json" ) ) ;
28+ await this . ThrowIfErrorAsync ( response ) ;
3629 var resultString = await response . Content . ReadAsStringAsync ( ) ;
37- if ( ! response . IsSuccessStatusCode )
38- {
39- var error = JsonConvert . DeserializeObject < JObject > ( resultString ) ;
40- throw new FacturapiException ( error [ "message" ] . ToString ( ) ) ;
41- }
4230 var customer = JsonConvert . DeserializeObject < Customer > ( resultString , this . jsonSettings ) ;
4331 return customer ;
4432 }
4533
4634 public async Task < Customer > RetrieveAsync ( string id )
4735 {
4836 var response = await client . GetAsync ( Router . RetrieveCustomer ( id ) ) ;
37+ await this . ThrowIfErrorAsync ( response ) ;
4938 var resultString = await response . Content . ReadAsStringAsync ( ) ;
50- if ( ! response . IsSuccessStatusCode )
51- {
52- var error = JsonConvert . DeserializeObject < JObject > ( resultString ) ;
53- throw new FacturapiException ( error [ "message" ] . ToString ( ) ) ;
54- }
5539 var customer = JsonConvert . DeserializeObject < Customer > ( resultString , this . jsonSettings ) ;
5640 return customer ;
5741 }
5842
5943 public async Task < Customer > DeleteAsync ( string id )
6044 {
6145 var response = await client . DeleteAsync ( Router . DeleteCustomer ( id ) ) ;
46+ await this . ThrowIfErrorAsync ( response ) ;
6247 var resultString = await response . Content . ReadAsStringAsync ( ) ;
63- if ( ! response . IsSuccessStatusCode )
64- {
65- var error = JsonConvert . DeserializeObject < JObject > ( resultString ) ;
66- throw new FacturapiException ( error [ "message" ] . ToString ( ) ) ;
67- }
6848 var customer = JsonConvert . DeserializeObject < Customer > ( resultString , this . jsonSettings ) ;
6949 return customer ;
7050 }
7151
7252 public async Task < Customer > UpdateAsync ( string id , Dictionary < string , object > data , Dictionary < string , object > queryParams = null )
7353 {
7454 var response = await client . PutAsync ( Router . UpdateCustomer ( id , queryParams ) , new StringContent ( JsonConvert . SerializeObject ( data ) , Encoding . UTF8 , "application/json" ) ) ;
55+ await this . ThrowIfErrorAsync ( response ) ;
7556 var resultString = await response . Content . ReadAsStringAsync ( ) ;
76- if ( ! response . IsSuccessStatusCode )
77- {
78- var error = JsonConvert . DeserializeObject < JObject > ( resultString ) ;
79- throw new FacturapiException ( error [ "message" ] . ToString ( ) ) ;
80- }
81- var customer = JsonConvert . DeserializeObject < Customer > ( resultString , this . jsonSettings ) ;
82- return customer ;
57+ var customer = JsonConvert . DeserializeObject < Customer > ( resultString , this . jsonSettings ) ;
58+ return customer ;
8359 }
8460
8561 public async Task < TaxInfoValidation > ValidateTaxInfoAsync ( string id )
8662 {
8763 var response = await client . GetAsync ( Router . ValidateCustomerTaxInfo ( id ) ) ;
64+ await this . ThrowIfErrorAsync ( response ) ;
8865 var resultString = await response . Content . ReadAsStringAsync ( ) ;
89- if ( ! response . IsSuccessStatusCode )
90- {
91- var error = JsonConvert . DeserializeObject < JObject > ( resultString ) ;
92- throw new FacturapiException ( error [ "message" ] . ToString ( ) ) ;
93- }
9466 var validation = JsonConvert . DeserializeObject < TaxInfoValidation > ( resultString , this . jsonSettings ) ;
9567 return validation ;
9668 }
9769
9870 public async Task SendEditLinkByEmailAsync ( string id , Dictionary < string , object > data )
9971 {
10072 var response = await client . PostAsync ( Router . SendEditLinkByEmail ( id ) , new StringContent ( JsonConvert . SerializeObject ( data ) , Encoding . UTF8 , "application/json" ) ) ;
101- if ( ! response . IsSuccessStatusCode )
102- {
103- var resultString = await response . Content . ReadAsStringAsync ( ) ;
104- var error = JsonConvert . DeserializeObject < JObject > ( resultString ) ;
105- var errorMessage = error [ "message" ] != null ? error [ "message" ] . ToString ( ) : "An error occurred" ;
106- throw new FacturapiException ( errorMessage ) ;
107- }
73+ await this . ThrowIfErrorAsync ( response ) ;
10874 }
10975 }
11076}
0 commit comments