using Crestron.SimplSharp.Net.Http; using PepperDash.Core; using System; namespace PepperDash.Essentials.Core; [Obsolete("Please use the builtin HttpClient class instead: https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines")] public class GenericHttpClient : Device, IBasicCommunication { private readonly HttpClient Client; /// /// Event raised when response is received /// public event EventHandler ResponseRecived; /// /// Constructor /// /// key of the device /// name of the device /// hostname for the HTTP client public GenericHttpClient(string key, string name, string hostname) : base(key, name) { Client = new HttpClient { HostName = hostname }; } /// /// SendText method /// /// the path to send the request to public void SendText(string path) { HttpClientRequest request = new HttpClientRequest(); string url = string.Format("http://{0}/{1}", Client.HostName, path); request.Url = new UrlParser(url); HttpClient.DISPATCHASYNC_ERROR error = Client.DispatchAsyncEx(request, Response, request); } /// /// SendText method /// /// format for the items /// items to format public void SendText(string format, params object[] items) { HttpClientRequest request = new HttpClientRequest(); string url = string.Format("http://{0}/{1}", Client.HostName, string.Format(format, items)); request.Url = new UrlParser(url); HttpClient.DISPATCHASYNC_ERROR error = Client.DispatchAsyncEx(request, Response, request); } /// /// SendTextNoResponse method /// /// format for the items /// items to format public void SendTextNoResponse(string format, params object[] items) { HttpClientRequest request = new HttpClientRequest(); string url = string.Format("http://{0}/{1}", Client.HostName, string.Format(format, items)); request.Url = new UrlParser(url); Client.Dispatch(request); } /// /// Response method /// /// response received from the HTTP client /// error status of the HTTP callback /// original HTTP client request private void Response(HttpClientResponse response, HTTP_CALLBACK_ERROR error, object request) { if (error == HTTP_CALLBACK_ERROR.COMPLETED) { var responseReceived = response; if (responseReceived.ContentString.Length > 0) { ResponseRecived?.Invoke(this, new GenericHttpClientEventArgs(responseReceived.ContentString, (request as HttpClientRequest).Url.ToString(), error)); } } } #region IBasicCommunication Members /// /// SendBytes method /// /// bytes to send public void SendBytes(byte[] bytes) { throw new NotImplementedException(); } #endregion #region ICommunicationReceiver Members /// /// BytesReceived event /// public event EventHandler BytesReceived; /// /// Connect method /// public void Connect() { throw new NotImplementedException(); } /// /// Disconnect method /// public void Disconnect() { throw new NotImplementedException(); } /// /// IsConnected property /// public bool IsConnected { get { return true; } } /// /// TextReceived event /// public event EventHandler TextReceived; #endregion } /// /// Represents a GenericHttpClientEventArgs /// public class GenericHttpClientEventArgs : EventArgs { /// /// Gets or sets the ResponseText /// public string ResponseText { get; private set; } /// /// Gets or sets the RequestPath /// public string RequestPath { get; private set; } /// /// Gets or sets the Error /// public HTTP_CALLBACK_ERROR Error { get; set; } /// /// Constructor /// /// response text /// request path /// error status public GenericHttpClientEventArgs(string response, string request, HTTP_CALLBACK_ERROR error) { ResponseText = response; RequestPath = request; Error = error; } }