Api Changes
This commit is contained in:
@@ -6,46 +6,45 @@ using System.Threading.Tasks;
|
||||
using Jugenddienst_Stunden.Types;
|
||||
using ZXing.QrCode.Internal;
|
||||
|
||||
namespace Jugenddienst_Stunden.Models
|
||||
{
|
||||
namespace Jugenddienst_Stunden.Models {
|
||||
|
||||
class Auth {
|
||||
class Auth {
|
||||
|
||||
public Hours hours;
|
||||
|
||||
public static async Task<string> GetApiDataWithAuthAsync(string url, string token) {
|
||||
// Erstellen eines HttpClient-Objekts
|
||||
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
|
||||
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
||||
// Hinzufügen des Bearer-Tokens zum Authorization-Header
|
||||
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)) {
|
||||
if (HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK) {
|
||||
using (HttpContent HttpContent = HttpResponseMessage.Content) {
|
||||
string responseData = await HttpContent.ReadAsStringAsync();
|
||||
return responseData;
|
||||
}
|
||||
//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)) {
|
||||
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
|
||||
//HttpResponseMessage response = await client.GetAsync(url);
|
||||
// Senden der Anfrage und Abrufen der Antwort
|
||||
//HttpResponseMessage response = await client.GetAsync(url);
|
||||
|
||||
// Überprüfen, ob die Anfrage erfolgreich war
|
||||
//response.EnsureSuccessStatusCode();
|
||||
// Überprüfen, ob die Anfrage erfolgreich war
|
||||
//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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user