Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintain get list of all ContactLists, create new ContactList and delete ContactList #67

Merged
merged 18 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Core/HubSpotAction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Skarp.HubSpotClient.Core
namespace Skarp.HubSpotClient.Core
{
/// <summary>
/// Enumerates the possible actions against the hubspot api
Expand All @@ -25,6 +25,8 @@ public enum HubSpotAction

UpdateBatch,

ReadBatch
ReadBatch,

Lists
}
}
}
69 changes: 38 additions & 31 deletions src/Core/Requests/RequestDataConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,48 +22,55 @@ public RequestDataConverter(
/// Converts the given <paramref name="entity"/> to a hubspot data entity.
/// </summary>
/// <param name="entity">The entity.</param>
/// <param name="preformConversion">This by default performs a data conversion so that the HubSpot "property-value" syntax will be used for serialization. If this parameter is set to false no data conversion is performed (such that a standard object serialization can be performed). </param>
/// <returns></returns>
public dynamic ToHubspotDataEntity(IHubSpotEntity entity)
public dynamic ToHubspotDataEntity(IHubSpotEntity entity, bool preformConversion = true)
{
_logger.LogDebug("Convert ToHubspotDataEntity");
dynamic mapped = new ExpandoObject();

mapped.Properties = new List<HubspotDataEntityProp>();

_logger.LogDebug("Use nameValue mapping?: {0}", entity.IsNameValue);

var allProps = entity.GetType().GetProperties();
_logger.LogDebug("Have {0} props to map", allProps.Length);

foreach (var prop in allProps)
if (preformConversion)
nover marked this conversation as resolved.
Show resolved Hide resolved
{
if (prop.HasIgnoreDataMemberAttribute()) { continue; }
mapped.Properties = new List<HubspotDataEntityProp>();

var propSerializedName = prop.GetPropSerializedName();
_logger.LogDebug("Mapping prop: '{0}' with serialization name: '{1}'", prop.Name, propSerializedName);
if (prop.Name.Equals("RouteBasePath") || prop.Name.Equals("IsNameValue")) { continue; }
_logger.LogDebug("Use nameValue mapping?: {0}", entity.IsNameValue);

// IF we have an complex type on the entity that we are trying to convert, let's NOT get the
// string value of it, but simply pass the object along - it will be serialized later as JSON...
var propValue = prop.GetValue(entity);
var value = propValue.IsComplexType() ? propValue : propValue?.ToString();
var item = new HubspotDataEntityProp
{
Property = propSerializedName,
Value = value
};
var allProps = entity.GetType().GetProperties();
_logger.LogDebug("Have {0} props to map", allProps.Length);

if (entity.IsNameValue)
foreach (var prop in allProps)
{
item.Property = null;
item.Name = propSerializedName;
if (prop.HasIgnoreDataMemberAttribute()) { continue; }

var propSerializedName = prop.GetPropSerializedName();
_logger.LogDebug("Mapping prop: '{0}' with serialization name: '{1}'", prop.Name, propSerializedName);
if (prop.Name.Equals("RouteBasePath") || prop.Name.Equals("IsNameValue")) { continue; }

// IF we have an complex type on the entity that we are trying to convert, let's NOT get the
// string value of it, but simply pass the object along - it will be serialized later as JSON...
var propValue = prop.GetValue(entity);
var value = propValue.IsComplexType() ? propValue : propValue?.ToString();
var item = new HubspotDataEntityProp
{
Property = propSerializedName,
Value = value
};

if (entity.IsNameValue)
{
item.Property = null;
item.Name = propSerializedName;
}
if (item.Value == null) { continue; }

mapped.Properties.Add(item);
}
if (item.Value == null) { continue; }

mapped.Properties.Add(item);
_logger.LogDebug("Mapping complete, returning data");
}
else
{
mapped = entity;
}

_logger.LogDebug("Mapping complete, returning data");

return mapped;
}
Expand Down Expand Up @@ -282,7 +289,7 @@ 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);

targetProp?.SetValue(dto, dynamicValue.GetType() == targetProp.PropertyType? dynamicValue: Convert.ChangeType(dynamicValue, targetProp.PropertyType));
targetProp?.SetValue(dto, dynamicValue.GetType() == targetProp.PropertyType ? dynamicValue : Convert.ChangeType(dynamicValue, targetProp.PropertyType));
}
return dto;
}
Expand Down
13 changes: 12 additions & 1 deletion src/Core/Requests/RequestSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,21 @@ public RequestSerializer(
/// <param name="entity">The entity.</param>
/// <returns>The serialized entity</returns>
public virtual string SerializeEntity(object obj)
{
return SerializeEntity(obj, true);
}

/// <summary>
/// Serializes the entity to JSON.
/// </summary>
/// <param name="entity">The entity.</param>
/// <param name="preformConversion">This by defines whether or not a data conversion is performed on the object so that the HubSpot "property-value" syntax will be used for serialization. If this parameter is set to false no data conversion is performed and a standard object serialization is performed. </param>
/// <returns>The serialized entity</returns>
public virtual string SerializeEntity(object obj, bool performConversion)
nover marked this conversation as resolved.
Show resolved Hide resolved
{
if (obj is IHubSpotEntity entity)
{
var converted = _requestDataConverter.ToHubspotDataEntity(entity);
var converted = _requestDataConverter.ToHubspotDataEntity(entity, performConversion);

entity.ToHubSpotDataEntity(ref converted);

Expand Down
20 changes: 20 additions & 0 deletions src/ListOfContacts/Dto/ContactListFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Skarp.HubSpotClient.ListOfContacts.Interfaces;
using System.Runtime.Serialization;

namespace Skarp.HubSpotClient.ListOfContacts.Dto
{

[DataContract(Name = "filter")]
public class ContactListFilter : IContactListFilter
{
[DataMember(Name = "type")]
public string Type { get; set; }
[DataMember(Name = "property")]
public string Property { get; set; }
[DataMember(Name = "value")]
public string Value { get; set; }
[DataMember(Name = "operator")]
public string op { get; set; }
}

}
19 changes: 19 additions & 0 deletions src/ListOfContacts/Dto/ContactListMetaData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Runtime.Serialization;

namespace Skarp.HubSpotClient.ListOfContacts.Dto
{
[DataContract(Name = "MetaData")]
public class ContactListMetaData
{
[DataMember(Name = "processing")]
public string Processing { get; set; }
[DataMember(Name = "size")]
public int Size { get; set; }
[DataMember(Name = "error")]
public string Error { get; set; }
[DataMember(Name = "lastProcessingStateChangeAt")]
public long LastProcessingStateChangeAtTimeStamp { get; set; }
[DataMember(Name = "lastSizeChangeAt")]
public long LastSizeChangeAtTimeStamp { get; set; }
}
}
40 changes: 40 additions & 0 deletions src/ListOfContacts/Dto/CreateContactListRequestHubSpotEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Newtonsoft.Json;
using Skarp.HubSpotClient.Core.Interfaces;
using Skarp.HubSpotClient.ListOfContacts.Interfaces;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;

namespace Skarp.HubSpotClient.ListOfContacts.Dto
{
[DataContract]
public class CreateContactListRequestHubSpotEntity : ICreateContactListRequestHubSpotEntity
{
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "dynamic")]
public bool? Dynamic { get; set; }
[DataMember(Name = "portalId")]
public int? PortalId { get; set; }
[DataMember(Name = "filters")]
public List<List<IContactListFilter>> Filters { get; set; }

public int offset { get; set; }

[DataMember(Name = "has-more")]
public bool HasMore { get; set; }

public bool IsNameValue => false;

public virtual void ToHubSpotDataEntity(ref dynamic dataEntity)
{

}

public virtual void FromHubSpotDataEntity(dynamic hubspotData)
{

}
}
}
56 changes: 56 additions & 0 deletions src/ListOfContacts/Dto/CreateContactListResponseHubSpotEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Newtonsoft.Json;
using Skarp.HubSpotClient.Core.Interfaces;
using Skarp.HubSpotClient.ListOfContacts.Interfaces;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;

namespace Skarp.HubSpotClient.ListOfContacts.Dto
{
[DataContract]
public class CreateContactListResponseHubSpotEntity: IHubSpotEntity
{
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "internalListId")]
public int InternalListId { get; set; }
[DataMember(Name = "listId")]
public int ListId { get; set; }
[DataMember(Name = "deleted")]
public bool Deleted { get; set; }
[DataMember(Name = "dynamic")]
public bool Dynamic { get; set; }
[DataMember(Name = "portalId")]
public int PortalId { get; set; }
[DataMember(Name = "createdAt")]
public string CreatedAtTimeStamp { get; set; }
[DataMember(Name = "updatedAt")]
public string UpdatedAtTimeStamp { get; set; }
[DataMember(Name = "metaData")]
public ContactListMetaData MetaData { get; set; }
[DataMember(Name = "filters")]
public List<List<ContactListFilter>> Filters { get; set; }
[DataMember(Name = "listType")]
public string ListType { get; set; }
[DataMember(Name = "archived")]
public bool Archived { get; set; }

public int offset { get; set; }

[DataMember(Name = "has-more")]
public bool HasMore { get; set; }

public bool IsNameValue => false;

public virtual void ToHubSpotDataEntity(ref dynamic dataEntity)
{

}

public virtual void FromHubSpotDataEntity(dynamic hubspotData)
{

}
}
}
92 changes: 92 additions & 0 deletions src/ListOfContacts/Dto/ListOfContactListsHubSpotEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Newtonsoft.Json;
using Skarp.HubSpotClient.Core.Interfaces;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;

