11using Newtonsoft . Json ;
2- using Newtonsoft . Json . Linq ;
32using System . Collections . Generic ;
43using System . Net . Http ;
5- using System . Text ;
64using System . Threading . Tasks ;
75
86namespace Facturapi . Wrappers
@@ -15,183 +13,74 @@ public CatalogWrapper(string apiKey, string apiVersion = "v2") : base(apiKey, ap
1513
1614 public async Task < SearchResult < CatalogItem > > SearchProducts ( Dictionary < string , object > query = null )
1715 {
18- var response = await client . GetAsync ( Router . SearchProductKeys ( query ) ) ;
19- var resultString = await response . Content . ReadAsStringAsync ( ) ;
20-
21- if ( ! response . IsSuccessStatusCode )
22- {
23- var error = JsonConvert . DeserializeObject < JObject > ( resultString , this . jsonSettings ) ;
24- throw new FacturapiException ( error [ "message" ] . ToString ( ) ) ;
25- }
26-
27- var searchResult = JsonConvert . DeserializeObject < SearchResult < CatalogItem > > ( resultString , this . jsonSettings ) ;
28- return searchResult ;
16+ return await this . SearchCatalogAsync ( Router . SearchProductKeys ( query ) ) ;
2917 }
3018
3119 public async Task < SearchResult < CatalogItem > > SearchUnits ( Dictionary < string , object > query = null )
3220 {
33- var response = await client . GetAsync ( Router . SearchUnitKeys ( query ) ) ;
34- var resultString = await response . Content . ReadAsStringAsync ( ) ;
35-
36- if ( ! response . IsSuccessStatusCode )
37- {
38- var error = JsonConvert . DeserializeObject < JObject > ( resultString , this . jsonSettings ) ;
39- throw new FacturapiException ( error [ "message" ] . ToString ( ) ) ;
40- }
41-
42- var searchResult = JsonConvert . DeserializeObject < SearchResult < CatalogItem > > ( resultString , this . jsonSettings ) ;
43- return searchResult ;
21+ return await this . SearchCatalogAsync ( Router . SearchUnitKeys ( query ) ) ;
4422 }
4523
4624 // Carta Porte catalogs
4725 public async Task < SearchResult < CatalogItem > > SearchAirTransportCodes ( Dictionary < string , object > query = null )
4826 {
49- var response = await client . GetAsync ( Router . SearchCartaporteAirTransportCodes ( query ) ) ;
50- var resultString = await response . Content . ReadAsStringAsync ( ) ;
51-
52- if ( ! response . IsSuccessStatusCode )
53- {
54- var error = JsonConvert . DeserializeObject < JObject > ( resultString , this . jsonSettings ) ;
55- throw new FacturapiException ( error [ "message" ] . ToString ( ) ) ;
56- }
57-
58- var searchResult = JsonConvert . DeserializeObject < SearchResult < CatalogItem > > ( resultString , this . jsonSettings ) ;
59- return searchResult ;
27+ return await this . SearchCatalogAsync ( Router . SearchCartaporteAirTransportCodes ( query ) ) ;
6028 }
6129
6230 public async Task < SearchResult < CatalogItem > > SearchTransportConfigs ( Dictionary < string , object > query = null )
6331 {
64- var response = await client . GetAsync ( Router . SearchCartaporteTransportConfigs ( query ) ) ;
65- var resultString = await response . Content . ReadAsStringAsync ( ) ;
66-
67- if ( ! response . IsSuccessStatusCode )
68- {
69- var error = JsonConvert . DeserializeObject < JObject > ( resultString , this . jsonSettings ) ;
70- throw new FacturapiException ( error [ "message" ] . ToString ( ) ) ;
71- }
72-
73- var searchResult = JsonConvert . DeserializeObject < SearchResult < CatalogItem > > ( resultString , this . jsonSettings ) ;
74- return searchResult ;
32+ return await this . SearchCatalogAsync ( Router . SearchCartaporteTransportConfigs ( query ) ) ;
7533 }
7634
7735 public async Task < SearchResult < CatalogItem > > SearchRightsOfPassage ( Dictionary < string , object > query = null )
7836 {
79- var response = await client . GetAsync ( Router . SearchCartaporteRightsOfPassage ( query ) ) ;
80- var resultString = await response . Content . ReadAsStringAsync ( ) ;
81-
82- if ( ! response . IsSuccessStatusCode )
83- {
84- var error = JsonConvert . DeserializeObject < JObject > ( resultString , this . jsonSettings ) ;
85- throw new FacturapiException ( error [ "message" ] . ToString ( ) ) ;
86- }
87-
88- var searchResult = JsonConvert . DeserializeObject < SearchResult < CatalogItem > > ( resultString , this . jsonSettings ) ;
89- return searchResult ;
37+ return await this . SearchCatalogAsync ( Router . SearchCartaporteRightsOfPassage ( query ) ) ;
9038 }
9139
9240 public async Task < SearchResult < CatalogItem > > SearchCustomsDocuments ( Dictionary < string , object > query = null )
9341 {
94- var response = await client . GetAsync ( Router . SearchCartaporteCustomsDocuments ( query ) ) ;
95- var resultString = await response . Content . ReadAsStringAsync ( ) ;
96-
97- if ( ! response . IsSuccessStatusCode )
98- {
99- var error = JsonConvert . DeserializeObject < JObject > ( resultString , this . jsonSettings ) ;
100- throw new FacturapiException ( error [ "message" ] . ToString ( ) ) ;
101- }
102-
103- var searchResult = JsonConvert . DeserializeObject < SearchResult < CatalogItem > > ( resultString , this . jsonSettings ) ;
104- return searchResult ;
42+ return await this . SearchCatalogAsync ( Router . SearchCartaporteCustomsDocuments ( query ) ) ;
10543 }
10644
10745 public async Task < SearchResult < CatalogItem > > SearchPackagingTypes ( Dictionary < string , object > query = null )
10846 {
109- var response = await client . GetAsync ( Router . SearchCartaportePackagingTypes ( query ) ) ;
110- var resultString = await response . Content . ReadAsStringAsync ( ) ;
111-
112- if ( ! response . IsSuccessStatusCode )
113- {
114- var error = JsonConvert . DeserializeObject < JObject > ( resultString , this . jsonSettings ) ;
115- throw new FacturapiException ( error [ "message" ] . ToString ( ) ) ;
116- }
117-
118- var searchResult = JsonConvert . DeserializeObject < SearchResult < CatalogItem > > ( resultString , this . jsonSettings ) ;
119- return searchResult ;
47+ return await this . SearchCatalogAsync ( Router . SearchCartaportePackagingTypes ( query ) ) ;
12048 }
12149
12250 public async Task < SearchResult < CatalogItem > > SearchTrailerTypes ( Dictionary < string , object > query = null )
12351 {
124- var response = await client . GetAsync ( Router . SearchCartaporteTrailerTypes ( query ) ) ;
125- var resultString = await response . Content . ReadAsStringAsync ( ) ;
126-
127- if ( ! response . IsSuccessStatusCode )
128- {
129- var error = JsonConvert . DeserializeObject < JObject > ( resultString , this . jsonSettings ) ;
130- throw new FacturapiException ( error [ "message" ] . ToString ( ) ) ;
131- }
132-
133- var searchResult = JsonConvert . DeserializeObject < SearchResult < CatalogItem > > ( resultString , this . jsonSettings ) ;
134- return searchResult ;
52+ return await this . SearchCatalogAsync ( Router . SearchCartaporteTrailerTypes ( query ) ) ;
13553 }
13654
13755 public async Task < SearchResult < CatalogItem > > SearchHazardousMaterials ( Dictionary < string , object > query = null )
13856 {
139- var response = await client . GetAsync ( Router . SearchCartaporteHazardousMaterials ( query ) ) ;
140- var resultString = await response . Content . ReadAsStringAsync ( ) ;
141-
142- if ( ! response . IsSuccessStatusCode )
143- {
144- var error = JsonConvert . DeserializeObject < JObject > ( resultString , this . jsonSettings ) ;
145- throw new FacturapiException ( error [ "message" ] . ToString ( ) ) ;
146- }
147-
148- var searchResult = JsonConvert . DeserializeObject < SearchResult < CatalogItem > > ( resultString , this . jsonSettings ) ;
149- return searchResult ;
57+ return await this . SearchCatalogAsync ( Router . SearchCartaporteHazardousMaterials ( query ) ) ;
15058 }
15159
15260 public async Task < SearchResult < CatalogItem > > SearchNavalAuthorizations ( Dictionary < string , object > query = null )
15361 {
154- var response = await client . GetAsync ( Router . SearchCartaporteNavalAuthorizations ( query ) ) ;
155- var resultString = await response . Content . ReadAsStringAsync ( ) ;
156-
157- if ( ! response . IsSuccessStatusCode )
158- {
159- var error = JsonConvert . DeserializeObject < JObject > ( resultString , this . jsonSettings ) ;
160- throw new FacturapiException ( error [ "message" ] . ToString ( ) ) ;
161- }
162-
163- var searchResult = JsonConvert . DeserializeObject < SearchResult < CatalogItem > > ( resultString , this . jsonSettings ) ;
164- return searchResult ;
62+ return await this . SearchCatalogAsync ( Router . SearchCartaporteNavalAuthorizations ( query ) ) ;
16563 }
16664
16765 public async Task < SearchResult < CatalogItem > > SearchPortStations ( Dictionary < string , object > query = null )
16866 {
169- var response = await client . GetAsync ( Router . SearchCartaportePortStations ( query ) ) ;
170- var resultString = await response . Content . ReadAsStringAsync ( ) ;
171-
172- if ( ! response . IsSuccessStatusCode )
173- {
174- var error = JsonConvert . DeserializeObject < JObject > ( resultString , this . jsonSettings ) ;
175- throw new FacturapiException ( error [ "message" ] . ToString ( ) ) ;
176- }
177-
178- var searchResult = JsonConvert . DeserializeObject < SearchResult < CatalogItem > > ( resultString , this . jsonSettings ) ;
179- return searchResult ;
67+ return await this . SearchCatalogAsync ( Router . SearchCartaportePortStations ( query ) ) ;
18068 }
18169
18270 public async Task < SearchResult < CatalogItem > > SearchMarineContainers ( Dictionary < string , object > query = null )
18371 {
184- var response = await client . GetAsync ( Router . SearchCartaporteMarineContainers ( query ) ) ;
185- var resultString = await response . Content . ReadAsStringAsync ( ) ;
72+ return await this . SearchCatalogAsync ( Router . SearchCartaporteMarineContainers ( query ) ) ;
73+ }
18674
187- if ( ! response . IsSuccessStatusCode )
75+ private async Task < SearchResult < CatalogItem > > SearchCatalogAsync ( string url )
76+ {
77+ using ( var response = await client . GetAsync ( url ) )
18878 {
189- var error = JsonConvert . DeserializeObject < JObject > ( resultString , this . jsonSettings ) ;
190- throw new FacturapiException ( error [ "message" ] . ToString ( ) ) ;
79+ await this . ThrowIfErrorAsync ( response ) ;
80+ var resultString = await response . Content . ReadAsStringAsync ( ) ;
81+ var searchResult = JsonConvert . DeserializeObject < SearchResult < CatalogItem > > ( resultString , this . jsonSettings ) ;
82+ return searchResult ;
19183 }
192-
193- var searchResult = JsonConvert . DeserializeObject < SearchResult < CatalogItem > > ( resultString , this . jsonSettings ) ;
194- return searchResult ;
19584 }
19685 }
19786}
0 commit comments