Skip to content

Commit

Permalink
Implement basic contact list API operations
Browse files Browse the repository at this point in the history
  • Loading branch information
lakesol committed Feb 17, 2022
1 parent 5ce9df5 commit 7930fe1
Show file tree
Hide file tree
Showing 8 changed files with 293 additions and 0 deletions.
35 changes: 35 additions & 0 deletions HubSpot.NET/Api/ContactList/Dto/ContactListModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Runtime.Serialization;
using HubSpot.NET.Core.Interfaces;

namespace HubSpot.NET.Api.ContactList.Dto
{
[DataContract]
public class ContactListModel: IHubSpotModel
{
[DataMember(Name = "listId")]
public long ListId { get; set; }

[DataMember(Name = "name")]
public string Name { get; set; }

[DataMember(Name = "listType")]
public string ListType { get; set; }

[DataMember(Name = "dynamic")]
public bool Dynamic { get; set; }

[IgnoreDataMember]
public bool IsNameValue => true;

public void ToHubSpotDataEntity(ref dynamic dataEntity)
{
}

public void FromHubSpotDataEntity(dynamic hubspotData)
{
}

[IgnoreDataMember]
public string RouteBasePath => "/contacts/v1/lists";
}
}
27 changes: 27 additions & 0 deletions HubSpot.NET/Api/ContactList/Dto/ContactListUpdateModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
using HubSpot.NET.Core.Interfaces;

namespace HubSpot.NET.Api.ContactList.Dto
{
[DataContract]
public class ContactListUpdateModel : IHubSpotModel
{
[DataMember(Name = "vids")]
public List<long> ContactIds = new List<long>();

[IgnoreDataMember]
public bool IsNameValue => false;

public void ToHubSpotDataEntity(ref dynamic dataEntity)
{
}

public void FromHubSpotDataEntity(dynamic hubspotData)
{
}

[IgnoreDataMember]
public string RouteBasePath => "/contacts/v1/lists";
}
}
33 changes: 33 additions & 0 deletions HubSpot.NET/Api/ContactList/Dto/ContactListUpdateResponseModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
using HubSpot.NET.Core.Interfaces;

namespace HubSpot.NET.Api.ContactList.Dto
{
[DataContract]
public class ContactListUpdateResponseModel : IHubSpotModel
{
[DataMember(Name = "discarded")]
public List<long> Discarded = new List<long>();

[DataMember(Name = "invalidVids")]
public List<long> InvalidContactIds = new List<long>();

[DataMember(Name = "updated")]
public List<long> UpdatedContactIds = new List<long>();

[IgnoreDataMember]
public bool IsNameValue => false;

public void ToHubSpotDataEntity(ref dynamic dataEntity)
{
}

public void FromHubSpotDataEntity(dynamic hubspotData)
{
}

[IgnoreDataMember]
public string RouteBasePath => "/contacts/v1/lists";
}
}
51 changes: 51 additions & 0 deletions HubSpot.NET/Api/ContactList/Dto/ListContactListModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
using HubSpot.NET.Core.Interfaces;

namespace HubSpot.NET.Api.ContactList.Dto
{
[DataContract]
public class ListContactListModel : IHubSpotModel
{
[DataMember(Name = "lists")]
public List<ContactListModel> Lists { get; set; } = new List<ContactListModel>();

/// <summary>
/// Gets or sets a value indicating whether more results are available.
/// </summary>
/// <value>
/// <c>true</c> if [more results available]; otherwise, <c>false</c>.
/// </value>
/// <remarks>
/// This is a mapping of the "has-more" prop in the JSON return data from HubSpot
/// </remarks>
[DataMember(Name = "has-more")]
public bool MoreResultsAvailable { get; set; }

/// <summary>
/// Gets or sets the continuation offset.
/// </summary>
/// <value>
/// The continuation offset.
/// </value>
/// <remarks>
/// This is a mapping of the "vid-offset" prop in the JSON reeturn data from HubSpot
/// </remarks>
[DataMember(Name = "offset")]
public long ContinuationOffset { get; set; }

[IgnoreDataMember]
public bool IsNameValue => false;

public void ToHubSpotDataEntity(ref dynamic dataEntity)
{
}

public void FromHubSpotDataEntity(dynamic hubspotData)
{
}

[IgnoreDataMember]
public string RouteBasePath => "/contacts/v1/lists";
}
}
83 changes: 83 additions & 0 deletions HubSpot.NET/Api/ContactList/HubSpotContactListApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System.Collections.Generic;
using Flurl;
using HubSpot.NET.Api.ContactList.Dto;
using HubSpot.NET.Core.Interfaces;
using RestSharp;

