Skip to content

Commit

Permalink
Fix issues with updating legal basis on specific email subscriptions …
Browse files Browse the repository at this point in the history
…and fix unsubscribe functionality
  • Loading branch information
lakesol authored and cdmdotnet committed Feb 19, 2022
1 parent 5ce9df5 commit 4379b58
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using HubSpot.NET.Core.Interfaces;

namespace HubSpot.NET.Api.EmailSubscriptions.Dto
Expand All @@ -19,6 +15,7 @@ public class SubscribeHubSpotModel : IHubSpotModel
[DataMember(Name = "subscriptionStatuses")]
public List<SubscribeStatusHubSpotModel> SubscriptionStatuses { get; set; }

[IgnoreDataMember]
public bool IsNameValue { get; }

public void ToHubSpotDataEntity(ref dynamic dataEntity)
Expand All @@ -29,6 +26,8 @@ public void FromHubSpotDataEntity(dynamic hubspotData)
{
}

[IgnoreDataMember]
public string RouteBasePath => "/email/public/v1/subscriptions";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@ public class SubscriptionStatusDetailHubSpotModel

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

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

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

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

namespace HubSpot.NET.Api.EmailSubscriptions.Dto
Expand All @@ -14,20 +10,21 @@ public class SubscriptionStatusHubSpotModel : IHubSpotModel
public bool Subscribed { get; set; }

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

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

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

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

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

[IgnoreDataMember]
public bool IsNameValue { get; }

public void ToHubSpotDataEntity(ref dynamic dataEntity)
Expand All @@ -38,6 +35,7 @@ public void FromHubSpotDataEntity(dynamic hubspotData)
{
}

[IgnoreDataMember]
public string RouteBasePath => "/email/public/v1";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
using HubSpot.NET.Core.Interfaces;

