Login auch im Testmodus Bei falschem Token nur eine Meldung Exception bei falschem Token während Loadsettings abfangen
180 lines
6.9 KiB
C#
180 lines
6.9 KiB
C#
using Jugenddienst_Stunden.Types;
|
|
using Newtonsoft.Json;
|
|
using System.Collections.ObjectModel;
|
|
using System.Diagnostics;
|
|
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);
|
|
|
|
// Ü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();
|
|
User userData = System.Text.Json.JsonSerializer.Deserialize<User>(responseData) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
|
|
return userData;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static HoursBase 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() {
|
|
//Filename = Path.GetFileName(filename),
|
|
//Text = File.ReadAllText(filename),
|
|
Date = File.GetLastWriteTime(filename)
|
|
};
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Stundeneintrag speichern
|
|
/// </summary>
|
|
internal static async Task SaveItemAsync(string url, string token, DayTime item, bool isNewItem = false) {
|
|
|
|
|
|
//Uhrzeiten sollten sinnvolle Werte haben
|
|
if (item.TimeSpanVon == item.TimeSpanBis) {
|
|
throw new Exception("Beginn und Ende sind gleich");
|
|
}
|
|
|
|
if (item.TimeSpanBis < item.TimeSpanVon) {
|
|
throw new Exception("Ende ist vor Beginn");
|
|
}
|
|
|
|
TimeSpan span = TimeSpan.Zero;
|
|
span += item.TimeSpanBis - item.TimeSpanVon;
|
|
if (span.Hours > 10) {
|
|
//Hier vielleicht eine Abfrage, ob mehr als 10 Stunden gesund sind?
|
|
//Das müsste aber das ViewModel machen
|
|
}
|
|
|
|
//Gemeinde ist ein Pflichtfeld
|
|
if (item.GemeindeAktiv == null) {
|
|
throw new Exception("Gemeinde nicht gewählt");
|
|
}
|
|
//Projekt ist ein Pflichtfeld
|
|
if (item.ProjektAktiv == null) {
|
|
throw new Exception("Projekt nicht gewählt");
|
|
}
|
|
//Keine Beschreibung
|
|
if (string.IsNullOrEmpty(item.Description)) {
|
|
throw new Exception("Keine Beschreibung");
|
|
}
|
|
using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
|
|
//HttpClient client = new HttpClient();
|
|
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
|
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
|
|
|
//string json = JsonSerializer.Serialize<DayTime>(item);
|
|
string json = JsonConvert.SerializeObject(item);
|
|
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
|
|
HttpResponseMessage? response = null;
|
|
if (isNewItem)
|
|
response = await client.PostAsync(url, content);
|
|
else
|
|
response = await client.PutAsync(url, content);
|
|
|
|
if (!response.IsSuccessStatusCode) {
|
|
throw new Exception("Fehler beim Speichern " + response.Content);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
internal static async Task DeleteItemAsync(string url, string token) {
|
|
using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
|
|
|
|
//HttpClient client = new HttpClient();
|
|
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
|
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
|
|
|
HttpResponseMessage response = await client.DeleteAsync(url);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
throw new Exception("Fehler beim Löschen " + response.Content);
|
|
}
|
|
}
|
|
|
|
}
|