Nuvolar.Base
This commit is contained in:
@@ -23,15 +23,24 @@
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<DebugType>none</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="DevExpress.ExpressApp.v17.2, Version=17.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
|
||||
<Reference Include="MySql.Data, Version=6.9.12.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MySql.Data.6.9.12\lib\net45\MySql.Data.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
@@ -41,8 +50,12 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="RESTApi.cs" />
|
||||
<Compile Include="RestAPI.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="app.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
+236
-3
@@ -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
|
||||
/// <summary>
|
||||
/// Laravel RestAPI kezelő osztály
|
||||
/// </summary>
|
||||
public static class RestAPI
|
||||
{
|
||||
private static string _UriBase;
|
||||
private static string _CDB;
|
||||
private static Dictionary<string, string> _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<string, string> { { "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<Dictionary<string, string>>();
|
||||
_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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<system.data>
|
||||
<DbProviderFactories>
|
||||
<remove invariant="MySql.Data.MySqlClient" />
|
||||
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.12.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
|
||||
</DbProviderFactories>
|
||||
</system.data>
|
||||
</configuration>
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="MySql.Data" version="6.9.12" targetFramework="net461" />
|
||||
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net461" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user