Api Changes

This commit is contained in:
2024-09-13 19:24:41 +02:00
parent 644a8e1899
commit d54c71a037
3 changed files with 68 additions and 42 deletions

View File

@@ -6,46 +6,45 @@ using System.Threading.Tasks;
using Jugenddienst_Stunden.Types; using Jugenddienst_Stunden.Types;
using ZXing.QrCode.Internal; using ZXing.QrCode.Internal;
namespace Jugenddienst_Stunden.Models namespace Jugenddienst_Stunden.Models {
{
class Auth { class Auth {
public Hours hours; public Hours hours;
public static async Task<string> GetApiDataWithAuthAsync(string url, string token) { public static async Task<string> GetApiDataWithAuthAsync(string url, string token) {
// Erstellen eines HttpClient-Objekts // Erstellen eines HttpClient-Objekts
using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) { using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
client.DefaultRequestHeaders.Add("Accept", "application/json"); client.DefaultRequestHeaders.Add("Accept", "application/json");
// Hinzufügen des Bearer-Tokens zum Authorization-Header // Hinzufügen des Bearer-Tokens zum Authorization-Header
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
//Beim Debugging im Lokalen Netz mit meinem Smartphone kommt es hier zu
//system.net.webexception nachricht = socket closed
//Grund: Falscher DNS-Server liefert falsche Server-IP
using (HttpResponseMessage HttpResponseMessage = await client.GetAsync(url).ConfigureAwait(false)) { //Beim Debugging im Lokalen Netz mit meinem Smartphone kommt es hier zu
if (HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK) { //system.net.webexception nachricht = socket closed
using (HttpContent HttpContent = HttpResponseMessage.Content) { //Grund: Falscher DNS-Server liefert falsche Server-IP
string responseData = await HttpContent.ReadAsStringAsync();
return responseData; using (HttpResponseMessage HttpResponseMessage = await client.GetAsync(url).ConfigureAwait(false)) {
} if (HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK) {
using (HttpContent HttpContent = HttpResponseMessage.Content) {
string responseData = await HttpContent.ReadAsStringAsync();
return responseData;
} }
} }
}
// Senden der Anfrage und Abrufen der Antwort // Senden der Anfrage und Abrufen der Antwort
//HttpResponseMessage response = await client.GetAsync(url); //HttpResponseMessage response = await client.GetAsync(url);
// Überprüfen, ob die Anfrage erfolgreich war // Überprüfen, ob die Anfrage erfolgreich war
//response.EnsureSuccessStatusCode(); //response.EnsureSuccessStatusCode();
// Lesen und Rückgabe der Antwort als String
//string responseData = await response.Content.ReadAsStringAsync();
//return responseData;
// Lesen und Rückgabe der Antwort als String
//string responseData = await response.Content.ReadAsStringAsync();
//return responseData;
} }
return null; return null;
} }

View File

@@ -84,5 +84,38 @@ namespace Jugenddienst_Stunden.Models
} }
public static async Task<Hours> LoadDay(DateTime date) {
Hours hours = new Hours();
if (Connectivity.Current.NetworkAccess == NetworkAccess.None) {
await App.Current.MainPage.DisplayAlert("Keine Internetverbindung",
"Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.",
"OK");
} else {
var tokendata = new TokenData(apiKey);
//string data = await Auth.GetApiDataWithAuthAsync(requestUrl, apiKey);
string data = await Auth.GetApiDataWithAuthAsync(tokendata.url + "?hours&date="+date.ToString("yyyy-MM-dd"), tokendata.apiKey);
if (data == null) {
throw new Exception("Keine Daten erhalten");
}
if (data == "\"Lalala\"") {
throw new Exception("Problem mit Token");
}
hours = JsonConvert.DeserializeObject<Hours>(data);
//Preferences.Default.Set("name", hours.operator_api.name);
//Preferences.Default.Set("surname", hours.operator_api.surname);
}
return hours;
}
} }
} }

View File

@@ -54,12 +54,13 @@ namespace Jugenddienst_Stunden.ViewModels {
public DateTime MinimumDate { public DateTime MinimumDate {
//get => _hour.MinDate; //get => _hour.MinDate;
get => DateTime.Today.AddDays(-21); //get => DateTime.Today.AddDays(-21);
get => DateTime.Today.AddDays(-365);
} }
public DateTime MaximumDate { public DateTime MaximumDate {
//get => _hour.MaxDate; //get => _hour.MaxDate;
get => DateTime.Today.AddDays(5); get => DateTime.Today.AddDays(60);
} }
private DateTime dateToday = DateTime.Today; private DateTime dateToday = DateTime.Today;
@@ -70,7 +71,7 @@ namespace Jugenddienst_Stunden.ViewModels {
dateToday = value; dateToday = value;
GetDay = dateToday.Day; GetDay = dateToday.Day;
OnPropertyChanged(); OnPropertyChanged();
_ = LoadData(); // Use discard operator to explicitly ignore the returned Task _ = LoadDay(value); // Use discard operator to explicitly ignore the returned Task
//RefreshProperties(); //RefreshProperties();
OnPropertyChanged(nameof(TimeDay)); OnPropertyChanged(nameof(TimeDay));
OnPropertyChanged(nameof(ShowDay)); OnPropertyChanged(nameof(ShowDay));
@@ -146,6 +147,14 @@ namespace Jugenddienst_Stunden.ViewModels {
public async Task LoadData() { public async Task LoadData() {
try { try {
_hour = await Models.Stunde.LoadData(); _hour = await Models.Stunde.LoadData();
} catch (Exception e) {
AlertEvent?.Invoke(this, e.Message);
}
}
public async Task LoadDay(DateTime date) {
try {
_hour = await Models.Stunde.LoadDay(date);
if (_hour.zeit_total_daily_api != null) { if (_hour.zeit_total_daily_api != null) {
TimeDay = _hour.zeit_total_daily_api.Where(static p => p.Day == GetDay).ToList() ?? new List<TimeDay> { new TimeDay { Day = GetDay, Hours = 0 } }; TimeDay = _hour.zeit_total_daily_api.Where(static p => p.Day == GetDay).ToList() ?? new List<TimeDay> { new TimeDay { Day = GetDay, Hours = 0 } };
RefreshProperties(); RefreshProperties();
@@ -154,21 +163,6 @@ namespace Jugenddienst_Stunden.ViewModels {
AlertEvent?.Invoke(this, e.Message); AlertEvent?.Invoke(this, e.Message);
} }
//if (_hour.zeit_total_daily_api != null) {
// try {
// TimeDay = _hour.zeit_total_daily_api.Where(static p => p.Day == GetDay).ToList() ?? new List<TimeDay> { new TimeDay { Day = GetDay, Hours = 0 } };
// RefreshProperties();
// } catch (Exception e) {
// AlertEvent?.Invoke(this, e.Message);
// }
//} else {
// await App.Current.MainPage.DisplayAlert("Fehler",
// "zeit_total_daily_api ist leer",
// "OK");
//}
} }
private void RefreshProperties() { private void RefreshProperties() {