Skip to content

Commit

Permalink
Add the email subscriptions API
Browse files Browse the repository at this point in the history
Doesn't include Timeline at this time
  • Loading branch information
clarkd committed Mar 9, 2018
1 parent fc1826e commit a9bea26
Show file tree
Hide file tree
Showing 12 changed files with 268 additions and 1 deletion.
43 changes: 43 additions & 0 deletions HubSpot.NET.Examples/EmailSubscriptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Linq;
using HubSpot.NET.Core;

namespace HubSpot.NET.Examples
{
public class EmailSubscriptions
{
public static void Example()
{
/**
* Initialize the API with your API Key
* You can find or generate this under Integrations -> HubSpot API key
*/
var api = new HubSpotApi("YOUR-API-KEY-HERE");

/**
* Get the available subscription types
*/
var all = api.EmailSubscriptions.GetEmailSubscriptionTypes();

/**
* Get the subscription statuses for the given email address
* A missing type implies that they have not opted out
*/
var john = api.EmailSubscriptions.GetStatus("[email protected]");

/**
* Unsubscribe a user from ALL emails
* WARNING: You cannot undo this
*/
api.EmailSubscriptions.UnsubscribeAll("[email protected]");


/**
* Unsubscribe a user from a given email type
* WARNING: You cannot undo this
*/
var type = all.Types.First();
api.EmailSubscriptions.UnsubscribeFrom("[email protected]", type.Id);

}
}
}
1 change: 1 addition & 0 deletions HubSpot.NET.Examples/HubSpot.NET.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="EmailSubscriptions.cs" />
<Compile Include="CompanyProperties.cs" />
<Compile Include="Companies.cs" />
<Compile Include="Deals.cs" />
Expand Down
2 changes: 2 additions & 0 deletions HubSpot.NET.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ static void Main(string[] args)
Contacts.Example();

CompanyProperties.Example();

EmailSubscriptions.Example();
}
}
}
2 changes: 1 addition & 1 deletion HubSpot.NET/Api/Deal/Dto/DealListHubSpotModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace HubSpot.NET.Api.Deal.Dto
/// The contacts.
/// </value>
[DataMember(Name = "deals")]
public IList<DealHubSpotModel> Deals { get; set; } = new List<DealHubSpotModel>();
public IList<T> Deals { get; set; } = new List<T>();

/// <summary>
/// Gets or sets a value indicating whether more results are available.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Runtime.Serialization;

