-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseProgress.cs
124 lines (103 loc) · 4.29 KB
/
BaseProgress.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Web;
using FEE.Domain.Networking;
using Newtonsoft.Json;
using RestSharp;
namespace FEE.Domain.ProgressTracking
{
public class BaseProgress<T>
{
private static readonly string _apiKey = ConfigurationManager.AppSettings["HubSpotApiKey"];
public static void PostPropertiesByVid(string vid, List<T> progress, string propertyName)
{
try
{
var client = new RestClient(baseUrl: "https://api.hubapi.com");
var request = new RestRequest("https://api.hubapi.com/contacts/v1/contact/vid/{vid}/profile?hapikey={hapikey}");
request.AddParameter("application/json",
JsonConvert.SerializeObject(new
{
properties = new List<HubSpotPropertyForProgressTracking>
{
new HubSpotPropertyForProgressTracking
{
property = propertyName,
value = JsonConvert.SerializeObject(progress)
}
}
}), ParameterType.RequestBody);
request.AddUrlSegment("vid", vid);
request.AddUrlSegment("hapikey", _apiKey);
client.Post(request);
}
catch (Exception ex)
{
Debug.WriteLine("Hubspot Update Properties By Vid Failed: {0}, {1}", ex.Source, ex.InnerException);
}
}
///
/// This helper function sends data to the the HubSpot Forms API
///
/// HubSpot Portal ID, or 'HUB ID'
/// Unique ID for the form
/// Dictionary containing all of the field names/values
/// UserToken from the visitor's browser
/// IP Address of the visitor
/// Title of the page they visited
/// URL of the page they visited
///
///
public bool Post_To_HubSpot_FormsAPI(string intPortalID,
string strFormGUID,
List<T> progress,
string strHubSpotUTK,
string propertyField,
string strPageTitle,
string strPageURL,
ref string strMessage)
{
// Build Endpoint URL
string strEndpointURL = string.Format("https://forms.hubspot.com/uploads/form/v2/{0}/{1}", intPortalID, strFormGUID);
// Setup HS Context Object
Dictionary<string, string> hsContext = new Dictionary<string, string>();
hsContext.Add("hutk", strHubSpotUTK);
hsContext.Add("pageUrl", strPageURL);
hsContext.Add("ipAddress", HttpContext.Current?.Request.GetOriginalHostAddress());
hsContext.Add("pageName", strPageTitle);
string progressValue = HttpUtility.UrlEncode(JsonConvert.SerializeObject(progress));
// Serialize HS Context to JSON (string)
var strHubSpotContextJSON = JsonConvert.SerializeObject(hsContext);
// Create string with post data
string strPostData = "";
// Add dictionary values
strPostData += $"{propertyField}={progressValue}&";
// Append HS Context JSON
strPostData += "hs_context=" + HttpUtility.UrlEncode(strHubSpotContextJSON);
// Create web request object
System.Net.HttpWebRequest r = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strEndpointURL);
// Set headers for POST
r.Method = "POST";
r.ContentType = "application/x-www-form-urlencoded";
r.ContentLength = strPostData.Length;
r.KeepAlive = false;
// POST data to endpoint
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(r.GetRequestStream()))
{
try
{
sw.Write(strPostData);
}
catch (Exception ex)
{
// POST Request Failed
strMessage = ex.Message;
return false;
}
}
return true; //POST Succeeded
}
}
}