Last active
November 18, 2018 21:55
-
-
Save fluxdigital/72b50b14d4d080cfa129958a060b93fa to your computer and use it in GitHub Desktop.
Custom Rest Host for Telligent Community that uses an API Key for access
This file contains hidden or 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
public class TelligentRestHost : Telligent.Evolution.Extensibility.Rest.Version1.RestHost | |
{ | |
private string _communityUrl = null; | |
private string _adminAPIKey = null; | |
private string _adminUserName = null; | |
private bool _impersonate = false; | |
private string _impersonateUsername = null; | |
private string _requestTimeoutSeconds = null; | |
public TelligentRestHost(string communityUrl, string adminUserName, string adminApiKey, bool impersonate, string requestTimeoutSeconds, string impersonateUsername = "") | |
{ | |
_communityUrl = communityUrl; | |
_adminAPIKey = adminApiKey; | |
_adminUserName = adminUserName; | |
_impersonate = impersonate; | |
_requestTimeoutSeconds = requestTimeoutSeconds; | |
_impersonateUsername = impersonateUsername; | |
} | |
public override void ApplyAuthenticationToHostRequest(System.Net.HttpWebRequest request, bool forAccessingUser) | |
{ | |
var adminKey = String.Format("{0}:{1}", _adminAPIKey, _adminUserName); | |
var adminKeyBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(adminKey)); | |
request.Headers.Add("Rest-User-Token", adminKeyBase64); | |
if (_impersonate) | |
{ | |
request.Headers.Add("Rest-Impersonate-User", _impersonateUsername); | |
} | |
} | |
public override string EvolutionRootUrl | |
{ | |
get { return _communityUrl; } | |
} | |
public string Name | |
{ | |
get { return "TelligentRestHost"; } | |
} | |
public override int GetTimeout | |
{ | |
get | |
{ | |
return GetTimeoutValue(); | |
} | |
} | |
public override int PostTimeout | |
{ | |
get | |
{ | |
return GetTimeoutValue(); | |
} | |
} | |
private int GetTimeoutValue() | |
{ | |
var requestTimeout = 10; | |
return Convert.ToInt32(TimeSpan.FromSeconds(requestTimeout).TotalMilliseconds); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment