-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Doesn't include Timeline at this time
- Loading branch information
Showing
12 changed files
with
268 additions
and
1 deletion.
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,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); | ||
|
||
} | ||
} | ||
} |
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
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
16 changes: 16 additions & 0 deletions
16
HubSpot.NET/Api/EmailSubscriptions/Dto/SubscriptionStatusDetailHubSpotModel.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,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; } | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
HubSpot.NET/Api/EmailSubscriptions/Dto/SubscriptionStatusHubSpotModel.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,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"; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
HubSpot.NET/Api/EmailSubscriptions/Dto/SubscriptionTypeHubSpotModel.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,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"; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
HubSpot.NET/Api/EmailSubscriptions/Dto/SubscriptionTypeListHubSpotModel.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,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
81
HubSpot.NET/Api/EmailSubscriptions/HubSpotEmailSubcriptionsApi.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,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); | ||
} | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
HubSpot.NET/Core/Interfaces/IHubSpotEmailSubscriptionsApi.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,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); | ||
} | ||
} |