Created
October 3, 2017 17:42
-
-
Save StefanRiedmann/83e014dd480c5a4cadc181e87738a6b3 to your computer and use it in GitHub Desktop.
Test for creating an Infusionsoft Resthook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Net; | |
using System.Text; | |
using Newtonsoft.Json; | |
using ServiceStack; | |
using Xunit; | |
namespace Tests | |
{ | |
public class InfusionsoftHookRequest | |
{ | |
[JsonProperty("eventKey")] | |
public string EventKey { get; set; } | |
[JsonProperty("hookUrl")] | |
public string HookUrl { get; set; } | |
} | |
public class Temp | |
{ | |
private string _accessToken = "abc"; | |
private string _infusionsoftUrl = "https://api.infusionsoft.com/crm/rest/v1/hooks"; | |
private string _callbackUrl = "http://www.mypage.com/api/infusionsoft/hookcallback"; | |
[Fact] | |
public void CreateWebhookTest() | |
{ | |
var hookObj = new InfusionsoftHookRequest | |
{ | |
EventKey = "contact.edit", | |
HookUrl = _callbackUrl | |
//HookUrl = HttpUtility.UrlEncode(_callbackUrl) //I tried both ways | |
}; | |
var data = JsonConvert.SerializeObject(hookObj); | |
var request = (HttpWebRequest)WebRequest.Create(_infusionsoftUrl); | |
request.Method = "POST"; | |
request.KeepAlive = true; | |
request.ContentType = "application/x-www-form-urlencoded"; | |
//request.ContentType = "application/json, */*"; //I tried both ways | |
request.Accept = "application/json, */*"; | |
var dataBytes = Encoding.UTF8.GetBytes(data); | |
using (var reqStream = request.GetRequestStream()) | |
{ | |
reqStream.Write(dataBytes, 0, dataBytes.Length); | |
} | |
request.Headers[HttpRequestHeader.Authorization] = $"Bearer {_accessToken}"; | |
string resultJson; | |
try | |
{ | |
using (var response = request.GetResponse()) | |
{ | |
var sr = new StreamReader(response.GetResponseStream()); | |
resultJson = response.ReadToEnd(); | |
sr.Close(); | |
} | |
} | |
catch (Exception e) | |
{ | |
Debug.WriteLine($"Error creating webhook: {e.Message}"); | |
return; | |
} | |
Debug.WriteLine($"CreateWebhook result: {resultJson}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment