@@ -47,11 +47,54 @@ public async Task<ApiResponse<T>> PatchAsync<T>(string endpoint, object payload)
4747 public async Task < ApiResponse < bool > > DeleteAsync ( string endpoint )
4848 => await SendRequestAsync < bool > ( HttpMethod . Delete , endpoint ) ;
4949
50- private async Task < ApiResponse < T > > SendRequestAsync < T > (
51- HttpMethod method ,
52- string endpoint ,
53- object content = null
54- )
50+ //private async Task<ApiResponse<T>> SendRequestAsync<T>(
51+ // HttpMethod method,
52+ // string endpoint,
53+ // object content = null
54+ //)
55+ //{
56+ // var request = new HttpRequestMessage(method, endpoint);
57+
58+ // if (content != null)
59+ // {
60+ // var json = JsonConvert.SerializeObject(content, _jsonSettings);
61+ // request.Content = new StringContent(json, Encoding.UTF8, "application/json");
62+ // }
63+
64+ // var response = await _httpClient.SendAsync(request);
65+ // var responseContent = await response.Content.ReadAsStringAsync();
66+
67+ // return response.IsSuccessStatusCode
68+ // ? JsonConvert.DeserializeObject<ApiResponse<T>>(responseContent, _jsonSettings)
69+ // : HandleFailureAsync<T>(responseContent);
70+ //}
71+
72+ //private ApiResponse<T> HandleFailureAsync<T>(string responseContent)
73+ //{
74+ // var failureResponse =
75+ // JsonConvert.DeserializeObject<ApiResponse<List<ValidationFailure>>>(responseContent, _jsonSettings);
76+
77+ // var failures = failureResponse.Data;
78+
79+ // var friendlyErrorMessage = "";
80+ // if (failures != null && failures.Count > 0)
81+ // {
82+ // friendlyErrorMessage = string.Join("; ",
83+ // failures.Select(x => $"{x.PropertyName}: {x.ErrorMessage}"));
84+ // }
85+
86+ // return new ApiResponse<T>
87+ // {
88+ // Succeeded = false,
89+ // HttpStatusCode = failureResponse.HttpStatusCode,
90+ // Message = failureResponse.Message,
91+ // Details = !string.IsNullOrEmpty(friendlyErrorMessage)
92+ // ? friendlyErrorMessage
93+ // : failureResponse.Details,
94+ // Data = default
95+ // };
96+ //}
97+ public async Task < ApiResponse < T > > SendRequestAsync < T > ( HttpMethod method , string endpoint , object content = null )
5598 {
5699 var request = new HttpRequestMessage ( method , endpoint ) ;
57100
@@ -66,33 +109,58 @@ private async Task<ApiResponse<T>> SendRequestAsync<T>(
66109
67110 return response . IsSuccessStatusCode
68111 ? JsonConvert . DeserializeObject < ApiResponse < T > > ( responseContent , _jsonSettings )
69- : HandleFailureAsync < T > ( responseContent ) ;
112+ : HandleFailureResponse < T > ( responseContent , ( int ) response . StatusCode ) ;
70113 }
71114
72- private ApiResponse < T > HandleFailureAsync < T > ( string responseContent )
115+ private ApiResponse < T > HandleFailureResponse < T > ( string responseContent , int statusCode )
73116 {
74- var failureResponse =
75- JsonConvert . DeserializeObject < ApiResponse < List < ValidationFailure > > > ( responseContent , _jsonSettings ) ;
76-
77- var failures = failureResponse . Data ;
78-
79- var friendlyErrorMessage = "" ;
80- if ( failures != null && failures . Count > 0 )
117+ try
81118 {
82- friendlyErrorMessage = string . Join ( "; " ,
83- failures . Select ( x => $ "{ x . PropertyName } : { x . ErrorMessage } ") ) ;
119+ // First try to deserialize as a generic response
120+ var failureResponse = JsonConvert . DeserializeObject < ApiResponse < object > > ( responseContent , _jsonSettings ) ;
121+
122+ // If status is 400, try to deserialize ValidationFailures
123+ if ( statusCode == 400 )
124+ {
125+ var validationResponse = JsonConvert . DeserializeObject < ApiResponse < List < ValidationFailure > > > ( responseContent , _jsonSettings ) ;
126+ var failures = validationResponse ? . Data ;
127+
128+ var validationErrors = failures != null && failures . Count > 0
129+ ? string . Join ( "; " , failures . Select ( x => $ "{ x . PropertyName } : { x . ErrorMessage } ") )
130+ : null ;
131+
132+ return new ApiResponse < T >
133+ {
134+ Succeeded = false ,
135+ HttpStatusCode = statusCode ,
136+ Message = validationResponse ? . Message ,
137+ Details = validationErrors ?? validationResponse ? . Details ,
138+ Data = default
139+ } ;
140+ }
141+
142+ // For other error codes (401, 403, 404, 500, etc.)
143+ return new ApiResponse < T >
144+ {
145+ Succeeded = false ,
146+ HttpStatusCode = statusCode ,
147+ Message = failureResponse ? . Message ,
148+ Details = failureResponse ? . Details ,
149+ Data = default
150+ } ;
84151 }
85-
86- return new ApiResponse < T >
152+ catch ( JsonException )
87153 {
88- Succeeded = false ,
89- HttpStatusCode = failureResponse . HttpStatusCode ,
90- Message = failureResponse . Message ,
91- Details = ! string . IsNullOrEmpty ( friendlyErrorMessage )
92- ? friendlyErrorMessage
93- : failureResponse . Details ,
94- Data = default
95- } ;
154+ // If we can't deserialize the response, create a generic error response
155+ return new ApiResponse < T >
156+ {
157+ Succeeded = false ,
158+ HttpStatusCode = statusCode ,
159+ Message = "Error processing server response" ,
160+ Details = responseContent ,
161+ Data = default
162+ } ;
163+ }
96164 }
97165 }
98166}
0 commit comments