|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Net.Http; |
| 5 | +using Umbraco.Core.Composing; |
| 6 | +using Umbraco.Core.Logging; |
| 7 | +using Umbraco.Web.Editors; |
| 8 | +using Umbraco.Web.Mvc; |
| 9 | + |
| 10 | +namespace Umbraco.Forms.Extensions.Crm.Hubspot |
| 11 | +{ |
| 12 | + [PluginController("FormsExtensions")] |
| 13 | + public class HubspotController : UmbracoAuthorizedJsonController |
| 14 | + { |
| 15 | + static readonly HttpClient client = new HttpClient(); |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// ~/Umbraco/[YourAreaName]/[YourControllerName] |
| 19 | + /// ~/Umbraco/FormsExtensions/Hubspot/GetAllProperties?apiKey=123 |
| 20 | + /// </summary> |
| 21 | + /// <param name="apiKey"></param> |
| 22 | + /// <returns></returns> |
| 23 | + public List<Property> GetAllProperties(string apiKey) |
| 24 | + { |
| 25 | + var properties = new List<Property>(); |
| 26 | + var propertiesApiUrl = new Uri($"https://api.hubapi.com/crm/v3/properties/contacts?hapikey={apiKey}"); |
| 27 | + |
| 28 | + // Map fields we have in settings to these fields/properties in Hubspot |
| 29 | + var propertiesResponse = client.GetAsync(propertiesApiUrl).Result; |
| 30 | + if (propertiesResponse.IsSuccessStatusCode == false) |
| 31 | + { |
| 32 | + // Log error |
| 33 | + Current.Logger.Error<HubspotWorkflow>("Failed to fetch contact properties from HubSpot API for mapping. {StatusCode} {ReasonPhrase}", propertiesResponse.StatusCode, propertiesResponse.ReasonPhrase); |
| 34 | + } |
| 35 | + |
| 36 | + // Map Properties back to our simplier object |
| 37 | + // Don't need all the fields in the response |
| 38 | + |
| 39 | + |
| 40 | + return properties; |
| 41 | + } |
| 42 | + |
| 43 | + public class Property |
| 44 | + { |
| 45 | + public string Name { get; set; } |
| 46 | + |
| 47 | + public string Label { get; set; } |
| 48 | + |
| 49 | + public string Description { get; set; } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments