Skip to content

Commit f1a60a9

Browse files
author
Jani Giannoudis
committed
payroll http client: extended get record id to support json objects
updated version to 0.9.0-beta.14
1 parent 373e013 commit f1a60a9

5 files changed

Lines changed: 33 additions & 13 deletions

File tree

Client.Core/Exchange/ExchangeImport.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,9 @@ protected override async Task VisitPayrunJobInvocationAsync(IExchangeTenant tena
585585
// create new payrun job (POST only)
586586
using var response = await HttpClient.PostAsync(PayrunApiEndpoints.PayrunJobsUrl(tenant.Id),
587587
DefaultJsonSerializer.SerializeJson(invocation));
588-
var payrunJobId = PayrollHttpClient.GetRecordId(response);
588+
589+
// payrun job id
590+
var payrunJobId = await PayrollHttpClient.GetRecordIdAsync(response);
589591
if (payrunJobId <= 0)
590592
{
591593
throw new PayrollException($"Error while creating the payrun job {invocation.Name}.");

Client.Core/PayrollEngine.Client.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
</None>
9090
</ItemGroup>
9191
<ItemGroup>
92-
<PackageReference Include="PayrollEngine.Core" Version="0.9.0-beta.13" />
92+
<PackageReference Include="PayrollEngine.Core" Version="0.9.0-beta.14" />
9393
</ItemGroup>
9494

9595
<!-- build json schema -->

Client.Core/PayrollEngine.Client.Core.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8477,7 +8477,7 @@
84778477
<param name="address">The address to test</param>
84788478
<returns>True if internet connection is available</returns>
84798479
</member>
8480-
<member name="M:PayrollEngine.Client.PayrollHttpClient.GetRecordId(System.Net.Http.HttpResponseMessage)">
8480+
<member name="M:PayrollEngine.Client.PayrollHttpClient.GetRecordIdAsync(System.Net.Http.HttpResponseMessage)">
84818481
<summary>Get record id from the response (last uri segment)</summary>
84828482
<param name="response">The http response message</param>
84838483
<returns>The new record id</returns>

Client.Core/PayrollHttpClient.cs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Net.Http;
99
using System.Threading.Tasks;
1010
using System.Collections.Generic;
11+
using System.Text.Json;
1112
using Task = System.Threading.Tasks.Task;
1213
using PayrollEngine.Serialization;
1314

@@ -223,15 +224,32 @@ public async Task<bool> IsConnectionAvailableAsync(string address)
223224
/// <summary>Get record id from the response (last uri segment)</summary>
224225
/// <param name="response">The http response message</param>
225226
/// <returns>The new record id</returns>
226-
public static int GetRecordId(HttpResponseMessage response)
227+
public static async Task<int> GetRecordIdAsync(HttpResponseMessage response)
227228
{
228229
if (response == null)
229230
{
230231
throw new ArgumentNullException(nameof(response));
231232
}
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;
235253
}
236254

237255
/// <summary>Get backend resource response</summary>
@@ -299,7 +317,7 @@ public async Task<T> GetAsync<T>(string requestUri)
299317
{
300318
return default;
301319
}
302-
return string.IsNullOrWhiteSpace(json) ? default : DefaultJsonSerializer.Deserialize<T>(json);
320+
return DefaultJsonSerializer.Deserialize<T>(json);
303321
}
304322

305323
/// <summary>Get backend resource with request content</summary>
@@ -425,7 +443,7 @@ public async Task<T> PostAsync<T>(string requestUri)
425443

426444
if (responseObj is IModel model && model.Id == 0)
427445
{
428-
model.Id = GetRecordId(response);
446+
model.Id = await GetRecordIdAsync(response);
429447
}
430448
LogPost(requestUri, responseObj);
431449
return responseObj;
@@ -465,7 +483,7 @@ public async Task<T> PostAsync<T>(string requestUri, T content) where T : class
465483

466484
if (responseObj is IModel model && model.Id == 0)
467485
{
468-
model.Id = GetRecordId(response);
486+
model.Id = await GetRecordIdAsync(response);
469487
}
470488
LogPost(requestUri, responseObj);
471489
return responseObj;
@@ -505,7 +523,7 @@ public async Task<TOut> PostAsync<TIn, TOut>(string requestUri, TIn content)
505523

506524
if (responseObj is IModel model && model.Id == 0)
507525
{
508-
model.Id = GetRecordId(response);
526+
model.Id = await GetRecordIdAsync(response);
509527
}
510528
LogPost(requestUri, responseObj);
511529
return responseObj;
@@ -666,7 +684,7 @@ public async Task UpsertObjectAsync<T>(string requestUri, T newObject, T existin
666684

667685
using var response = await HttpClient.PostAsync(requestUri, DefaultJsonSerializer.SerializeJson(newObject));
668686
await EnsureSuccessResponse(response);
669-
newObject.Id = GetRecordId(response);
687+
newObject.Id = await GetRecordIdAsync(response);
670688
if (newObject.Id <= 0)
671689
{
672690
throw new PayrollException($"error while creating new record of object {newObject}.");

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net10.0</TargetFramework>
5-
<Version>0.9.0-beta.13</Version>
5+
<Version>0.9.0-beta.14</Version>
66
<FileVersion>0.9.0</FileVersion>
77
<InformationalVersion></InformationalVersion>
88
<Authors>Jani Giannoudis</Authors>

0 commit comments

Comments
 (0)