Skip to content

Commit c6cb166

Browse files
author
Warren Buckley
committed
Add in a component to register API url in ServerVariables so we can get in JS the correct API path to controller for new field mapping Forms Workflow Setting UI
1 parent cdfface commit c6cb166

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Web;
4+
using System.Web.Mvc;
5+
using System.Web.Routing;
6+
using Umbraco.Core.Composing;
7+
using Umbraco.Web;
8+
using Umbraco.Web.JavaScript;
9+
10+
namespace Umbraco.Forms.Extensions.Crm.Hubspot
11+
{
12+
public class HubspotComposer : ComponentComposer<HubspotComponent>, IUserComposer { }
13+
14+
public class HubspotComponent : IComponent
15+
{
16+
public void Initialize()
17+
{
18+
ServerVariablesParser.Parsing += ServerVariablesParser_Parsing;
19+
}
20+
21+
private void ServerVariablesParser_Parsing(object sender, Dictionary<string, object> e)
22+
{
23+
if (HttpContext.Current == null) throw new InvalidOperationException("HttpContext is null");
24+
var urlHelper = new UrlHelper(new RequestContext(new HttpContextWrapper(HttpContext.Current), new RouteData()));
25+
e.Add("Umbraco.Forms.Extensions.HubSpot", new Dictionary<string, object>
26+
{
27+
{"GetPropertiesBaseUrl", urlHelper.GetUmbracoApiServiceBaseUrl<HubspotController>(controller => controller.GetAllProperties(null))},
28+
});
29+
}
30+
31+
public void Terminate()
32+
{
33+
ServerVariablesParser.Parsing -= ServerVariablesParser_Parsing;
34+
}
35+
}
36+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

Comments
 (0)