Skip to content

Commit a9bea26

Browse files
committed
Add the email subscriptions API
Doesn't include Timeline at this time
1 parent fc1826e commit a9bea26

12 files changed

+268
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Linq;
2+
using HubSpot.NET.Core;
3+
4+
namespace HubSpot.NET.Examples
5+
{
6+
public class EmailSubscriptions
7+
{
8+
public static void Example()
9+
{
10+
/**
11+
* Initialize the API with your API Key
12+
* You can find or generate this under Integrations -> HubSpot API key
13+
*/
14+
var api = new HubSpotApi("YOUR-API-KEY-HERE");
15+
16+
/**
17+
* Get the available subscription types
18+
*/
19+
var all = api.EmailSubscriptions.GetEmailSubscriptionTypes();
20+
21+
/**
22+
* Get the subscription statuses for the given email address
23+
* A missing type implies that they have not opted out
24+
*/
25+
var john = api.EmailSubscriptions.GetStatus("[email protected]");
26+
27+
/**
28+
* Unsubscribe a user from ALL emails
29+
* WARNING: You cannot undo this
30+
*/
31+
api.EmailSubscriptions.UnsubscribeAll("[email protected]");
32+
33+
34+
/**
35+
* Unsubscribe a user from a given email type
36+
* WARNING: You cannot undo this
37+
*/
38+
var type = all.Types.First();
39+
api.EmailSubscriptions.UnsubscribeFrom("[email protected]", type.Id);
40+
41+
}
42+
}
43+
}

HubSpot.NET.Examples/HubSpot.NET.Examples.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<Reference Include="System.Xml" />
4343
</ItemGroup>
4444
<ItemGroup>
45+
<Compile Include="EmailSubscriptions.cs" />
4546
<Compile Include="CompanyProperties.cs" />
4647
<Compile Include="Companies.cs" />
4748
<Compile Include="Deals.cs" />

HubSpot.NET.Examples/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ static void Main(string[] args)
2222
Contacts.Example();
2323

2424
CompanyProperties.Example();
25+
26+
EmailSubscriptions.Example();
2527
}
2628
}
2729
}

HubSpot.NET/Api/Deal/Dto/DealListHubSpotModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace HubSpot.NET.Api.Deal.Dto
1818
/// The contacts.
1919
/// </value>
2020
[DataMember(Name = "deals")]
21-
public IList<DealHubSpotModel> Deals { get; set; } = new List<DealHubSpotModel>();
21+
public IList<T> Deals { get; set; } = new List<T>();
2222

2323
/// <summary>
2424
/// Gets or sets a value indicating whether more results are available.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace HubSpot.NET.Api.EmailSubscriptions.Dto
4+
{
5+
public class SubscriptionStatusDetailHubSpotModel
6+
{
7+
[DataMember(Name = "id")]
8+
public long Id { get; set; }
9+
10+
[DataMember(Name = "subscribed")]
11+
public bool Subscribed { get;set; }
12+
13+
[DataMember(Name = "updatedAt")]
14+
public string UpdatedAt { get;set; }
15+
}
16+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.Serialization;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using HubSpot.NET.Core.Interfaces;
8+
9+
namespace HubSpot.NET.Api.EmailSubscriptions.Dto
10+
{
11+
public class SubscriptionStatusHubSpotModel : IHubSpotModel
12+
{
13+
[DataMember(Name = "subscribed")]
14+
public bool Subscribed { get; set; }
15+
16+
[DataMember(Name = "markedAsSpam")]
17+
public bool MarkedAsSpam { get;set; }
18+
19+
[DataMember(Name = "bounced")]
20+
public bool Bounced { get; set; }
21+
22+
[DataMember(Name = "email")]
23+
public string Email { get;set; }
24+
25+
[DataMember(Name = "status")]
26+
public string Status { get; set; }
27+
28+
[DataMember(Name = "subscriptionStatuses")]
29+
public List<SubscriptionStatusDetailHubSpotModel> SubscriptionStatuses { get; set; }
30+
31+
public bool IsNameValue { get; }
32+
33+
public void ToHubSpotDataEntity(ref dynamic dataEntity)
34+
{
35+
}
36+
37+
public void FromHubSpotDataEntity(dynamic hubspotData)
38+
{
39+
}
40+
41+
public string RouteBasePath => "/email/public/v1";
42+
}
43+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.Serialization;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using HubSpot.NET.Core.Interfaces;
8+
9+
namespace HubSpot.NET.Api.EmailSubscriptions.Dto
10+
{
11+
[DataContract]
12+
public class SubscriptionTypeHubSpotModel : IHubSpotModel
13+
{
14+
[DataMember(Name = "active")]
15+
public bool Active { get; set; }
16+
17+
[DataMember(Name = "description")]
18+
public string Description { get;set; }
19+
20+
[DataMember(Name = "id")]
21+
public long Id { get; set; }
22+
23+
[DataMember(Name = "name")]
24+
public string Name { get;set; }
25+
26+
public bool IsNameValue { get; }
27+
public void ToHubSpotDataEntity(ref dynamic dataEntity)
28+
{
29+
}
30+
31+
public void FromHubSpotDataEntity(dynamic hubspotData)
32+
{
33+
}
34+
35+
public string RouteBasePath => "/email/public/v1";
36+
}
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections.Generic;
2+
using System.Runtime.Serialization;
3+
using HubSpot.NET.Api.Contact.Dto;
4+
using HubSpot.NET.Api.EmailSubscriptions.Dto;
5+
using HubSpot.NET.Core.Interfaces;
6+
7+
namespace HubSpot.NET.Api.EmailSubscriptions.Dto
8+
{
9+
[DataContract]
10+
public class SubscriptionTypeListHubSpotModel : IHubSpotModel
11+
{
12+
[DataMember(Name = "subscriptionDefinitions")]
13+
public IList<SubscriptionTypeHubSpotModel> Types { get; set; } = new List<SubscriptionTypeHubSpotModel>();
14+
15+
public string RouteBasePath => "/email/public/v1";
16+
17+
public bool IsNameValue => false;
18+
19+
public virtual void ToHubSpotDataEntity(ref dynamic converted)
20+
{
21+
}
22+
23+
public virtual void FromHubSpotDataEntity(dynamic hubspotData)
24+
{
25+
}
26+
}
27+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Flurl;
5+
using HubSpot.NET.Api.Deal.Dto;
6+
using HubSpot.NET.Api.EmailSubscriptions.Dto;
7+
using HubSpot.NET.Core;
8+
using HubSpot.NET.Core.Interfaces;
9+
using RestSharp;
10+
11+
namespace HubSpot.NET.Api.EmailSubscriptions
12+
{
13+
public class HubSpotEmailSubscriptionsApi : IHubSpotEmailSubscriptionsApi
14+
{
15+
private readonly IHubSpotClient _client;
16+
17+
public HubSpotEmailSubscriptionsApi(IHubSpotClient client)
18+
{
19+
_client = client;
20+
}
21+
22+
/// <summary>
23+
/// Gets the available email subscription types available in the portal
24+
/// </summary>
25+
public SubscriptionTypeListHubSpotModel GetEmailSubscriptionTypes()
26+
{
27+
var path = $"{new SubscriptionTypeListHubSpotModel().RouteBasePath}/subscriptions";
28+
29+
return _client.ExecuteList<SubscriptionTypeListHubSpotModel>(path, convertToPropertiesSchema: false);
30+
}
31+
32+
/// <summary>
33+
/// Get subscription status for the given email address
34+
/// </summary>
35+
/// <param name="email"></param>
36+
public SubscriptionStatusHubSpotModel GetStatus(string email)
37+
{
38+
var path = $"{new SubscriptionTypeListHubSpotModel().RouteBasePath}/subscriptions/{email}";
39+
40+
return _client.Execute<SubscriptionStatusHubSpotModel>(path, Method.GET, false);
41+
}
42+
43+
44+
/// <summary>
45+
/// Unsubscribe the given email address from ALL email
46+
/// WARNING: There is no UNDO for this operation
47+
/// </summary>
48+
/// <param name="email"></param>
49+
public void UnsubscribeAll(string email)
50+
{
51+
var path = $"{new SubscriptionTypeListHubSpotModel().RouteBasePath}/subscriptions/{email}";
52+
53+
_client.Execute(path, new { unsubscribeFromAll = true }, Method.PUT, false);
54+
}
55+
56+
/// <summary>
57+
/// Unsubscribe the given email address from the given subscription type
58+
/// WARNING: There is no UNDO for this operation
59+
/// </summary>
60+
/// <param name="email"></param>
61+
/// <param name="id">The ID of the subscription type</param>
62+
public void UnsubscribeFrom(string email, long id)
63+
{
64+
var path = $"{new SubscriptionTypeListHubSpotModel().RouteBasePath}/subscriptions/{email}";
65+
66+
var model = new SubscriptionStatusHubSpotModel
67+
{
68+
SubscriptionStatuses = new List<SubscriptionStatusDetailHubSpotModel>()
69+
{
70+
new SubscriptionStatusDetailHubSpotModel()
71+
{
72+
Id = id,
73+
Subscribed = false
74+
}
75+
}
76+
};
77+
78+
_client.Execute(path, model, Method.PUT, false);
79+
}
80+
}
81+
}

HubSpot.NET/Core/HubSpotApi.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using HubSpot.NET.Api.Company;
22
using HubSpot.NET.Api.Contact;
33
using HubSpot.NET.Api.Deal;
4+
using HubSpot.NET.Api.EmailSubscriptions;
45
using HubSpot.NET.Api.Engagement;
56
using HubSpot.NET.Api.Files;
67
using HubSpot.NET.Api.Owner;
@@ -22,6 +23,8 @@ public class HubSpotApi : IHubSpotApi
2223
public IHubSpotOwnerApi Owner { get; }
2324
public IHubSpotCompanyPropertiesApi CompanyProperties { get; }
2425

26+
public IHubSpotEmailSubscriptionsApi EmailSubscriptions { get; }
27+
2528
public HubSpotApi(string apiKey)
2629
{
2730
IHubSpotClient client = new HubSpotBaseClient(apiKey);
@@ -33,6 +36,7 @@ public HubSpotApi(string apiKey)
3336
File = new HubSpotCosFileApi(client);
3437
Owner = new HubSpotOwnerApi(client);
3538
CompanyProperties = new HubSpotCompaniesPropertiesApi(client);
39+
EmailSubscriptions = new HubSpotEmailSubscriptionsApi(client);
3640
}
3741

3842
}

0 commit comments

Comments
 (0)