Skip to content

Commit

Permalink
fix: handle timestamp conversion to datetime(offset) [#79]
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeStevens318 committed May 20, 2021
1 parent fb75e30 commit 22a014a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
22 changes: 19 additions & 3 deletions src/Core/Requests/RequestDataConverter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -289,9 +290,24 @@ internal object ConvertSingleEntity(ExpandoObject dynamicObject, object dto)
_logger.LogDebug("Have target prop? '{0}' with name: '{1}' and actual value: '{2}'", targetProp != null,
dynamicProp.Key, dynamicValue);

// skip any nullable properties with a empty value
if (targetProp != null && Nullable.GetUnderlyingType(targetProp.PropertyType) != null && string.IsNullOrEmpty(dynamicValue.ToString()))
continue;
if (targetProp != null)
{
// property type
var pt = targetProp.PropertyType;
var val = dynamicValue.ToString();

// skip any nullable properties with a empty value
if (Nullable.GetUnderlyingType(pt) != null && string.IsNullOrEmpty(val))
continue;

// convert to DateTime/DateTime?
if ((pt == typeof(DateTime) || pt == typeof(DateTime?)) && long.TryParse(val, out var milliseconds))
dynamicValue = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(milliseconds);

// convert to DateTimeOffset/DateTimeOffset?
if ((pt == typeof(DateTimeOffset) || pt == typeof(DateTimeOffset?)) && long.TryParse(val, out milliseconds))
dynamicValue = DateTimeOffset.FromUnixTimeMilliseconds(milliseconds);
}

targetProp?.SetValue(dto, dynamicValue.GetType() == targetProp.PropertyType
? dynamicValue
Expand Down
17 changes: 17 additions & 0 deletions test/integration/Contact/Dto/ContactHubSpotEntityExtended.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Runtime.Serialization;
using Skarp.HubSpotClient.Contact.Dto;

namespace integration.Contact.Dto
{
/// <summary>
/// Extended HubSpot Contact Entity
/// </summary>
/// <remarks>Used to test functionality of DateTime/DateTime?/DateTimeOffset/DateTimeOffset?</remarks>
[DataContract]
public class ContactHubSpotEntityExtended : ContactHubSpotEntity
{
[DataMember(Name = "lastmodifieddate")]
public DateTimeOffset? LastModified { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using integration.Contact.Dto;
using Microsoft.Extensions.Logging;
using RapidCore.Network;
using Skarp.HubSpotClient.Contact;
Expand Down Expand Up @@ -73,9 +76,10 @@ public async Task Create_contact_and_get_works()
public async Task List_contacts_works()
{
var contacts =
await _client.ListAsync<ContactListHubSpotEntity<ContactHubSpotEntity>>(new ContactListRequestOptions
await _client.ListAsync<ContactListHubSpotEntity<ContactHubSpotEntityExtended>>(new ContactListRequestOptions
{
NumberOfContactsToReturn = 5
NumberOfContactsToReturn = 5,
PropertiesToInclude = new List<string> { "lastmodifieddate" }
});

Assert.NotNull(contacts);
Expand Down

0 comments on commit 22a014a

Please sign in to comment.