106 lines
4.1 KiB
C#
106 lines
4.1 KiB
C#
using Jugenddienst_Stunden.Types;
|
|
using Newtonsoft.Json;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
|
|
|
|
namespace Jugenddienst_Stunden.Models;
|
|
|
|
internal static class BaseFunc {
|
|
internal static async Task<string> GetApiDataWithAuthAsync(string url, string token) {
|
|
if (Connectivity.Current.NetworkAccess == NetworkAccess.None)
|
|
throw new Exception("Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.");
|
|
|
|
if (string.IsNullOrEmpty(token))
|
|
throw new Exception("Kein APIKEY, bitte zuerst Login durchführen");
|
|
|
|
// Erstellen eines HttpClient-Objekts
|
|
using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
|
|
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
|
|
|
// Hinzufügen des Bearer-Tokens zum Authorization-Header
|
|
client.DefaultRequestHeaders.Authorization =
|
|
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
|
|
|
// Senden der Anfrage und Abrufen der Antwort
|
|
using (HttpResponseMessage HttpResponseMessage = await client.GetAsync(url).ConfigureAwait(false)) {
|
|
var byteArray = await HttpResponseMessage.Content.ReadAsByteArrayAsync();
|
|
string responseData = Encoding.UTF8.GetString(byteArray);
|
|
//using (HttpContent HttpContent = HttpResponseMessage.Content) {
|
|
// //responseData = await HttpContent.ReadAsStringAsync();
|
|
//}
|
|
if (HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK) {
|
|
return responseData;
|
|
} else {
|
|
var options = new JsonDocumentOptions { AllowTrailingCommas = true };
|
|
using (JsonDocument doc = JsonDocument.Parse(responseData, options)) {
|
|
JsonElement root = doc.RootElement;
|
|
string message = root.GetProperty("message").GetString() ??
|
|
throw new Exception("Fehler: 'message' ist null.");
|
|
throw new Exception(message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
internal static async Task<User> AuthUserPass(string user, string pass, string url) {
|
|
var values = new Dictionary<string, string> { { "user", user }, { "pass", pass } };
|
|
|
|
var content = new FormUrlEncodedContent(values);
|
|
|
|
using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
|
|
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
|
|
|
// Senden der Anfrage und Abrufen der Antwort
|
|
using (HttpResponseMessage HttpResponseMessage =
|
|
await client.PostAsync(url, content).ConfigureAwait(false)) {
|
|
if (!HttpResponseMessage.IsSuccessStatusCode) {
|
|
//throw new Exception("Fehler beim Einloggen " + HttpResponseMessage.Content);
|
|
var byteArray = await HttpResponseMessage.Content.ReadAsByteArrayAsync();
|
|
string responseData = Encoding.UTF8.GetString(byteArray);
|
|
var options = new JsonDocumentOptions { AllowTrailingCommas = true };
|
|
using (JsonDocument doc = JsonDocument.Parse(responseData, options)) {
|
|
JsonElement root = doc.RootElement;
|
|
string message = root.GetProperty("message").GetString() ??
|
|
throw new Exception("Fehler: 'message' ist null.");
|
|
throw new Exception(message);
|
|
}
|
|
}
|
|
|
|
// Überprüfen, ob die Anfrage erfolgreich war
|
|
if (HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK) {
|
|
using (HttpContent HttpContent = HttpResponseMessage.Content) {
|
|
// Lesen und Rückgabe der Antwort als String
|
|
|
|
string responseData = await HttpContent.ReadAsStringAsync();
|
|
BaseResponse res = JsonConvert.DeserializeObject<BaseResponse>(responseData) ??
|
|
throw new Exception("Fehler beim Deserialisieren der Daten");
|
|
//User userData = System.Text.Json.JsonSerializer.Deserialize<User>(responseData) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
|
|
return res.user;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Notiz laden
|
|
/// </summary>
|
|
internal static Note Load(string filename) {
|
|
filename = System.IO.Path.Combine(FileSystem.AppDataDirectory, filename);
|
|
|
|
if (!File.Exists(filename))
|
|
throw new FileNotFoundException("Unable to find file on local storage.", filename);
|
|
|
|
return
|
|
new() { Date = File.GetLastWriteTime(filename) };
|
|
}
|
|
|
|
|
|
|
|
|
|
} |