namespace HubSpot.NET.Api.EmailSubscriptions.Dto
{
public class SubscriptionStatusDetailHubSpotModel
{
[DataMember(Name = "id")]
public long Id { get; set; }

[DataMember(Name = "subscribed")]
public bool Subscribed { get;set; }

[DataMember(Name = "updatedAt")]
public string UpdatedAt { get;set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using HubSpot.NET.Core.Interfaces;

namespace HubSpot.NET.Api.EmailSubscriptions.Dto
{
public class SubscriptionStatusHubSpotModel : IHubSpotModel
{
[DataMember(Name = "subscribed")]
public bool Subscribed { get; set; }

[DataMember(Name = "markedAsSpam")]
public bool MarkedAsSpam { get;set; }

[DataMember(Name = "bounced")]
public bool Bounced { get; set; }

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

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

[DataMember(Name = "subscriptionStatuses")]
public List<SubscriptionStatusDetailHubSpotModel> SubscriptionStatuses { get; set; }

public bool IsNameValue { get; }

public void ToHubSpotDataEntity(ref dynamic dataEntity)
{
}

public void FromHubSpotDataEntity(dynamic hubspotData)
{
}

public string RouteBasePath => "/email/public/v1";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using HubSpot.NET.Core.Interfaces;

namespace HubSpot.NET.Api.EmailSubscriptions.Dto
{
[DataContract]
public class SubscriptionTypeHubSpotModel : IHubSpotModel
{
[DataMember(Name = "active")]
public bool Active { get; set; }

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

[DataMember(Name = "id")]
public long Id { get; set; }

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

public bool IsNameValue { get; }
public void ToHubSpotDataEntity(ref dynamic dataEntity)
{
}

public void FromHubSpotDataEntity(dynamic hubspotData)
{
}

public string RouteBasePath => "/email/public/v1";
}
}
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.Api.Contact.Dto;
using HubSpot.NET.Api.EmailSubscriptions.Dto;
using HubSpot.NET.Core.Interfaces;

namespace HubSpot.NET.Api.EmailSubscriptions.Dto
{
[DataContract]
public class SubscriptionTypeListHubSpotModel : IHubSpotModel
{
[DataMember(Name = "subscriptionDefinitions")]
public IList<SubscriptionTypeHubSpotModel> Types { get; set; } = new List<SubscriptionTypeHubSpotModel>();

public string RouteBasePath => "/email/public/v1";

public bool IsNameValue => false;

public virtual void ToHubSpotDataEntity(ref dynamic converted)
{
}

public virtual void FromHubSpotDataEntity(dynamic hubspotData)
{
}
}
}
81 changes: 81 additions & 0 deletions HubSpot.NET/Api/EmailSubscriptions/HubSpotEmailSubcriptionsApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Flurl;
using HubSpot.NET.Api.Deal.Dto;
using HubSpot.NET.Api.EmailSubscriptions.Dto;
using HubSpot.NET.Core;
using HubSpot.NET.Core.Interfaces;
using RestSharp;

namespace HubSpot.NET.Api.EmailSubscriptions
{
public class HubSpotEmailSubscriptionsApi : IHubSpotEmailSubscriptionsApi
{
private readonly IHubSpotClient _client;

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

/// <summary>
/// Gets the available email subscription types available in the portal
/// </summary>
public SubscriptionTypeListHubSpotModel GetEmailSubscriptionTypes()
{
var path = $"{new SubscriptionTypeListHubSpotModel().RouteBasePath}/subscriptions";

return _client.ExecuteList<SubscriptionTypeListHubSpotModel>(path, convertToPropertiesSchema: false);
}

/// <summary>
/// Get subscription status for the given email address
/// </summary>
/// <param name="email"></param>
public SubscriptionStatusHubSpotModel GetStatus(string email)
{
var path = $"{new SubscriptionTypeListHubSpotModel().RouteBasePath}/subscriptions/{email}";

return _client.Execute<SubscriptionStatusHubSpotModel>(path, Method.GET, false);
}


/// <summary>
/// Unsubscribe the given email address from ALL email
/// WARNING: There is no UNDO for this operation
/// </summary>
/// <param name="email"></param>
public void UnsubscribeAll(string email)
{
var path = $"{new SubscriptionTypeListHubSpotModel().RouteBasePath}/subscriptions/{email}";

_client.Execute(path, new { unsubscribeFromAll = true }, Method.PUT, false);
}

/// <summary>
/// Unsubscribe the given email address from the given subscription type
/// WARNING: There is no UNDO for this operation
/// </summary>
/// <param name="email"></param>
/// <param name="id">The ID of the subscription type</param>
public void UnsubscribeFrom(string email, long id)
{
var path = $"{new SubscriptionTypeListHubSpotModel().RouteBasePath}/subscriptions/{email}";

var model = new SubscriptionStatusHubSpotModel
{
SubscriptionStatuses = new List<SubscriptionStatusDetailHubSpotModel>()
{
new SubscriptionStatusDetailHubSpotModel()
{
Id = id,
Subscribed = false
}
}
};

_client.Execute(path, model, Method.PUT, false);
}
}
}
4 changes: 4 additions & 0 deletions HubSpot.NET/Core/HubSpotApi.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using HubSpot.NET.Api.Company;
using HubSpot.NET.Api.Contact;
using HubSpot.NET.Api.Deal;
using HubSpot.NET.Api.EmailSubscriptions;
using HubSpot.NET.Api.Engagement;
using HubSpot.NET.Api.Files;
using HubSpot.NET.Api.Owner;
Expand All @@ -22,6 +23,8 @@ public class HubSpotApi : IHubSpotApi
public IHubSpotOwnerApi Owner { get; }
public IHubSpotCompanyPropertiesApi CompanyProperties { get; }

public IHubSpotEmailSubscriptionsApi EmailSubscriptions { get; }

public HubSpotApi(string apiKey)
{
IHubSpotClient client = new HubSpotBaseClient(apiKey);
Expand All @@ -33,6 +36,7 @@ public HubSpotApi(string apiKey)
File = new HubSpotCosFileApi(client);
Owner = new HubSpotOwnerApi(client);
CompanyProperties = new HubSpotCompaniesPropertiesApi(client);
EmailSubscriptions = new HubSpotEmailSubscriptionsApi(client);
}

}
Expand Down
1 change: 1 addition & 0 deletions HubSpot.NET/Core/Interfaces/IHubSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public interface IHubSpotApi
IHubSpotCosFileApi File { get; }
IHubSpotOwnerApi Owner { get; }
IHubSpotCompanyPropertiesApi CompanyProperties { get; }
IHubSpotEmailSubscriptionsApi EmailSubscriptions { get; }
}
}
12 changes: 12 additions & 0 deletions HubSpot.NET/Core/Interfaces/IHubSpotEmailSubscriptionsApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using HubSpot.NET.Api.EmailSubscriptions.Dto;

namespace HubSpot.NET.Core.Interfaces
{
public interface IHubSpotEmailSubscriptionsApi
{
SubscriptionTypeListHubSpotModel GetEmailSubscriptionTypes();
SubscriptionStatusHubSpotModel GetStatus(string email);
void UnsubscribeAll(string email);
void UnsubscribeFrom(string email, long id);
}
}

0 comments on commit a9bea26

Please sign in to comment.