using System; using System.Collections.Generic; using System.Linq; using System.Text; using Crestron.SimplSharp; using Crestron.SimplSharp.Net.Http; using Crestron.SimplSharp.Net.Https; namespace PepperDash.Core.GenericRESTfulCommunications { /// /// Generic RESTful communication class /// public class GenericRESTfulClient { /// /// Boolean event handler /// public event EventHandler BoolChange; /// /// Ushort event handler /// public event EventHandler UshrtChange; /// /// String event handler /// public event EventHandler StringChange; /// /// Constructor /// public GenericRESTfulClient() { } /// /// Generic RESTful submit request /// /// /// /// /// /// /// public void SubmitRequest(string url, ushort port, ushort requestType, string contentType, string username, string password) { if (url.StartsWith("https:", StringComparison.OrdinalIgnoreCase)) { SubmitRequestHttps(url, port, requestType, contentType, username, password); } else if (url.StartsWith("http:", StringComparison.OrdinalIgnoreCase)) { SubmitRequestHttp(url, port, requestType, contentType, username, password); } else { OnStringChange(string.Format("Invalid URL {0}", url), 0, GenericRESTfulConstants.ErrorStringChange); } } /// /// Private HTTP submit request /// /// /// /// /// /// /// private void SubmitRequestHttp(string url, ushort port, ushort requestType, string contentType, string username, string password) { try { HttpClient client = new HttpClient(); HttpClientRequest request = new HttpClientRequest(); HttpClientResponse response; client.KeepAlive = false; if(port >= 1 || port <= 65535) client.Port = port; else client.Port = 80; var authorization = ""; if (!string.IsNullOrEmpty(username)) authorization = EncodeBase64(username, password); if (!string.IsNullOrEmpty(authorization)) request.Header.SetHeaderValue("Authorization", authorization); if (!string.IsNullOrEmpty(contentType)) request.Header.ContentType = contentType; request.Url.Parse(url); request.RequestType = (Crestron.SimplSharp.Net.Http.RequestType)requestType; response = client.Dispatch(request); CrestronConsole.PrintLine(string.Format("SubmitRequestHttp Response[{0}]: {1}", response.Code, response.ContentString.ToString())); if (!string.IsNullOrEmpty(response.ContentString.ToString())) OnStringChange(response.ContentString.ToString(), 0, GenericRESTfulConstants.ResponseStringChange); if (response.Code > 0) OnUshrtChange((ushort)response.Code, 0, GenericRESTfulConstants.ResponseCodeChange); } catch (Exception e) { //var msg = string.Format("SubmitRequestHttp({0}, {1}, {2}) failed:{3}", url, port, requestType, e.Message); //CrestronConsole.PrintLine(msg); //ErrorLog.Error(msg); CrestronConsole.PrintLine(e.Message); OnStringChange(e.Message, 0, GenericRESTfulConstants.ErrorStringChange); } } /// /// Private HTTPS submit request /// /// /// /// /// /// /// private void SubmitRequestHttps(string url, ushort port, ushort requestType, string contentType, string username, string password) { try { HttpsClient client = new HttpsClient(); HttpsClientRequest request = new HttpsClientRequest(); HttpsClientResponse response; client.KeepAlive = false; client.HostVerification = false; client.PeerVerification = false; var authorization = ""; if (!string.IsNullOrEmpty(username)) authorization = EncodeBase64(username, password); if (!string.IsNullOrEmpty(authorization)) request.Header.SetHeaderValue("Authorization", authorization); if (!string.IsNullOrEmpty(contentType)) request.Header.ContentType = contentType; request.Url.Parse(url); request.RequestType = (Crestron.SimplSharp.Net.Https.RequestType)requestType; response = client.Dispatch(request); CrestronConsole.PrintLine(string.Format("SubmitRequestHttp Response[{0}]: {1}", response.Code, response.ContentString.ToString())); if(!string.IsNullOrEmpty(response.ContentString.ToString())) OnStringChange(response.ContentString.ToString(), 0, GenericRESTfulConstants.ResponseStringChange); if(response.Code > 0) OnUshrtChange((ushort)response.Code, 0, GenericRESTfulConstants.ResponseCodeChange); } catch (Exception e) { //var msg = string.Format("SubmitRequestHttps({0}, {1}, {2}, {3}, {4}) failed:{5}", url, port, requestType, username, password, e.Message); //CrestronConsole.PrintLine(msg); //ErrorLog.Error(msg); CrestronConsole.PrintLine(e.Message); OnStringChange(e.Message, 0, GenericRESTfulConstants.ErrorStringChange); } } /// /// Private method to encode username and password to Base64 string /// /// /// /// authorization private string EncodeBase64(string username, string password) { var authorization = ""; try { if (!string.IsNullOrEmpty(username)) { string base64String = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(string.Format("{0}:{1}", username, password))); authorization = string.Format("Basic {0}", base64String); } } catch (Exception e) { var msg = string.Format("EncodeBase64({0}, {1}) failed:\r{2}", username, password, e); CrestronConsole.PrintLine(msg); ErrorLog.Error(msg); return "" ; } return authorization; } /// /// Protected method to handle boolean change events /// /// /// /// protected void OnBoolChange(bool state, ushort index, ushort type) { var handler = BoolChange; if (handler != null) { var args = new BoolChangeEventArgs(state, type); args.Index = index; BoolChange(this, args); } } /// /// Protected mehtod to handle ushort change events /// /// /// /// protected void OnUshrtChange(ushort value, ushort index, ushort type) { var handler = UshrtChange; if (handler != null) { var args = new UshrtChangeEventArgs(value, type); args.Index = index; UshrtChange(this, args); } } /// /// Protected method to handle string change events /// /// /// /// protected void OnStringChange(string value, ushort index, ushort type) { var handler = StringChange; if (handler != null) { var args = new StringChangeEventArgs(value, type); args.Index = index; StringChange(this, args); } } } }