|
| 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 | +} |
0 commit comments