Skip to content

Commit

Permalink
Merge pull request #17 from bfutrell70/recent_deals
Browse files Browse the repository at this point in the history
Recent deals
  • Loading branch information
clarkd committed Jun 5, 2018
2 parents efa3d08 + 4e2297c commit da7f7e4
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 1 deletion.
30 changes: 29 additions & 1 deletion HubSpot.NET.Examples/Deals.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using HubSpot.NET.Api.Deal.Dto;
using HubSpot.NET.Core;
using System;
using System.Collections.Generic;

namespace HubSpot.NET.Examples
Expand Down Expand Up @@ -36,6 +37,7 @@ public static void Example()

/**
* Get all deals
* This is commented in case the HubSpot data has a large quantity of deals.
*/
//var moreResults = true;
//long offset = 0;
Expand All @@ -46,8 +48,34 @@ public static void Example()

// moreResults = allDeals.MoreResultsAvailable;
// if (moreResults) offset = allDeals.ContinuationOffset;

//}

/**
* Get recently created deals since 7 days ago, limited to 10 records
* Will default to 30 day if Since is not set.
* Using DealRecentListHubSpotModel to accomodate deals returning in the "results" property.
*/
var currentdatetime = DateTime.SpecifyKind(DateTime.Now.AddDays(-7), DateTimeKind.Utc);
var since = ((DateTimeOffset)currentdatetime).ToUnixTimeMilliseconds().ToString();

var recentlyCreatedDeals = api.Deal.RecentlyCreated<DealHubSpotModel>(new DealRecentRequestOptions
{
Limit = 10,
IncludePropertyVersion = false,
Since = since
});

/**
* Get recently created deals since 7 days ago, limited to 10 records
* Will default to 30 day if Since is not set.
* Using DealRecentListHubSpotModel to accomodate deals returning in the "results" property.
*/
var recentlyUpdatedDeals = api.Deal.RecentlyCreated<DealHubSpotModel>(new DealRecentRequestOptions
{
Limit = 10,
IncludePropertyVersion = false,
Since = since
});
}
}
}
1 change: 1 addition & 0 deletions HubSpot.NET/Api/Company/HubSpotCompanyApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,6 @@ public void Delete(long companyId)

_client.Execute(path, method: Method.DELETE);
}

}
}
28 changes: 28 additions & 0 deletions HubSpot.NET/Api/Deal/Dto/DealRecentListHubSpotModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace HubSpot.NET.Api.Deal.Dto
{
/// <summary>
/// Models a set of results returned when querying for sets of recently created or modified deals
/// </summary>
/// <remarks>
/// The sole reason for this class is because HubSpot uses a different property when returning
/// recently created or modified deals versus all deals.
///
/// With retrieving all deals the deals are returned in the property "deals".
/// With recent deals the deals are returned in the property "results".
/// </remarks>
public class DealRecentListHubSpotModel<T> : DealListHubSpotModel<T> where T : DealHubSpotModel, new()
{
/// <summary>
/// Gets or sets the deals.
/// </summary>
/// <value>
/// The list of deals.
/// </value>
[DataMember(Name = "results")]
public new IList<T> Deals { get; set; } = new List<T>();

}
}
22 changes: 22 additions & 0 deletions HubSpot.NET/Api/Deal/Dto/DealRecentRequestOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using HubSpot.NET.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HubSpot.NET.Api.Deal.Dto
{
public class DealRecentRequestOptions : ListRequestOptions
{
/// <summary>
/// Used to specify the oldest timestamp to use to retrieve deals
/// </summary>
public string Since { get; set; }

/// <summary>
/// Specififes if the current value for a property should be fetched or all historical values
/// </summary>
public bool IncludePropertyVersion { get; set; } = false;
}
}
73 changes: 73 additions & 0 deletions HubSpot.NET/Api/Deal/HubSpotDealApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,78 @@ public void Delete(long dealId)

_client.Execute(path, method: Method.DELETE);
}

/// <summary>
/// Gets a list of recently created deals
/// </summary>
/// <typeparam name="T">Implementation of DealListHubSpotModel</typeparam>
/// <param name="opts">Options (limit, offset) relating to request</param>
/// <returns>List of deals</returns>
public DealRecentListHubSpotModel<T> RecentlyCreated<T>(DealRecentRequestOptions opts = null) where T : DealHubSpotModel, new()
{

if (opts == null)
{
opts = new DealRecentRequestOptions();
}

var path = $"{new DealRecentListHubSpotModel<T>().RouteBasePath}/deal/recent/created"
.SetQueryParam("limit", opts.Limit);

if (opts.Offset.HasValue)
{
path = path.SetQueryParam("offset", opts.Offset);
}

if (opts.IncludePropertyVersion)
{
path = path.SetQueryParam("includePropertyVersions", "true");
}

if (!string.IsNullOrEmpty(opts.Since))
{
path = path.SetQueryParam("since", opts.Since);
}

var data = _client.ExecuteList<DealRecentListHubSpotModel<T>>(path, opts);

return data;
}

/// <summary>
/// Gets a list of recently modified deals
/// </summary>
/// <typeparam name="T">Implementation of DealListHubSpotModel</typeparam>
/// <param name="opts">Options (limit, offset) relating to request</param>
/// <returns>List of deals</returns>
public DealRecentListHubSpotModel<T> RecentlyUpdated<T>(DealRecentRequestOptions opts = null) where T : DealHubSpotModel, new()
{
if (opts == null)
{
opts = new DealRecentRequestOptions();
}

var path = $"{new DealRecentListHubSpotModel<T>().RouteBasePath}/deal/recent/modified"
.SetQueryParam("limit", opts.Limit);

if (opts.Offset.HasValue)
{
path = path.SetQueryParam("offset", opts.Offset);
}

if (opts.IncludePropertyVersion)
{
path = path.SetQueryParam("includePropertyVersions", "true");
}

if (!string.IsNullOrEmpty(opts.Since))
{
path = path.SetQueryParam("since", opts.Since);
}

var data = _client.ExecuteList<DealRecentListHubSpotModel<T>>(path, opts);

return data;
}
}
}
6 changes: 6 additions & 0 deletions HubSpot.NET/Core/Interfaces/IHubSpotDealApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ public interface IHubSpotDealApi

DealListHubSpotModel<T> List<T>(bool includeAssociations, ListRequestOptions opts = null)
where T : DealHubSpotModel, new();

DealRecentListHubSpotModel<T> RecentlyCreated<T>(DealRecentRequestOptions opts = null)
where T : DealHubSpotModel, new();

DealRecentListHubSpotModel<T> RecentlyUpdated<T>(DealRecentRequestOptions opts = null)
where T : DealHubSpotModel, new();
}
}

0 comments on commit da7f7e4

Please sign in to comment.