1- using Microsoft . AspNetCore . Components . WebAssembly . Http ;
1+ using CodeBeam . UltimateAuth . Core . Contracts ;
2+ using Microsoft . AspNetCore . Components . WebAssembly . Http ;
23using System . Net . Http . Json ;
34
45namespace CodeBeam . UltimateAuth . Sample . BlazorStandaloneWasm . ResourceApi ;
@@ -25,56 +26,53 @@ private HttpRequestMessage CreateRequest(HttpMethod method, string url, object?
2526 return request ;
2627 }
2728
28- public async Task < List < Product > > GetAllAsync ( )
29- {
30- var request = CreateRequest ( HttpMethod . Get , "/api/products" ) ;
31- var response = await _http . SendAsync ( request ) ;
29+ public Task < UAuthResult < List < SampleProduct > > > GetAllAsync ( )
30+ => SendAsync < List < SampleProduct > > ( CreateRequest ( HttpMethod . Get , "/api/products" ) ) ;
3231
33- if ( ! response . IsSuccessStatusCode )
34- throw new Exception ( $ "Failed to fetch products: { response . StatusCode } " ) ;
32+ public Task < UAuthResult < SampleProduct ? > > GetAsync ( int id )
33+ => SendAsync < SampleProduct ? > ( CreateRequest ( HttpMethod . Get , $ "/api/ products/ { id } " ) ) ;
3534
36- return await response . Content . ReadFromJsonAsync < List < Product > > ( ) ?? new ( ) ;
37- }
35+ public Task < UAuthResult < SampleProduct ? > > CreateAsync ( SampleProduct product )
36+ => SendAsync < SampleProduct ? > ( CreateRequest ( HttpMethod . Post , $ "/api/products" , product ) ) ;
3837
39- public async Task < Product ? > GetAsync ( int id )
40- {
41- var request = CreateRequest ( HttpMethod . Get , $ "/api/products/{ id } ") ;
42- var response = await _http . SendAsync ( request ) ;
38+ public Task < UAuthResult < SampleProduct ? > > UpdateAsync ( int id , SampleProduct product )
39+ => SendAsync < SampleProduct ? > ( CreateRequest ( HttpMethod . Put , $ "/api/products/{ id } ", product ) ) ;
4340
44- if ( ! response . IsSuccessStatusCode )
45- return null ;
41+ public Task < UAuthResult < SampleProduct ? > > DeleteAsync ( int id )
42+ => SendAsync < SampleProduct ? > ( CreateRequest ( HttpMethod . Delete , $ "/api/products/ { id } " ) ) ;
4643
47- return await response . Content . ReadFromJsonAsync < Product > ( ) ;
48- }
49-
50- public async Task < Product ? > CreateAsync ( Product product )
44+ private async Task < UAuthResult < T > > SendAsync < T > ( HttpRequestMessage request )
5145 {
52- var request = CreateRequest ( HttpMethod . Post , "/api/products" , product ) ;
5346 var response = await _http . SendAsync ( request ) ;
5447
55- if ( ! response . IsSuccessStatusCode )
56- throw new Exception ( $ "Create failed: { response . StatusCode } ") ;
57-
58- return await response . Content . ReadFromJsonAsync < Product > ( ) ;
59- }
60-
61- public async Task < Product ? > UpdateAsync ( int id , Product product )
62- {
63- var request = CreateRequest ( HttpMethod . Put , $ "/api/products/{ id } ", product ) ;
64- var response = await _http . SendAsync ( request ) ;
48+ var result = new UAuthResult < T >
49+ {
50+ Status = ( int ) response . StatusCode ,
51+ IsSuccess = response . IsSuccessStatusCode
52+ } ;
6553
66- if ( ! response . IsSuccessStatusCode )
67- throw new Exception ( $ "Update failed: { response . StatusCode } ") ;
54+ if ( response . IsSuccessStatusCode )
55+ {
56+ result . Value = await response . Content . ReadFromJsonAsync < T > ( ) ;
57+ return result ;
58+ }
6859
69- return await response . Content . ReadFromJsonAsync < Product > ( ) ;
60+ result . Problem = await TryReadProblem ( response ) ;
61+ return result ;
7062 }
7163
72- public async Task DeleteAsync ( int id )
64+ private async Task < UAuthProblem ? > TryReadProblem ( HttpResponseMessage response )
7365 {
74- var request = CreateRequest ( HttpMethod . Delete , $ "/api/products/{ id } ") ;
75- var response = await _http . SendAsync ( request ) ;
76-
77- if ( ! response . IsSuccessStatusCode )
78- throw new Exception ( $ "Delete failed: { response . StatusCode } ") ;
66+ try
67+ {
68+ return await response . Content . ReadFromJsonAsync < UAuthProblem > ( ) ;
69+ }
70+ catch
71+ {
72+ return new UAuthProblem
73+ {
74+ Title = response . ReasonPhrase
75+ } ;
76+ }
7977 }
8078}
0 commit comments