namespace HubSpot.NET.Api.EmailSubscriptions.Dto
{
/// <summary>
/// The subscription status update hub spot model class
/// </summary>
/// <seealso cref="IHubSpotModel"/>
public class SubscriptionStatusUpdateHubSpotModel : IHubSpotModel
{
/// <summary>
/// Gets or sets the value of the subscription statuses
/// </summary>
[DataMember(Name = "subscriptionStatuses")]
public List<SubscriptionStatusDetailHubSpotModel> SubscriptionStatuses { get; set; }

/// <summary>
/// Gets the value of the is name value
/// </summary>
[IgnoreDataMember]
public bool IsNameValue { get; }

/// <summary>
/// Returns the hub spot data entity using the specified data entity
/// </summary>
/// <param name="dataEntity">The data entity</param>
public void ToHubSpotDataEntity(ref dynamic dataEntity)
{
}

/// <summary>
/// Creates the hub spot data entity using the specified hubspot data
/// </summary>
/// <param name="hubspotData">The hubspot data</param>
public void FromHubSpotDataEntity(dynamic hubspotData)
{
}

/// <summary>
/// Gets the value of the route base path
/// </summary>
[IgnoreDataMember]
public string RouteBasePath => "/email/public/v1";
}
}
68 changes: 52 additions & 16 deletions HubSpot.NET/Api/EmailSubscriptions/HubSpotEmailSubcriptionsApi.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
namespace HubSpot.NET.Api.EmailSubscriptions
namespace HubSpot.NET.Api.EmailSubscriptions
{
using System.Collections.Generic;
using HubSpot.NET.Api.EmailSubscriptions.Dto;
using HubSpot.NET.Core.Interfaces;
using RestSharp;

/// <summary>
/// The hub spot email subscriptions api class
/// </summary>
/// <seealso cref="IHubSpotEmailSubscriptionsApi"/>
public class HubSpotEmailSubscriptionsApi : IHubSpotEmailSubscriptionsApi
{
/// <summary>
/// The client
/// </summary>
private readonly IHubSpotClient _client;

/// <summary>
/// Initializes a new instance of the <see cref="HubSpotEmailSubscriptionsApi"/> class
/// </summary>
/// <param name="client">The client</param>
public HubSpotEmailSubscriptionsApi(IHubSpotClient client)
{
_client = client;
Expand Down Expand Up @@ -58,7 +69,7 @@ public void UnsubscribeFrom(string email, long id)
{
var path = $"{new SubscriptionTypeListHubSpotModel().RouteBasePath}/subscriptions/{email}";

var model = new SubscriptionStatusHubSpotModel
var model = new SubscriptionStatusUpdateHubSpotModel
{
SubscriptionStatuses = new List<SubscriptionStatusDetailHubSpotModel>()
{
Expand All @@ -74,30 +85,55 @@ public void UnsubscribeFrom(string email, long id)
}



/// <summary>
/// Subscribe the given email address to the given subscription type
/// See <see cref="https://legacydocs.hubspot.com/docs/methods/email/update_status"/>
/// </summary>
public void SubscribeTo(string email, long id, string basis, string basisExplaination)
/// </summary> /// <param name="email">The email</param>
/// <param name="id">The id</param>
/// <param name="basis">The basis</param>
/// <param name="basisExplanation">The basis explanation</param>
/// <param name="setPortalSubscriptionBasis">The set portal subscription basis</param>
/// <param name="setSubscriptionBasis">The set subscription basis</param>
/// <param name="subscriptionBasis">The subscription basis</param>
/// <param name="subscriptionBasisExplanation">The subscription basis explanation</param>
public void SubscribeTo(string email, long id, string basis, string basisExplanation, bool setPortalSubscriptionBasis = true, bool setSubscriptionBasis = false, string subscriptionBasis = null, string subscriptionBasisExplanation = null)
{
var model = new SubscribeHubSpotModel
var subscription = new SubscribeStatusHubSpotModel()
{
Id = id,
Subscribed = true,
OptState = "OPT_IN"
};
if (setSubscriptionBasis)
{
SubscriptionStatuses = new List<SubscribeStatusHubSpotModel>()
if (string.IsNullOrEmpty(subscriptionBasis))
{
new SubscribeStatusHubSpotModel()
{
Id = id,
Subscribed = true,
OptState = "OPT_IN"
}
},
SubscriptionLegalBasis = basis,
SubscriptionLegalBasisExplanation = basisExplaination
subscriptionBasis = basis;
}

if (subscriptionBasisExplanation == null)
{
subscriptionBasisExplanation = basisExplanation;
}

subscription.LegalBasis = subscriptionBasis;
subscription.LegalBasisExplanation = subscriptionBasisExplanation;
}

var model = new SubscribeHubSpotModel
{
SubscriptionStatuses = new List<SubscribeStatusHubSpotModel>() { subscription }
};
if (setPortalSubscriptionBasis)
{
model.SubscriptionLegalBasis = basis;
model.SubscriptionLegalBasisExplanation = basisExplanation;
}

var path = $"{model.RouteBasePath}/{email}";

_client.Execute(path, model, Method.PUT, false);
}
}
}
}
11 changes: 9 additions & 2 deletions HubSpot.NET/Core/Interfaces/IHubSpotEmailSubscriptionsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ public interface IHubSpotEmailSubscriptionsApi
/// <summary>
/// Subscribe the given email address to the given subscription type
/// See <see cref="https://legacydocs.hubspot.com/docs/methods/email/update_status"/>
/// </summary>
void SubscribeTo(string email, long id, string basis, string basisExplaination);
/// </summary> /// <param name="email">The email</param>
/// <param name="id">The id</param>
/// <param name="basis">The basis</param>
/// <param name="basisExplanation">The basis explanation</param>
/// <param name="setPortalSubscriptionBasis">The set portal subscription basis</param>
/// <param name="setSubscriptionBasis">The set subscription basis</param>
/// <param name="subscriptionBasis">The subscription basis</param>
/// <param name="subscriptionBasisExplanation">The subscription basis explanation</param>
void SubscribeTo(string email, long id, string basis, string basisExplanation, bool setPortalSubscriptionBasis = true, bool setSubscriptionBasis = false, string subscriptionBasis = null, string subscriptionBasisExplanation = null);
}
}

0 comments on commit 4379b58

Please sign in to comment.