diff --git a/Nuvolar.Base/Nuvolar.Base.csproj b/Nuvolar.Base/Nuvolar.Base.csproj index 082bd56..26afbfe 100644 --- a/Nuvolar.Base/Nuvolar.Base.csproj +++ b/Nuvolar.Base/Nuvolar.Base.csproj @@ -23,15 +23,24 @@ x86 - pdbonly + none true bin\Release\ TRACE prompt 4 + x86 + + + ..\packages\MySql.Data.6.9.12\lib\net45\MySql.Data.dll + + + ..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll + + @@ -41,8 +50,12 @@ - + + + + + \ No newline at end of file diff --git a/Nuvolar.Base/RESTApi.cs b/Nuvolar.Base/RESTApi.cs index fd78e2e..9d36044 100644 --- a/Nuvolar.Base/RESTApi.cs +++ b/Nuvolar.Base/RESTApi.cs @@ -1,12 +1,245 @@ -using System; +using DevExpress.Persistent.Base; +using MySql.Data.MySqlClient; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System; using System.Collections.Generic; +using System.Configuration; +using System.IO; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; -namespace Nuvolar.Base +namespace Nuvolar.Base.RestAPI + { - public class RESTApi + /// + /// Laravel RestAPI kezelő osztály + /// + public static class RestAPI { + private static string _UriBase; + private static string _CDB; + private static Dictionary _LoginInfo; + private static int _StatusCode; + private static string _StatusMessage; + private static string _ResponseMessage; + private static string _API_Token; + private static string _GeocodeAPIKey; + + public static int StatusCode + { + get { return _StatusCode; } + } + public static string StatusMessage + { + get { return _StatusMessage; } + } + public static string ResponseMessage + { + get { return _ResponseMessage; } + } + public static void Init(string _ConnectionString,string _database) + { + + _CDB = _database; + _ResponseMessage = ""; + _UriBase = ConfigurationManager.AppSettings["Uri"]; + _GeocodeAPIKey = ConfigurationManager.AppSettings["GeocodeAPIKey"]; + + MySqlConnection _MySqlConnection = new MySqlConnection(_ConnectionString); + MySqlCommand _MySqlCommand = new MySqlCommand("SELECT api_token FROM Users WHERE email='sysadmin@emango.com'", _MySqlConnection); + MySqlDataReader _MySqlDataReader = _MySqlCommand.ExecuteReader(); + bool _Exists = false; + if (_MySqlDataReader.HasRows) + { + while (_MySqlDataReader.Read()) + { + _API_Token = _MySqlDataReader["api_token"] as string; + _Exists = true; + } + } + + if (_Exists) + { + if (String.IsNullOrEmpty(_API_Token)) + { + + _LoginInfo = new Dictionary { { "email", "sysadmin@emango.com" }, + { "password", "SysAdmin" }}; + + var json = ExecuteRootePOST("login", false, JsonConvert.SerializeObject(_LoginInfo)); + + if (_StatusCode == 200) + { + var dd = JsonConvert.DeserializeObject(json); + var dict = JObject.Parse(json).SelectToken("data").ToObject>(); + _API_Token = dict["api_token"]; + } + } + } + } + public static string ExecuteRootePOST(string _RooteString, bool _NeedAuthentication, string _postData) + { + _StatusCode = 500; + _StatusMessage = "Not respond"; + string json = ""; + + + Tracing.Tracer.LogText(_UriBase + _RooteString); + + var _Uri = new Uri(_UriBase + _RooteString, UriKind.Absolute); + var _WebRequest = WebRequest.Create(_Uri); + HttpWebRequest _HttpWebRequest = (HttpWebRequest)_WebRequest; + + _HttpWebRequest.Method = "POST"; + byte[] byteArray = Encoding.UTF8.GetBytes(_postData); + + + if (_NeedAuthentication) + { + _HttpWebRequest.PreAuthenticate = true; + _HttpWebRequest.Headers.Add("Authorization", "Bearer " + _API_Token); + _HttpWebRequest.Headers.Add("cdb", _CDB); + } + + _HttpWebRequest.ContentType = "application/json"; + _HttpWebRequest.Accept = "application/json"; + _HttpWebRequest.ContentLength = byteArray.Length; + Stream _dataStream = _HttpWebRequest.GetRequestStream(); + _dataStream.Write(byteArray, 0, byteArray.Length); + _dataStream.Close(); + + try + { + WebResponse _WebResponse = _WebRequest.GetResponse(); + _StatusCode = (int)((HttpWebResponse)_WebResponse).StatusCode; + _StatusMessage = ((HttpWebResponse)_WebResponse).StatusDescription; + var _responseStream = _WebResponse.GetResponseStream(); + if (_responseStream == null) return json; + + var _StreamReader = new StreamReader(_responseStream, Encoding.Default); + json = _StreamReader.ReadToEnd(); + + try + { + _StatusMessage = JsonConvert.DeserializeObject(json).ToString(); + } + catch (Exception) + { + + } + + + _responseStream.Close(); + _WebResponse.Close(); + } + catch (WebException ex) + { + + if (ex.Status == WebExceptionStatus.ProtocolError) + { + var _WebResponse = (HttpWebResponse)ex.Response; + _StatusCode = (int)_WebResponse.StatusCode; + _StatusMessage = ((HttpWebResponse)_WebResponse).StatusDescription; + } + } + + return json; + + } + public static string ExecuteRooteGET(string _RooteString, bool _NeedAuthentication) + { + string json = ""; + + Tracing.Tracer.LogText(_UriBase + _RooteString); + + var _Uri = new Uri(_UriBase + _RooteString, UriKind.Absolute); + var _WebRequest = WebRequest.Create(_Uri); + _WebRequest.Method = "GET"; + + + HttpWebRequest _HttpWebRequest = (HttpWebRequest)_WebRequest; + if (_NeedAuthentication) + { + _HttpWebRequest.PreAuthenticate = true; + _HttpWebRequest.Headers.Add("Authorization", "Bearer " + _API_Token); + _HttpWebRequest.Headers.Add("cdb", _CDB); + } + + _HttpWebRequest.Accept = "application/json"; + _HttpWebRequest.ContentType = "application/json"; + + try + { + WebResponse _WebResponse = _WebRequest.GetResponse(); + _StatusCode = (int)((HttpWebResponse)_WebResponse).StatusCode; + Stream _responseStream = _WebResponse.GetResponseStream(); + if (_responseStream == null) return json; + + StreamReader _StreamReader = new StreamReader(_responseStream, Encoding.Default); + json = _StreamReader.ReadToEnd(); + + _responseStream.Close(); + _WebResponse.Close(); + } + catch (WebException ex) + { + + if (ex.Status == WebExceptionStatus.ProtocolError) + { + var _WebResponse = (HttpWebResponse)ex.Response; + _StatusCode = (int)_WebResponse.StatusCode; + } + } + + + return json; + } + public static string ExecuteGOOGLEMAPSGET(string _RouteString) + { + string json = ""; + + Tracing.Tracer.LogText(_UriBase + _RouteString); + + + var _Uri = new Uri("https://maps.googleapis.com/maps/api/geocode/json?address=" + _RouteString + "&key=" + _GeocodeAPIKey, UriKind.Absolute); + var _WebRequest = WebRequest.Create(_Uri); + _WebRequest.Method = "GET"; + + + HttpWebRequest _HttpWebRequest = (HttpWebRequest)_WebRequest; + + _HttpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"; + _HttpWebRequest.ContentType = "application/json"; + _HttpWebRequest.Headers.Add("accept-language", "hu-HU,hu;q=0.9,en-US;q=0.8,en;q=0.7,pt;q=0.6"); + + try + { + WebResponse _WebResponse = _WebRequest.GetResponse(); + _StatusCode = (int)((HttpWebResponse)_WebResponse).StatusCode; + Stream _responseStream = _WebResponse.GetResponseStream(); + if (_responseStream == null) return json; + + StreamReader _StreamReader = new StreamReader(_responseStream, Encoding.Default); + json = _StreamReader.ReadToEnd(); + + _responseStream.Close(); + _WebResponse.Close(); + } + catch (WebException ex) + { + + if (ex.Status == WebExceptionStatus.ProtocolError) + { + var _WebResponse = (HttpWebResponse)ex.Response; + _StatusCode = (int)_WebResponse.StatusCode; + } + } + + + return json; + } } } diff --git a/Nuvolar.Base/app.config b/Nuvolar.Base/app.config new file mode 100644 index 0000000..0314dd4 --- /dev/null +++ b/Nuvolar.Base/app.config @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/Nuvolar.Base/packages.config b/Nuvolar.Base/packages.config new file mode 100644 index 0000000..c156400 --- /dev/null +++ b/Nuvolar.Base/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file