|
8 | 8 | using System.Net.Http; |
9 | 9 | using System.Threading.Tasks; |
10 | 10 | using System.Collections.Generic; |
| 11 | +using System.Text.Json; |
11 | 12 | using Task = System.Threading.Tasks.Task; |
12 | 13 | using PayrollEngine.Serialization; |
13 | 14 |
|
@@ -223,15 +224,32 @@ public async Task<bool> IsConnectionAvailableAsync(string address) |
223 | 224 | /// <summary>Get record id from the response (last uri segment)</summary> |
224 | 225 | /// <param name="response">The http response message</param> |
225 | 226 | /// <returns>The new record id</returns> |
226 | | - public static int GetRecordId(HttpResponseMessage response) |
| 227 | + public static async Task<int> GetRecordIdAsync(HttpResponseMessage response) |
227 | 228 | { |
228 | 229 | if (response == null) |
229 | 230 | { |
230 | 231 | throw new ArgumentNullException(nameof(response)); |
231 | 232 | } |
232 | | - return response.Headers.Location == null ? |
233 | | - 0 : |
234 | | - response.Headers.Location.GetLastSegmentId(); |
| 233 | + |
| 234 | + // location header |
| 235 | + if (response.Headers.Location != null && |
| 236 | + response.Headers.Location.TryGetLastSegmentId(out var id)) |
| 237 | + { |
| 238 | + return id; |
| 239 | + } |
| 240 | + |
| 241 | + // created object |
| 242 | + var json = await response.Content.ReadAsStringAsync(); |
| 243 | + if (!string.IsNullOrWhiteSpace(json)) |
| 244 | + { |
| 245 | + var obj = DefaultJsonSerializer.Deserialize<Dictionary<string, object>>(json); |
| 246 | + if (obj.TryGetValue("id", out var objId) && objId is JsonElement jsonValue) |
| 247 | + { |
| 248 | + return jsonValue.GetInt32(); |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + return 0; |
235 | 253 | } |
236 | 254 |
|
237 | 255 | /// <summary>Get backend resource response</summary> |
@@ -299,7 +317,7 @@ public async Task<T> GetAsync<T>(string requestUri) |
299 | 317 | { |
300 | 318 | return default; |
301 | 319 | } |
302 | | - return string.IsNullOrWhiteSpace(json) ? default : DefaultJsonSerializer.Deserialize<T>(json); |
| 320 | + return DefaultJsonSerializer.Deserialize<T>(json); |
303 | 321 | } |
304 | 322 |
|
305 | 323 | /// <summary>Get backend resource with request content</summary> |
@@ -425,7 +443,7 @@ public async Task<T> PostAsync<T>(string requestUri) |
425 | 443 |
|
426 | 444 | if (responseObj is IModel model && model.Id == 0) |
427 | 445 | { |
428 | | - model.Id = GetRecordId(response); |
| 446 | + model.Id = await GetRecordIdAsync(response); |
429 | 447 | } |
430 | 448 | LogPost(requestUri, responseObj); |
431 | 449 | return responseObj; |
@@ -465,7 +483,7 @@ public async Task<T> PostAsync<T>(string requestUri, T content) where T : class |
465 | 483 |
|
466 | 484 | if (responseObj is IModel model && model.Id == 0) |
467 | 485 | { |
468 | | - model.Id = GetRecordId(response); |
| 486 | + model.Id = await GetRecordIdAsync(response); |
469 | 487 | } |
470 | 488 | LogPost(requestUri, responseObj); |
471 | 489 | return responseObj; |
@@ -505,7 +523,7 @@ public async Task<TOut> PostAsync<TIn, TOut>(string requestUri, TIn content) |
505 | 523 |
|
506 | 524 | if (responseObj is IModel model && model.Id == 0) |
507 | 525 | { |
508 | | - model.Id = GetRecordId(response); |
| 526 | + model.Id = await GetRecordIdAsync(response); |
509 | 527 | } |
510 | 528 | LogPost(requestUri, responseObj); |
511 | 529 | return responseObj; |
@@ -666,7 +684,7 @@ public async Task UpsertObjectAsync<T>(string requestUri, T newObject, T existin |
666 | 684 |
|
667 | 685 | using var response = await HttpClient.PostAsync(requestUri, DefaultJsonSerializer.SerializeJson(newObject)); |
668 | 686 | await EnsureSuccessResponse(response); |
669 | | - newObject.Id = GetRecordId(response); |
| 687 | + newObject.Id = await GetRecordIdAsync(response); |
670 | 688 | if (newObject.Id <= 0) |
671 | 689 | { |
672 | 690 | throw new PayrollException($"error while creating new record of object {newObject}."); |
|
0 commit comments