Ajout des fonctions communes

master
lchandellier 4 years ago
parent ea194447a5
commit 729764aacf
  1. 40
      BDAccess/APIAccess.cs
  2. 71
      Commun/APIAccess.cs
  3. 4
      Commun/BDaccess.cs
  4. 32
      Commun/Function.cs
  5. 29
      Commun/KeycloakAuthentificator.cs

@ -1,40 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Threading.Tasks;
namespace EPAServeur.BDAccess
{
public class APIAccess
{
private static APIAccess instance = null;
private string serveur = "http://localhost:3000";
private APIAccess()
{
}
public static APIAccess getInstance()
{
return instance;
}
public static List<T> requestAPI<T>(string query)
{
if (instance == null)
{
instance = new APIAccess();
}
List<T> result = new List<T>();
WebClient client = new WebClient();
result = JsonSerializer.Deserialize<List<T>>(client.DownloadString(instance.serveur+query));
return result;
}
}
}

@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace EPAServeur.BDAccess
{
public class APIAccess
{
private static APIAccess instance = null;
private string serveur = "http://localhost:3000";
private APIAccess()
{
}
public static APIAccess getInstance()
{
return instance;
}
public static List<T> requestGetAPI<T>(string query)
{
if (instance == null)
{
instance = new APIAccess();
}
List<T> result = new List<T>();
WebClient client = new WebClient();
result = JsonSerializer.Deserialize<List<T>>(client.DownloadString(instance.serveur+query));
return result;
}
public static List<T> requestPostAPI<T>(string query, List<KeyValuePair<string, string>> postParam = null)
{
if (instance == null)
{
instance = new APIAccess();
}
List<T> result = new List<T>();
WebClient client = new WebClient();
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
if (postParam != null)
{
foreach (KeyValuePair<string, string> arg in postParam)
{
client.QueryString.Add(arg.Key, arg.Value);
}
}
try
{
var data = client.UploadValues(query, "POST", client.QueryString);
result = JsonSerializer.Deserialize<List<T>>(UnicodeEncoding.UTF8.GetString(data));
}
catch
{
result = null;
}
return result;
}
}
}

@ -36,10 +36,10 @@ namespace EPAServeur.BDAccess
{
instance = new BDAccess();
}
instance.connection.Close();
instance.connection.Open();
MySqlCommand command = new MySqlCommand(query, instance.connection);
command.Connection.Open();
return command.ExecuteReader();
}

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EPAServeur.Commun
{
public class Function
{
private static Function instance = null;
private Function()
{
}
public static Function getInstance()
{
return instance;
}
public static string TransformNetDateTimeToSqlDate(DateTime date)
{
return date.ToString("yyyy'-'MM'-'dd' 'HH':'mm':'ss");
}
public static DateTime TransformSqlDateToNetDateTime(string date)
{
return new DateTime();
}
}
}

@ -0,0 +1,29 @@
using EPAServeur.BDAccess;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace EPAServeur.Commun
{
public class KeycloakAuthentificator
{
private static readonly string host = "http://localhost:8080/auth/realms/master/protocol/openid-connect/userinfo";
public static bool IsvalidToken(string token)
{
List<KeyValuePair<string, string>> param = new List<KeyValuePair<string, string>>();
param.Add(new KeyValuePair<string, string>("access_token", token));
if (APIAccess.requestPostAPI<KeyValuePair<string, string>>(host, param) == null)
{
return false;
}
else
{
return true;
}
}
}
}
Loading…
Cancel
Save