namespace HubSpot.NET.Api.ContactList
{
public class HubSpotContactListApi : IHubSpotContactListApi
{
private readonly IHubSpotClient _client;

public HubSpotContactListApi(IHubSpotClient client)
{
_client = client;
}

public ListContactListModel GetContactLists(ListOptions opts = null)
{
if (opts == null)
{
opts = new ListOptions();
}

var path = $"{new ListContactListModel().RouteBasePath}".SetQueryParam("count", opts.Limit);
if (opts.Offset.HasValue)
{
path = path.SetQueryParam("offset", opts.Offset);
}

var data = _client.ExecuteList<ListContactListModel>(path, convertToPropertiesSchema: false);

return data;
}

public ListContactListModel GetStaticContactLists(ListOptions opts = null)
{
if (opts == null)
{
opts = new ListOptions();
}

var path = $"{new ListContactListModel().RouteBasePath}/static".SetQueryParam("count", opts.Limit);
if (opts.Offset.HasValue)
{
path = path.SetQueryParam("offset", opts.Offset);
}

var data = _client.ExecuteList<ListContactListModel>(path, convertToPropertiesSchema: false);

return data;
}

public ContactListModel GetContactListById(long contactListId)
{
var path = $"{new ContactListModel().RouteBasePath}/{contactListId}";

var data = _client.ExecuteList<ContactListModel>(path, convertToPropertiesSchema: false);

return data;
}

public ContactListUpdateResponseModel AddContactsToList(long listId, IEnumerable<long> contactIds)
{
var model = new ContactListUpdateModel();
var path = $"{model.RouteBasePath}/{listId}/add";
model.ContactIds.AddRange(contactIds);
var data = _client.Execute<ContactListUpdateResponseModel>(path, model, Method.POST, convertToPropertiesSchema: false);

return data;
}

public ContactListUpdateResponseModel RemoveContactsFromList(long listId, IEnumerable<long> contactIds)
{
var model = new ContactListUpdateModel();
var path = $"{model.RouteBasePath}/{listId}/remove";
model.ContactIds.AddRange(contactIds);
var data = _client.Execute<ContactListUpdateResponseModel>(path, model, Method.POST, convertToPropertiesSchema: false);

return data;
}
}
}
42 changes: 42 additions & 0 deletions HubSpot.NET/Api/ContactList/ListOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;

namespace HubSpot.NET.Api.ContactList
{
public class ListOptions
{
/// <summary>
/// Get or set the continuation offset when calling list many times to enumerate all your items
/// </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 virtual long? Offset { get; set; } = null;

private int _limit = 20;
private readonly int _upperLimit = 250;

/// <summary>
/// Gets or sets the number of items to return.
/// </summary>
/// <remarks>
/// Defaults to 20 which is also the HubSpot API default. Max value is 100
/// </remarks>
/// <value>
/// The number of items to return.
/// </value>
public virtual int Limit
{
get => _limit;
set
{
if (value < 1 || value > _upperLimit)
{
throw new ArgumentException(
$"Number of items to return must be a positive integer greater than 0, and less than {_upperLimit} - you provided {value}");
}
_limit = value;
}
}
}
}
3 changes: 3 additions & 0 deletions HubSpot.NET/Core/HubSpotApi.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using HubSpot.NET.Api.Company;
using HubSpot.NET.Api.Contact;
using HubSpot.NET.Api.ContactList;
using HubSpot.NET.Api.Deal;
using HubSpot.NET.Api.EmailSubscriptions;
using HubSpot.NET.Api.Engagement;
Expand All @@ -22,6 +23,7 @@ public class HubSpotApi : IHubSpotApi
public IHubSpotCosFileApi File { get; }
public IHubSpotOwnerApi Owner { get; }
public IHubSpotCompanyPropertiesApi CompanyProperties { get; }
public IHubSpotContactListApi ContactLists { get; }

public IHubSpotEmailSubscriptionsApi EmailSubscriptions { get; }

Expand All @@ -37,6 +39,7 @@ public HubSpotApi(string apiKey)
Owner = new HubSpotOwnerApi(client);
CompanyProperties = new HubSpotCompaniesPropertiesApi(client);
EmailSubscriptions = new HubSpotEmailSubscriptionsApi(client);
ContactLists = new HubSpotContactListApi(client);
}

}
Expand Down
19 changes: 19 additions & 0 deletions HubSpot.NET/Core/Interfaces/IHubSpotContactListApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;
using HubSpot.NET.Api.ContactList;
using HubSpot.NET.Api.ContactList.Dto;

namespace HubSpot.NET.Core.Interfaces
{
public interface IHubSpotContactListApi
{
ListContactListModel GetContactLists(ListOptions opts = null);

ListContactListModel GetStaticContactLists(ListOptions opts = null);

ContactListModel GetContactListById(long contactListId);

ContactListUpdateResponseModel AddContactsToList(long listId, IEnumerable<long> contactIds);

ContactListUpdateResponseModel RemoveContactsFromList(long listId, IEnumerable<long> contactIds);
}
}

0 comments on commit 7930fe1

Please sign in to comment.