Skip to content

Commit d059e92

Browse files
author
Warren Buckley
committed
Adds in API Uri and verifies the API key is not empty & is valid by not getting a HTTP error code
1 parent d1bd5e9 commit d059e92

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

src/Umbraco.Forms.Extensions.Crm.Hubspot/HubspotWorkflow.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,55 @@ public HubspotWorkflow()
1717
Group = "CRM";
1818
}
1919

20+
[Setting("Hubspot API Key", Description = "Enter the API Key from your HubSpot account", View = "TextField")]
21+
public string HubspotApiKey { get; set; }
22+
23+
private Uri HubspotApiUrl
24+
{
25+
get
26+
{
27+
return new Uri($"https://api.hubapi.com/crm/v3/objects/contacts?hapikey={HubspotApiKey}");
28+
}
29+
}
30+
31+
static readonly HttpClient client = new HttpClient();
2032
public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
2133
{
2234
return WorkflowExecutionStatus.Completed;
2335
}
2436

2537
public override List<Exception> ValidateSettings()
2638
{
27-
return new List<Exception>();
39+
var errors = new List<Exception>();
40+
41+
// Verify API key is not empty
42+
if (string.IsNullOrWhiteSpace(HubspotApiKey))
43+
{
44+
errors.Add(new Exception("Hubspot API key is missing"));
45+
return errors;
46+
}
47+
48+
// Make a super simple GET request to fetch contacts in HubSpot
49+
// This way with we can verify that the API key is valid
50+
// https://developers.hubspot.com/docs/api/crm/contacts
51+
var testResponse = client.GetAsync(HubspotApiUrl).Result;
52+
53+
if (testResponse.IsSuccessStatusCode == false)
54+
{
55+
// Invalid key will return a 401
56+
// Message property of response contains useful message
57+
// The API key provided is invalid. View or manage your API key here: https://app.hubspot.com/l/api-key/
58+
var errorResponse = testResponse.Content.ReadAsStringAsync().Result;
59+
var errorObj = JsonConvert.DeserializeObject<ErrorResponse>(errorResponse);
60+
errors.Add(new Exception(errorObj.message));
61+
}
62+
63+
return errors;
2864
}
2965
}
66+
67+
public class ErrorResponse
68+
{
69+
public string message { get; set; }
70+
}
3071
}

0 commit comments

Comments
 (0)