namespace Skarp.HubSpotClient.ListOfContacts.Dto
{
public class ListOfContactListsHubSpotEntity: IHubSpotEntity
{
[DataContract(Name="MetaData")]
public class ContactListsMetaData
{
[DataMember(Name="processing")]
public string Processing { get; set; }
[DataMember(Name="size")]
public int Size { get; set; }
[DataMember(Name="error")]
public string Error { get; set; }
[DataMember(Name="lastProcessingStateChangeAt")]
public long LastProcessingStateChangeAtTimeStamp { get; set; }
[DataMember(Name="lastSizeChangeAt")]
public long LastSizeChangeAtTimeStamp { get; set; }
}

[DataContract(Name="List")]
public class ContactListsItem
{
[DataMember(Name="dynamic")]
public bool Dynamic { get; set; }
[DataMember(Name="metaData")]
public ContactListsMetaData MetaData { get; set; }
[DataMember(Name="name")]
public string Name { get; set; }
[DataMember(Name="filters")]
public List<List<ContactListsFilter>> Filters { get; set; }
[DataMember(Name="portalId")]
public int PortalId { get; set; }
[DataMember(Name="createdAt")]
public long CreatedAtTimeStamp { get; set; }
[DataMember(Name="listId")]
public int ListId { get; set; }
[DataMember(Name="updatedAt")]
public long UpdatedAtTimeStamp { get; set; }
[DataMember(Name="listType")]
public string ListType { get; set; }
[DataMember(Name="internalListId")]
public int InternalListId { get; set; }
[DataMember(Name="deleteable")]
public bool Deleteable { get; set; }
}

[DataContract(Name="filter")]
public class ContactListsFilter
{
[DataMember(Name="filterFamily")]
public string FilterFamily { get; set; }
[DataMember(Name="withinTimeMode")]
public string WithinTimeMode { get; set; }
[DataMember(Name="checkPastVersions")]
public bool CheckPastVersions { get; set; }
[DataMember(Name="type")]
public string Type { get; set; }
[DataMember(Name="property")]
public string Property { get; set; }
[DataMember(Name="value")]
public string Value { get; set; }
[DataMember(Name = "operator")]
public string op { get; set; }
}


public List<ContactListsItem> lists { get; set; }
public int offset { get; set; }

[DataMember(Name = "has-more")]
public bool HasMore { get; set; }

public bool IsNameValue => false;

public virtual void ToHubSpotDataEntity(ref dynamic dataEntity)
{

}

public virtual void FromHubSpotDataEntity(dynamic hubspotData)
{

}
}
}
Loading