Skip to content

Commit

Permalink
Added get list of contact lists (#63)
Browse files Browse the repository at this point in the history
Add  HubSpotListOfContactsClient.cs
  • Loading branch information
davymirfin committed Jan 14, 2021
1 parent 6f2835a commit 016fda1
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 3 deletions.
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
}
}
}
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)
{

}
}
}
24 changes: 24 additions & 0 deletions src/ListOfContacts/HubSpotListOfContactsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,28 @@ public async Task<T> GetListByIdAsync<T>(long listId, ListOfContactsRequestOptio
return data;
}

/// <summary>
/// Return a list of contact lists from hubspot
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public async Task<T> GetListAsync<T>(ListOfContactListsRequestOptions opts = null)
{
Logger.LogDebug("Get list of contact lists");
if (opts == null)
{
opts = new ListOfContactListsRequestOptions();
}
var path = PathResolver(new ContactHubSpotEntity(), HubSpotAction.Lists)
.SetQueryParam("count", opts.NumberOfContactListsToReturn);
if (opts.ContactListOffset.HasValue)
{
path = path.SetQueryParam("vidOffset", opts.ContactListOffset);
}
var data = await GetGenericAsync<T>(path);
return data;
}

/// <summary>
/// Add list of contacts based on list id
/// </summary>
Expand Down Expand Up @@ -116,6 +138,8 @@ public string PathResolver(Contact.Interfaces.IContactHubSpotEntity entity, HubS
{
case HubSpotAction.Get:
return $"{entity.RouteBasePath}/lists/:listId:/contacts/all";
case HubSpotAction.Lists:
return $"{entity.RouteBasePath}/lists";
case HubSpotAction.CreateBatch:
return $"{entity.RouteBasePath}/lists/:listId:/add";
case HubSpotAction.DeleteBatch:
Expand Down
41 changes: 41 additions & 0 deletions src/ListOfContacts/ListOfContactListsRequestOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;

namespace Skarp.HubSpotClient.ListOfContacts
{
public class ListOfContactListsRequestOptions
{
private int _numberOfContactListsToReturn = 100;

/// <summary>
/// Gets or sets the number of contact lists to return.
/// </summary>
/// <remarks>
/// Defaults to 20 which is also the hubspot api default. Max value is 100
/// </remarks>
/// <value>
/// The number of contacts to return.
/// </value>
public int NumberOfContactListsToReturn
{
get => _numberOfContactListsToReturn;
set
{
if (value < 1 || value > 250)
{
throw new ArgumentException(
$"Number of contacts to return must be a positive ingeteger greater than 0 and less than 251 - you provided {value}");
}
_numberOfContactListsToReturn = value;
}
}

/// <summary>
/// Get or set the continuation offset when calling list many times to enumerate all your contact lists
/// </summary>
/// <remarks>
/// The return DTO from List contains the current "offset" that you can inject into your next list call
/// to continue the listing process
/// </remarks>
public long? ContactListOffset { get; set; } = null;
}
}

0 comments on commit 016fda1

Please sign in to comment.