forked from hubspot-net/HubSpot.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement basic contact list API operations
- Loading branch information
Showing
8 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
HubSpot.NET/Api/ContactList/Dto/ContactListUpdateResponseModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |