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.
ConsEco/Sources/Data/ClientAPI.cs

144 lines
4.7 KiB

using Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace Data
{
public static class ClientAPI
{
private const string ROOT_URL = "http://82.64.164.20:8888/";
//routes inscrit
private const string GET_INSCRITS_DATA_URL = ROOT_URL+"Inscrit/";
private const string POST_EMAIL_INSCRIT_DATA_URL = ROOT_URL+"Inscrit/FromMail/";
private const string PUT_PASSWORD_INSCRIT_DATA_URL = ROOT_URL+"Inscrit/UpdatePassword/";
private const string POST_ADD_INSCRIT_DATA_URL = ROOT_URL + "Inscrit/add/";
2 years ago
private const string DELETE_INSCRIT_DATA_URL = ROOT_URL + "Inscrit/delete/";
//routes banque
private const string GET_BANQUES_DATA_URL = ROOT_URL + "Banque/";
private const string POST_BANQUES_INSCRIT_DATA_URL = ROOT_URL + "Banque/FromId/";
private const string POST_ADD_BANQUE_INSCRIT_DATA_URL = ROOT_URL + "Banque/add/";
private const string DELETE_BANQUE_INSCRIT_DATA_URL = ROOT_URL + "Banque/delete/";
//add all routes
private static HttpClient cli = new HttpClient();
//Inscrit
public static async Task<List<Inscrit>> GetInscritsAsync()
{
HttpResponseMessage reponse = await cli.GetAsync(GET_INSCRITS_DATA_URL);
if(reponse.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<List<Inscrit>>(await reponse.Content.ReadAsStringAsync());
}
else
{
throw new HttpRequestException(reponse.StatusCode.ToString());
}
}
public static async Task<List<Inscrit>> GetInscritAsync(string email)
{
var dataBody = new Dictionary<string, string> { { "email", email } };
HttpResponseMessage reponse = await cli.PostAsJsonAsync(POST_EMAIL_INSCRIT_DATA_URL, dataBody);
if (reponse.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<List<Inscrit>>(await reponse.Content.ReadAsStringAsync());
}
else
{
throw new HttpRequestException(reponse.StatusCode.ToString());
}
}
public static async Task<bool> PutPasswordInscritAsync(string email, string password)
{
var dataBody = new Dictionary<string, string> { { "email", email }, { "password", password } };
HttpResponseMessage reponse = await cli.PutAsJsonAsync(PUT_PASSWORD_INSCRIT_DATA_URL, dataBody);
if (reponse.IsSuccessStatusCode)
{
return true;
}
else
{
throw new HttpRequestException(reponse.StatusCode.ToString());
}
}
public static async Task<bool> PostAddInscritAsync(string nom, string prenom, string email, string password)
{
var dataBody = new Dictionary<string, string> { { "nom", nom }, { "prenom", prenom }, { "email", email }, { "password", password } };
HttpResponseMessage reponse = await cli.PostAsJsonAsync(POST_ADD_INSCRIT_DATA_URL, dataBody);
if (reponse.IsSuccessStatusCode)
{
return true;
}
else
{
throw new HttpRequestException(reponse.StatusCode.ToString());
}
}
2 years ago
public static async Task<bool> DeleteInscritAsync(string email)
{
var dataBody = new Dictionary<string, string> { { "email", email } };
var reponse =
cli.SendAsync(
new HttpRequestMessage(HttpMethod.Delete, DELETE_INSCRIT_DATA_URL)
{
Content = new FormUrlEncodedContent(dataBody)
})
.Result;
if (reponse.IsSuccessStatusCode)
{
return true;
}
else
{
throw new HttpRequestException(reponse.StatusCode.ToString());
}
}
//Banque
public static async Task<List<Banque>> GetBanquesAsync()
{
HttpResponseMessage reponse = await cli.GetAsync(GET_BANQUES_DATA_URL);
if (reponse.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<List<Banque>>(await reponse.Content.ReadAsStringAsync());
}
else
{
throw new HttpRequestException(reponse.StatusCode.ToString());
}
}
}
}