You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Digitalisation_EPA_Serveur/EPAServeur/Commun/APIAccess.cs

71 lines
1.9 KiB

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://www.epa.apside.com/api";
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;
}
}
}