using gehGassiApp.Core.Interfaces;
using System.Text;
using System.Text.Json;
using gehGassi.Dto;
using gehGassiApp.Domain.Authentication;
using gehGassiApp.Domain.Users;
using gehGassiApp.Core.Helper;
using System.Net.Http.Headers;
using gehGassi.Dto.News;
using gehGassiApp.Core.Resources;
using gehGassiApp.Domain.News;
using System.Web;
using System.Globalization;
using System.Net;
using System.Net.Http.Json;
using gehGassi.Dto.Banners;
using gehGassi.Dto.Common;
using gehGassi.Dto.Listings;
using gehGassi.Dto.Lookup;
using gehGassi.Dto.Messages;
using gehGassiApp.Domain.Banners;
using gehGassiApp.Domain.Common;
using gehGassiApp.Domain.Listings;
using gehGassiApp.Domain.Lookup;
using gehGassiApp.Domain.Messages;
using gehGassiApp.Domain.Advertisements;
using gehGassi.Dto.Advertisements;
using gehGassi.Dto.Dogs;
using gehGassi.Dto.DogWalkers;
using gehGassi.Dto.Walks;
using gehGassiApp.Domain.Dogs;
using gehGassiApp.Domain.Walks;
using gehGassi.Dto.DogOwners;
using gehGassi.Dto.Favourites;
using gehGassi.Dto.Feedback;
using gehGassi.Dto.Payment;
using gehGassi.Dto.Pushnotifications;
using gehGassi.Dto.Ratings;
using gehGassi.Dto.Reporting;
using gehGassiApp.Core.Mapper;
using gehGassiApp.Domain.AppVersions;
using gehGassiApp.Domain.Favourites;
using gehGassiApp.Domain.Feedback;
using gehGassiApp.Domain.Payment;
using gehGassiApp.Domain.Vouchers;
using gehGassiApp.Domain.Walkers;
using gehGassi.Dto.Vouchers;
using gehGassiApp.Domain.Subscriptions;
using gehGassi.Dto.Subscriptions;
using Platform = gehGassiApp.Domain.Common.Platform;
namespace gehGassiApp.Core.Services
{
///
/// Service der die Kommunikation mit dem Server ermöglicht
///
public class CommunicationService : ICommunicationService
{
private readonly ISettingsService _settingsService;
private readonly IAppReportingService _appReportingService;
private readonly HttpClient _httpClient;
private readonly JsonSerializerOptions _jsonOptions;
private DateTimeOffset? _lastOnlineCheck;
private bool _isOnline;
private bool _hasInternet;
///
/// Erstellt eine Instanz
///
/// Basisadresse des Servers
/// Instanz eines ISettingsService
/// Instanz eines IAppReportingService
public CommunicationService(string baseAddress, ISettingsService settingsService, IAppReportingService appReportingService)
{
_settingsService = settingsService;
_appReportingService = appReportingService;
//Sonderfall wegen lokaler Entwicklung und Verbindung zu localhost --> Android & Windows
#if DEBUG
var devSslHelper = new DevHttpsConnectionHelper(new Uri(baseAddress).Port);
_httpClient = devSslHelper.HttpClient;
_httpClient.BaseAddress = new Uri(devSslHelper.DevServerRootUrl);
#else
_httpClient = new HttpClient();
_httpClient.BaseAddress = new Uri(baseAddress);
#endif
_httpClient.DefaultRequestHeaders.Add(Common.Constants.HasHeader, Common.Constants.HasHeaderValue);
_jsonOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
_lastOnlineCheck = null;
_isOnline = true;
_hasInternet = true;
}
///
/// Prüft ob eine Verbindung zum Server besteht
///
/// true wenn verbunden, false sonst
public async Task IsConnected()
{
//var appSettings = _settingsService.GetAppSettings();
var oldOnlineStatus = _isOnline;
try
{
SetClientTime();
//return false; //Um die Verbindung zu faken....
//var accessType = Connectivity.Current.NetworkAccess;
_hasInternet = Connectivity.Current.NetworkAccess == Microsoft.Maui.Networking.NetworkAccess.Internet;
if (_hasInternet)
{
//Wenn nicht Online, oder noch kein Onlinecheck durchgeführt wurde, oder der letzte Check länger als OnlineCheckDelay her ist
if (!_isOnline)
{
System.Diagnostics.Debug.WriteLine($"Check Server online status at {DateTimeOffset.UtcNow}");
using var cts = new CancellationTokenSource(Common.Constants.IsConnectedTimeout);
var result = await _httpClient.GetAsync($"api/Status/IsOnline", cts.Token).ConfigureAwait(false);
result.EnsureSuccessStatusCode();
var success = (bool.Parse(await result.Content.ReadAsStringAsync(cts.Token)));
_isOnline = success;
_lastOnlineCheck = DateTimeOffset.UtcNow;
}
}
else
{
_isOnline = false;
_lastOnlineCheck = null;
}
}
catch (Exception ex)
{
//Wird aufgerufen wenn ein Fehler bei der Abfrage am Server vorhanden ist...
_hasInternet = Connectivity.Current.NetworkAccess == Microsoft.Maui.Networking.NetworkAccess.Internet;
_isOnline = false;
_lastOnlineCheck = null;
System.Diagnostics.Debug.WriteLine(ex.Message);
}
//var onlineStatus = new OnlineStatusMessage() { WasOnline = oldOnlineStatus, IsOnline = _isOnline && _hasInternet };
//WeakReferenceMessenger.Default.Send(new OnlineStatusChangedMessage(onlineStatus));
return _isOnline && _hasInternet;
}
#region Helper für DebugInfo
public bool GetIsOnline()
{
return _isOnline;
}
public bool GetHasInternet()
{
return _hasInternet;
}
public DateTimeOffset? GetLastOnlineCheck()
{
return _lastOnlineCheck;
}
public Microsoft.Maui.Networking.NetworkAccess GetCurrentNetworkAccess()
{
return Connectivity.Current.NetworkAccess;
}
#endregion
#region Registrierung
///
/// Prüfen ob ein Benutzername noch verfügbar ist und ob das Passwort ausreicht
///
/// Benutzername
/// Passwort
/// CancellationToken
/// true wenn alles passt, false sonst
public async Task> RegisterCheckAvailableAsync(string userName, string password, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
var loginDto = new LoginDto() { UserName = userName, Password = password };
var json = JsonSerializer.Serialize(loginDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/Account/IsAvailable", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Register_Email_Exists:
result.ErrorMessage = Errors.Register_Email_Exists;
break;
case CommunicationErrors.Register_Password_Rules:
result.ErrorMessage = Errors.Register_Password_Rules;
break;
case CommunicationErrors.Register_Password_Pwned:
result.ErrorMessage = Errors.Register_Password_Pwned;
break;
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Login_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Prüfen ob ein Passwort ausreicht
///
/// Passwort
/// CancellationToken
/// true wenn alles passt, false sonst
public async Task> CheckPasswordAsync(string password, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
var checkDto = new CheckPasswordDto() { Password = password };
var json = JsonSerializer.Serialize(checkDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/Account/CheckPassword", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Register_Password_Rules:
result.ErrorMessage = Errors.Register_Password_Rules;
break;
case CommunicationErrors.Register_Password_Pwned:
result.ErrorMessage = Errors.Register_Password_Pwned;
break;
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Login_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Registrieren eines App-Users
///
/// Benutzername
/// Passwort
/// Land
/// CancellationToken
/// AppUser-Typ
/// Vorname
/// Nachname
/// Geburtsdatum
/// PLZ
/// Ort
/// Bundesland
/// Nationalität
/// Land Hauptwohnsitz
/// true wenn alles passt, false sonst
public async Task> RegisterAsync(string userName, string password, AppUserType appUserType, string firstName, string lastName, DateTimeOffset? birthDate, string zip, string city, string state, string country, string nationality, string mainResidence, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
var registerDto = new RegisterV2Dto()
{
UserName = userName,
Password = password,
AppUserType = (AppUserTypeDto)appUserType,
FirstName = firstName,
LastName = lastName,
BirthDate = birthDate,
Zip = zip,
City = city,
State = state,
CountryCode = country,
NationalityCode = nationality,
MainResidenceCountryCode = mainResidence
};
var json = JsonSerializer.Serialize(registerDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/v2/Account/Register", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Register_Email_Exists:
result.ErrorMessage = Errors.Register_Email_Exists;
break;
case CommunicationErrors.Register_Password_Rules:
result.ErrorMessage = Errors.Register_Password_Rules;
break;
case CommunicationErrors.Register_Password_Pwned:
result.ErrorMessage = Errors.Register_Password_Pwned;
break;
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Login_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Registrieren eines App-Users Extern
///
/// Benutzername
/// Passwort
/// Land
/// CancellationToken
/// AppUser-Typ
/// Vorname
/// Nachname
/// Geburtsdatum
/// PLZ
/// Ort
/// Bundesland
/// Token des externen Providers
/// Login-Provider
/// Nationalität
/// Land Hauptwohnsitz
/// true wenn alles passt, false sonst
public async Task> RegisterExternalAsync(string userName, string password, AppUserType appUserType, string firstName, string lastName, DateTimeOffset? birthDate, string zip, string city, string state, string country, string nationality, string mainResidence, string accessToken, string loginProvider, CancellationToken token)
{
var result = new CommunicationResult()
{
Success = false,
Value = null
};
try
{
SetClientTime();
var registerDto = new RegisterExternalV2Dto()
{
UserName = userName,
Password = password,
AppUserType = (AppUserTypeDto)appUserType,
FirstName = firstName,
LastName = lastName,
BirthDate = birthDate,
Zip = zip,
City = city,
State = state,
CountryCode = country,
AccessToken = accessToken,
LoginProvider = loginProvider,
NationalityCode = nationality,
MainResidenceCountryCode = mainResidence
};
var json = JsonSerializer.Serialize(registerDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/v2/Account/RegisterExternal", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
var userDto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var user = userDto.ToDomain();
SetIsOnline(true);
result.Success = true;
result.Value = user;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Register_Email_Exists:
result.ErrorMessage = Errors.Register_Email_Exists;
break;
case CommunicationErrors.Register_Password_Rules:
result.ErrorMessage = Errors.Register_Password_Rules;
break;
case CommunicationErrors.Register_Password_Pwned:
result.ErrorMessage = Errors.Register_Password_Pwned;
break;
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Login_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
#endregion
#region Token / Login
///
/// Anmelden eines Benutzers
///
/// Benutzername
/// Passwort
/// CancellationToken
/// true wenn erfolgreich, false sonst
public async Task> LoginAsync(string userName, string password, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
var loginDto = new LoginDto() { UserName = userName, Password = password };
var json = JsonSerializer.Serialize(loginDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/Account/Login", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var userDto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var user = userDto.ToDomain();
result.Success = true;
result.Value = user;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Login_Email_NotConfirmed:
result.ErrorMessage = Errors.Login_Email_NotConfirmed;
break;
case CommunicationErrors.Login_Invalid_Credentials:
result.ErrorMessage = Errors.Login_Invalid_Credentials;
break;
case CommunicationErrors.Login_LockedOut:
result.ErrorMessage = Errors.Login_LockedOut;
break;
case CommunicationErrors.Login_Assignment_Missing:
result.ErrorMessage = Errors.Login_Assignment_Missing;
break;
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Login_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Anmelden eines Benutzers mittels Acces-Token wegen external Provider
///
/// Access-Token
/// CancellationToken
/// CommunicationResult
public async Task> LoginExternalAsync(string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var serverResult = await _httpClient.GetAsync($"api/Account/LoginExternal", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var stringResult = await serverResult.Content.ReadAsStringAsync(token);
var userDto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var user = userDto.ToDomain();
result.Success = true;
result.Value = user;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Login_Email_NotConfirmed:
result.ErrorMessage = Errors.Login_Email_NotConfirmed;
break;
case CommunicationErrors.Login_Invalid_Credentials:
result.ErrorMessage = Errors.Login_Invalid_Credentials;
break;
case CommunicationErrors.Login_LockedOut:
result.ErrorMessage = Errors.Login_LockedOut;
break;
case CommunicationErrors.Login_Assignment_Missing:
result.ErrorMessage = Errors.Login_Assignment_Missing;
break;
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Login_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Anmelden eines Benutzers mittels Apple Provider
///
/// Login-Daten von Apple
/// CancellationToken
/// CommunicationResult
public async Task> LoginAppleAsync(LoginAppleDto dto, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
var json = JsonSerializer.Serialize(dto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/Account/LoginApple", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var userDto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var user = userDto.ToDomain();
result.Success = true;
result.Value = user;
}
else
{
if (serverResult.StatusCode == HttpStatusCode.NotFound)
{
//Sonderlösung damit wir das Refreshtoken erhalten
var resposeString = await serverResult.Content.ReadAsStringAsync(token);
var appleResponseDto = JsonSerializer.Deserialize(resposeString);
result.ErrorCode = CommunicationErrors.Common_NotFound;
result.ErrorMessage = appleResponseDto.RefreshToken;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Login_Email_NotConfirmed:
result.ErrorMessage = Errors.Login_Email_NotConfirmed;
break;
case CommunicationErrors.Login_Invalid_Credentials:
result.ErrorMessage = Errors.Login_Invalid_Credentials;
break;
case CommunicationErrors.Login_LockedOut:
result.ErrorMessage = Errors.Login_LockedOut;
break;
case CommunicationErrors.Login_Assignment_Missing:
result.ErrorMessage = Errors.Login_Assignment_Missing;
break;
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Login_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Abmelden eines Benutzers.
/// Löscht alle Refreshtokens des Benutzers
///
/// Access-Token
/// CancellationToken
///
public async Task> LogoutAsync(string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var serverResult = await _httpClient.GetAsync($"api/Account/Logout", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Login_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Zurücksetzen des Passworts eines Benutzers
///
/// Benutzername / Email
/// CancellationToken
/// CommunicationResult
public async Task> ResetPasswordAsync(string userName, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
var json = JsonSerializer.Serialize(userName, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/Account/ResetPassword", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Senden der E-Mail Bestätigung für einen Benutzer
///
/// Benutzername / Email
/// CancellationToken
/// CommunicationResult
public async Task> ResendConfirmationAsync(string userName, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
var json = JsonSerializer.Serialize(userName, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/Account/SendConfirmation", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Anmelden eines Benutzers mittels Refreshtoken
///
/// Aktuelles Accesstoken
/// Refreshtoken
/// CancellationToken
/// true wenn erfolgreich, false sonst
public async Task> RefreshTokenAsync(string accessToken, string refreshToken, CancellationToken token)
{
var appSettings = _settingsService.GetAppSettings();
var result = new CommunicationResult();
try
{
SetClientTime();
var dto = new RefreshTokenDto() { AccessToken = accessToken, RefreshToken = refreshToken };
var json = JsonSerializer.Serialize(dto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/Account/Refresh", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var refreshTokenDto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var tokenResponse = refreshTokenDto.ToDomain();
result.Success = true;
result.Value = tokenResponse;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.RefreshToken_NotFound:
result.ErrorMessage = Errors.RefreshToken_NotFound;
break;
case CommunicationErrors.RefreshToken_Expired:
result.ErrorMessage = Errors.RefreshToken_Expired;
break;
case CommunicationErrors.AccessToken_Empty:
result.ErrorMessage = Errors.AccessToken_Empty;
break;
case CommunicationErrors.RefreshToken_UserNotFound:
result.ErrorMessage = Errors.RefreshToken_UserNotFound;
break;
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Login_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
if (appSettings.LogConnection)
{
var infos = new Dictionary
{
{ "_isOnline", _isOnline.ToString() },
{ "_lastOnlineCheck", _lastOnlineCheck?.ToString() },
{ "_networkAccess", Connectivity.Current.NetworkAccess.ToString() },
{ "OnlineCheckDelay", _lastOnlineCheck.HasValue ? (DateTimeOffset.UtcNow - _lastOnlineCheck).Value.Seconds.ToString() : "_lastOnlineCheck NULL" },
{ "ErrorCode", result.ErrorCode.ToString()},
{ "Errormessage", result.ErrorMessage}
};
await _appReportingService.TrackEventAsync("RefreshTokenAsync Flo", infos);
}
}
}
catch (Exception ex)
{
result.ErrorCode = CommunicationErrors.Timeout;
result.ErrorMessage = ex.Message;
var error = $"{ex.Message} - {ex.StackTrace}";
if (appSettings.LogConnection)
{
var infos = new Dictionary
{
{ "_isOnline", _isOnline.ToString() },
{ "_lastOnlineCheck", _lastOnlineCheck?.ToString() },
{ "_networkAccess", Connectivity.Current.NetworkAccess.ToString() },
{ "OnlineCheckDelay", _lastOnlineCheck.HasValue ? (DateTimeOffset.UtcNow - _lastOnlineCheck).Value.Seconds.ToString() : "_lastOnlineCheck NULL" },
{ "Errormessage", ex.Message}
};
await _appReportingService.TrackEventAsync("RefreshTokenAsync Catch", infos);
}
}
return result;
}
///
/// Prüfen ob ein Benutzer ein Passwort hat
///
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> HasPasswordAsync(string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var serverResult = await _httpClient.GetAsync($"api/Account/HasPassword", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
var stringResult = await serverResult.Content.ReadAsStringAsync(token);
var success = bool.Parse(stringResult);
SetIsOnline(true);
result.Success = true;
result.Value = success;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Register_Password_Rules:
result.ErrorMessage = Errors.Register_Password_Rules;
break;
case CommunicationErrors.Register_Password_Pwned:
result.ErrorMessage = Errors.Register_Password_Pwned;
break;
case CommunicationErrors.Register_Password_Mismatch:
result.ErrorMessage = Errors.Register_Password_Mismatch;
break;
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Login_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Hinzufügen eines Passswortes zu einem Benutzer, wenn dieser keines hat
///
/// Benutzername
/// Passwort das hinzugefügt werden soll
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> AddPasswordAsync(string userName, string password, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
var loginDto = new AddPasswordDto() { UserName = userName, Password = password };
var json = JsonSerializer.Serialize(loginDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/Account/AddPassword", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
var stringResult = await serverResult.Content.ReadAsStringAsync(token);
var success = bool.Parse(stringResult);
SetIsOnline(true);
result.Success = true;
result.Value = success;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Register_Password_Rules:
result.ErrorMessage = Errors.Register_Password_Rules;
break;
case CommunicationErrors.Register_Password_Pwned:
result.ErrorMessage = Errors.Register_Password_Pwned;
break;
case CommunicationErrors.Register_Password_Mismatch:
result.ErrorMessage = Errors.Register_Password_Mismatch;
break;
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Login_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Ändern des Passwortes eines Benutzers
///
/// Benutzername
/// Passwort das gesetzt werden soll
/// Bisheriges Passwort
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> ChangePasswordAsync(string userName, string password, string oldPassword, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
var loginDto = new ChangePasswordDto() { UserName = userName, Password = password, OldPassword = oldPassword};
var json = JsonSerializer.Serialize(loginDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/Account/ChangePassword", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
var stringResult = await serverResult.Content.ReadAsStringAsync(token);
var success = bool.Parse(stringResult);
SetIsOnline(true);
result.Success = true;
result.Value = success;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Register_Password_Rules:
result.ErrorMessage = Errors.Register_Password_Rules;
break;
case CommunicationErrors.Register_Password_Pwned:
result.ErrorMessage = Errors.Register_Password_Pwned;
break;
case CommunicationErrors.Register_Password_Mismatch:
result.ErrorMessage = Errors.Register_Password_Mismatch;
break;
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Login_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Löschen des Accounts eines Benutzers
///
/// Benutzername
/// Id des Benutzers
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> DeleteAccountAsync(string userName, string userId, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
var deleteAccountDto = new DeleteAccountDto() { UserName = userName, UserId = userId};
var json = JsonSerializer.Serialize(deleteAccountDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/Account/DeleteAccount", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
var stringResult = await serverResult.Content.ReadAsStringAsync(token);
var success = bool.Parse(stringResult);
SetIsOnline(true);
result.Success = true;
result.Value = success;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.DeleteAccount_NotPossible;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.DeleteAccount_NotPossible;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
#endregion
#region App-User
///
/// Holt den App-User für den aktuell angemeldeten Benutzer
///
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> GetAppUserAsync(string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var serverResult = await _httpClient.GetAsync($"api/Account/GetAppUser", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var appUser = dto.ToDomain();
result.Success = true;
result.Value = appUser;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Login_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holt den App-User für den aktuell angemeldeten Benutzer Sync
///
/// Wann wurden das letzte mal Daten erfolgreich geholt? Wenn null, dann alle Daten
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> GetAppUserSyncAsync(DateTimeOffset? lastUpdate, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var lastUpdateString = HttpUtility.UrlEncode(lastUpdate.ToString());
if (lastUpdate.HasValue)
lastUpdateString = HttpUtility.UrlEncode(lastUpdate.Value.ToString("o"));
var serverResult = await _httpClient.GetAsync($"api/Account/GetAppUserSync?lastUpdate={lastUpdateString}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = null;
if (serverResult.StatusCode == HttpStatusCode.OK)
{
var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var appUser = dto.ToDomain();
result.Value = appUser;
}
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Login_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Aktualisieren eines App-Users am Server
///
/// App-User DTO
/// Foto-Datei
/// Aktuelles Accesstoken
/// CancellationToken
/// Dateiname Photo
/// CommunicationResult
public async Task> UpdateAppUserAsync(AppUserDto appUserDto, string photoFileName, byte[] photoFile, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(appUserDto, _jsonOptions);
var multipartContent = new MultipartFormDataContent();
multipartContent.Add(new StringContent(json, Encoding.UTF8, "application/json"), "model");
if (!string.IsNullOrEmpty(photoFileName) && photoFile != null)
{
multipartContent.Add(new ByteArrayContent(photoFile), "photoUpdateFile", photoFileName);
}
var serverResult = await _httpClient.PostAsync($"api/Account/Update", multipartContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Setzen des AppUser Types am Server.
/// Kann nur online erfolgen!
///
/// Dto für das Setzen des Typs
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> SetAppUserTypeAsync(SetAppUserTypeDto setAppUserTypeDto, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(setAppUserTypeDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/Account/SetAppUserType", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Setzen des AppUser Types am Server. Wenn hundebesitzer Walker wird
/// Kann nur online erfolgen!
///
/// Dto für das Setzen des Typs
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> SetAppUserTypeAsync(SetAppUserTypeExDto setAppUserTypeDto, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(setAppUserTypeDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/Account/SetAppUserTypeEx", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
if (serverResult.StatusCode == HttpStatusCode.OK)
{
var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var appUser = dto.ToDomain();
result.Value = appUser;
}
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Hinzufügen eines Payment Users am Server.
///
/// DTO für das Hinzufeügen des Payment-Users
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> AddPaymentUserAsync(AddPaymentUserDto addPaymentUserDto, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(addPaymentUserDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/Account/AddPaymentUser", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
if (serverResult.StatusCode == HttpStatusCode.OK)
{
var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var appUser = dto.ToDomain();
result.Value = appUser;
}
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Blockieren eines App-Users am Server
///
/// Dto für das Blockieren eines anderen App-Users
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> BlockAppUserAsync(BlockCreateDto blockDto, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(blockDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/Account/BlockAppUser", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
if (serverResult.StatusCode == HttpStatusCode.OK)
{
var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
result.Value = dto != null;
}
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Blockieren eines App-Users am Server aufheben
///
/// Dto für das Aufheben der Blockierung eines anderen App-Users
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> UnblockAppUserAsync(BlockRemoveDto unblockDto, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(unblockDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/Account/UnblockAppUser", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
if (serverResult.StatusCode == HttpStatusCode.OK)
{
var success = (bool.Parse(await serverResult.Content.ReadAsStringAsync(token)));
result.Value = success;
}
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen von blockierten App-Usern
///
/// Abfrageobjekt
/// Aktuelles Accesstoken
/// CancellationToken
/// ListCommunicationResult
public async Task>> GetBlockedAppUsersAsync(BlockedQueryDto query, string accessToken, CancellationToken token)
{
var result = new ListCommunicationResult>
{
Value = new List()
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(query, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/account/GetBlockedAppUsers", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var listResponseDto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var blockedAppUsers = listResponseDto.List.ToDomain();
result.Success = true;
result.Value = blockedAppUsers;
result.Total = listResponseDto.Total;
result.Skip = listResponseDto.Skip;
result.Take = listResponseDto.Take;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Melden eines App-Users
///
/// Dto für das Melden eines anderen App-Users
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> ReportAppUserAsync(AppUserReportDto reportDto, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(reportDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/Account/ReportAppUser", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
if (serverResult.StatusCode == HttpStatusCode.OK)
{
var success = (bool.Parse(await serverResult.Content.ReadAsStringAsync(token)));
result.Value = success;
}
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
#endregion
#region Walker Profile
///
/// Holt den das Walker Profil für den aktuell angemeldeten Benutzer
///
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> GetWalkerProfileAsync(string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var serverResult = await _httpClient.GetAsync($"api/Account/GetWalkerProfile", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var walkerProfile = dto.ToDomain();
result.Success = true;
result.Value = walkerProfile;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Login_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holt das Walker Profil für den aktuell angemeldeten Benutzer Sync!
///
/// Wann wurden das letzte mal Daten erfolgreich geholt? Wenn null, dann alle Daten
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> GetWalkerProfileSyncAsync(DateTimeOffset? lastUpdate, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var lastUpdateString = HttpUtility.UrlEncode(lastUpdate.ToString());
if (lastUpdate.HasValue)
lastUpdateString = HttpUtility.UrlEncode(lastUpdate.Value.ToString("o"));
var serverResult = await _httpClient.GetAsync($"api/Account/GetWalkerProfileSync?lastUpdate={lastUpdateString}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = null;
if (serverResult.StatusCode == HttpStatusCode.OK)
{
var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var walkerProfile = dto.ToDomain();
result.Value = walkerProfile;
}
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Login_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Aktualisieren eines Dogwalker Profils am Server
///
/// Profil DTO
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> UpdateWalkerProfileAsync(DogWalkerProfileDto walkerProfileDto, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(walkerProfileDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/Account/UpdateWalkerProfile", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
#endregion
#region DogOwners
///
/// Gibt Detailsinfos zu einem DogOwner zurück
///
/// Id des DogOwners
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> GetDogOwnerAsync(string dogOwnerId, string accessToken, CancellationToken token)
{
var result = new CommunicationResult
{
Value = new DogOwnerInfo()
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var param = HttpUtility.UrlEncode(dogOwnerId);
var serverResult = await _httpClient.GetAsync($"api/dogowners/GetDogOwner?dogOwnerId={param}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var responseDto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var item = responseDto.ToDomain();
result.Success = true;
result.Value = item;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
#endregion
#region DogWalkers
///
/// Gibt Detailsinfos zu einem Dogwalker zurück
///
/// Id des Dogwalkers
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> GetDogWalkerAsync(string dogWalkerId, string accessToken, CancellationToken token)
{
var result = new CommunicationResult
{
Value = new DogWalkerInfo()
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var param = HttpUtility.UrlEncode(dogWalkerId);
var serverResult = await _httpClient.GetAsync($"api/dogwalkers/GetDogWalker?dogWalkerId={param}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var responseDto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var item = responseDto.ToDomain();
result.Success = true;
result.Value = item;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen von Dogwalkern vom Server.
///
/// Abfrageobjekt
/// Aktuelles Accesstoken
/// CancellationToken
/// ListCommunicationResult
public async Task>> GetDogWalkersAsync(DogWalkerQueryDto query, string accessToken, CancellationToken token)
{
var result = new ListCommunicationResult>
{
Value = new List()
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(query, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/dogwalkers/GetDogWalkers", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var listResponseDto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var dogWalkers = listResponseDto.List.ToDomain();
result.Success = true;
result.Value = dogWalkers;
result.Total = listResponseDto.Total;
result.Skip = listResponseDto.Skip;
result.Take = listResponseDto.Take;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen von Dogwalkern vom Server. Als Lookup
///
/// Abfrageobjekt
/// Aktuelles Accesstoken
/// CancellationToken
/// ListCommunicationResult
public async Task>> GetDogWalkersLookupAsync(DogWalkerQueryDto query, string accessToken, CancellationToken token)
{
var result = new ListCommunicationResult>
{
Value = new List()
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(query, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/dogwalkers/GetDogWalkersLookup", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var listResponseDto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var dogWalkers = listResponseDto.List.ToDomain();
result.Success = true;
result.Value = dogWalkers;
result.Total = listResponseDto.Total;
result.Skip = listResponseDto.Skip;
result.Take = listResponseDto.Take;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen der Walkingtimes vom Server
///
/// Id des DogWalkers
/// Wann wurden das letzte mal Daten erfolgreich geholt? Wenn null, dann alle Daten
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetWalkingTimesForSyncAsync(string dogWalkerId, DateTimeOffset? lastUpdate, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>();
try
{
SetClientTime();
SetAccessToken(accessToken);
var param1 = HttpUtility.UrlEncode(dogWalkerId);
var lastUpdateString = HttpUtility.UrlEncode(lastUpdate.ToString());
if (lastUpdate.HasValue)
lastUpdateString = HttpUtility.UrlEncode(lastUpdate.Value.ToString("o"));
var serverResult = await _httpClient.GetAsync($"api/dogwalkers/GetWalkingTimesForSync?dogWalkerId={param1}&lastUpdate={lastUpdateString}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var list = dto.ToDomain();
result.Success = true;
result.Value = list;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Anlegen einer WalkingTime am Server
///
/// WalkingTime
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> CreateWalkingTimeAsync(WalkingTimeDto walkingTimeDto, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>()
{
Success = false
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(walkingTimeDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/dogwalkers/CreateWalkingTime", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var serverResponse = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var createResponse = new CreateResponse
{
Status = (CreateStatus)serverResponse.Status,
Value = serverResponse.Value.ToDomain()
};
result.Success = true;
result.Value = createResponse;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Aktualisieren einer WalkingTime am Server
///
/// WalkingTime DTO
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> UpdateWalkingTimeAsync(WalkingTimeDto walkingTimeDto, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(walkingTimeDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/dogwalkers/UpdateWalkingTime", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Löschen einer WalkingTime am Server
///
/// WalkingTime DTO
/// Id des App-Users
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> DeleteWalkingTimeAsync(WalkingTimeDto walkingTimeDto, string appUserId, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(walkingTimeDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var paramAppUserId = HttpUtility.UrlEncode(appUserId);
var serverResult = await _httpClient.PostAsync($"api/dogwalkers/DeleteWalkingTime?appUserId={paramAppUserId}", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Gibt alle WalkingTimes eins Dogwalkers vom Server zurück
///
/// ID des DogWalkers
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetAllWalkingTimesAsync(string dogWalkerId, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>();
try
{
SetClientTime();
SetAccessToken(accessToken);
var parameter = HttpUtility.UrlEncode(dogWalkerId);
var serverResult = await _httpClient.GetAsync($"api/dogwalkers/GetWalkingTimes?dogWalkerId={parameter}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var walkingTimes = dto.ToDomain();
result.Success = true;
result.Value = walkingTimes;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
#endregion
#region News & NewsCategories
///
/// Holen der News-Kategorien vom Server.
///
/// Wann wurden das letzte mal Daten erfolgreich geholt? Wenn null, dann alle Daten
/// Gewünschte Sprache
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetNewsCategoriesAsync(DateTimeOffset? lastUpdate, string language, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>();
try
{
SetClientTime();
SetAccessToken(accessToken);
var lastUpdateString = HttpUtility.UrlEncode(lastUpdate.ToString());
if(lastUpdate.HasValue)
lastUpdateString = HttpUtility.UrlEncode(lastUpdate.Value.ToString("o"));
var serverResult = await _httpClient.GetAsync($"api/news/GetCategories?lastUpdate={lastUpdateString}&language={language}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var categories = dto.ToDomain();
result.Success = true;
result.Value = categories;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen von News vom Server.
///
/// Abfrageobjekt
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetNewsAsync(NewsQueryDto query, string accessToken, CancellationToken token)
{
var result = new ListCommunicationResult>
{
Value = new List()
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(query, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/news/GetNews", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var listResponseDto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var news = listResponseDto.List.ToDomain();
result.Success = true;
result.Value = news;
result.Total = listResponseDto.Total;
result.Skip = listResponseDto.Skip;
result.Take = listResponseDto.Take;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
#endregion
#region Lookup
///
/// Gibt eine Liste von Dog- Ownern und Walkern basierend auf einem Filter zurück
///
/// Filter
/// Anzahl max. Datensätze
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> LookupEntitiesAsync(string filter, int take, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>
{
Value = new List()
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var filterUrl = HttpUtility.UrlEncode(filter);
var serverResult = await _httpClient.GetAsync($"api/lookup/Entities?filter={filterUrl}&take={take}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var listResponseDto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var items = listResponseDto.ToDomain();
result.Success = true;
result.Value = items;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Gibt eine Liste von Dogwalkern mit Distanz zur aktuellen Position zurück
///
/// Id die ignoriert werden soll falls der Benutzer DogOwner und DogWalker ist
/// Breitengrad
/// Längengrad
/// Anzahl Walker gewünscht
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> LookupDogWalkersAsync(string ignoreId, double lat, double lng, int take, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>
{
Value = new List()
};
var appSettings = _settingsService.GetAppSettings();
try
{
SetClientTime();
SetAccessToken(accessToken);
var latString = HttpUtility.UrlEncode(lat.ToString(CultureInfo.InvariantCulture));
var lngString = HttpUtility.UrlEncode(lng.ToString(CultureInfo.InvariantCulture));
var paramIgnoreId = HttpUtility.UrlEncode(ignoreId);
var serverResult = await _httpClient.GetAsync($"api/lookup/DogWalkers?ignoreId={paramIgnoreId}&lat={latString}&lng={lngString}&take={take}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var listResponseDto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var items = listResponseDto.ToDomain();
result.Success = true;
result.Value = items;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
if (appSettings.LogConnection)
{
var infos = new Dictionary
{
{ "_isOnline", _isOnline.ToString() },
{ "_lastOnlineCheck", _lastOnlineCheck?.ToString() },
{ "_networkAccess", Connectivity.Current.NetworkAccess.ToString() },
{ "OnlineCheckDelay", _lastOnlineCheck.HasValue ? (DateTimeOffset.UtcNow - _lastOnlineCheck).Value.Seconds.ToString() : "_lastOnlineCheck NULL" },
{ "ErrorCode", result.ErrorCode.ToString()},
{ "Errormessage", result.ErrorMessage}
};
await _appReportingService.TrackEventAsync("LookupDogWalkersAsync Flo", infos);
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
if (appSettings.LogConnection)
{
var infos = new Dictionary
{
{ "_isOnline", _isOnline.ToString() },
{ "_lastOnlineCheck", _lastOnlineCheck?.ToString() },
{ "_networkAccess", Connectivity.Current.NetworkAccess.ToString() },
{ "OnlineCheckDelay", _lastOnlineCheck.HasValue ? (DateTimeOffset.UtcNow - _lastOnlineCheck).Value.Seconds.ToString() : "_lastOnlineCheck NULL" },
{ "Errormessage", ex.Message}
};
await _appReportingService.TrackEventAsync("LookupDogWalkersAsync", infos);
}
}
return result;
}
///
/// Gibt Detailsinfos zu einem Dogwalker zurück
///
/// Id des Dogwalkers
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> LookupDogWalkerAsync(string dogWalkerId, string accessToken, CancellationToken token)
{
var result = new CommunicationResult
{
Value = new DogWalkerInfo()
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var param = HttpUtility.UrlEncode(dogWalkerId);
var serverResult = await _httpClient.GetAsync($"api/lookup/DogWalker?dogWalkerId={param}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var responseDto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var item = responseDto.ToDomain();
result.Success = true;
result.Value = item;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
#endregion
#region Messages
///
/// Holt eine Liste der Konversationen eines Benutzers
///
/// Id des Benutzers (DogOwner | DogWalker)
/// Letztes Update
/// Aktuelles Accesstoken
/// CancellationToken
/// Liste von Konversationen
public async Task>> GetConversationsAsync(string senderId, DateTimeOffset? lastUpdate, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>();
try
{
SetClientTime();
SetAccessToken(accessToken);
var lastUpdateString = HttpUtility.UrlEncode(lastUpdate.ToString());
if (lastUpdate.HasValue)
lastUpdateString = HttpUtility.UrlEncode(lastUpdate.Value.ToString("o"));
var serverResult = await _httpClient.GetAsync($"api/messages/GetConversations?senderId={senderId}&lastUpdate={lastUpdateString}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var conversations = dto.ToDomain();
result.Success = true;
result.Value = conversations;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holt eine Liste der Konversationen eines Benutzers mit Filter am Server.
/// Geblockte oder Gesperrte Benutzer werden nicht zurückgegeben.
///
/// Id des Benutzers (DogOwner | DogWalker)
/// Filter
/// Aktuelles Accesstoken
/// CancellationToken
/// Liste von Konversationen
public async Task>> GetConversationsWithFilterAsync(string senderId, string filter, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>();
try
{
SetClientTime();
SetAccessToken(accessToken);
var filterString = HttpUtility.UrlEncode(filter);
var serverResult = await _httpClient.GetAsync($"api/messages/GetConversationsEx?senderId={senderId}&filter={filterString}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var conversations = dto.ToDomain();
result.Success = true;
result.Value = conversations;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holt eine Konversationen eines Benutzers mit einem Ziel
///
/// Id des Benutzers (DogOwner | DogWalker)
/// Id des Empfängers
/// Aktuelles Accesstoken
/// CancellationToken
/// Liste von Konversationen
public async Task> GetConversationByReceipientAsync(string senderId, string receipientId, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var serverResult = await _httpClient.GetAsync($"api/messages/GetConversationByReceipient?senderId={senderId}&receipientId={receipientId}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var conversation = dto.ToDomain();
result.Success = true;
result.Value = conversation;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Anlegen einer Konversation am Server
///
/// Gewünschte Id der Konversation
/// Id des Senders
/// Id des Empfängers
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> CreateConversationAsync(string conversationId, string senderId, string receiverId, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>()
{
Success = false
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var createConversationDto = new CreateConversationDto()
{
Id = conversationId,
SenderId = senderId,
ReceiverId = receiverId,
};
var json = JsonSerializer.Serialize(createConversationDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/messages/CreateConversation", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var conversationDto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var createResponse = new CreateResponse
{
Status = (CreateStatus)conversationDto.Status,
Value = conversationDto.Value.ToDomain()
};
result.Success = true;
result.Value = createResponse;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holt eine Liste der offenen Nachrichten eines Benutzers
///
/// Id des Benutzers (DogOwner | DogWalker)
/// Letztes Update
/// Aktuelles Accesstoken
/// CancellationToken
/// Liste von Konversationen
public async Task>> GetMessagesAsync(string senderId, DateTimeOffset? lastUpdate, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>();
try
{
SetClientTime();
SetAccessToken(accessToken);
var lastUpdateString = HttpUtility.UrlEncode(lastUpdate.ToString());
if (lastUpdate.HasValue)
lastUpdateString = HttpUtility.UrlEncode(lastUpdate.Value.ToString("o"));
var serverResult = await _httpClient.GetAsync($"api/messages/GetMessages?senderId={senderId}&lastUpdate={lastUpdateString}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var messages = dto.ToDomain();
foreach (var message in messages)
{
message.Direction = MessageDirection.In;
message.Read = false;
}
result.Success = true;
result.Value = messages;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holt eine Liste der offenen Nachrichten eines Benutzers für eine Konversation
///
/// Id des Benutzers (DogOwner | DogWalker)
/// Id der Konversation
/// Letztes Update
/// Aktuelles Accesstoken
/// CancellationToken
/// Liste von Konversationen
public async Task>> GetMessagesAsync(string senderId, string conversationId, DateTimeOffset? lastUpdate, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>();
try
{
SetClientTime();
SetAccessToken(accessToken);
var lastUpdateString = HttpUtility.UrlEncode(lastUpdate.ToString());
if (lastUpdate.HasValue)
lastUpdateString = HttpUtility.UrlEncode(lastUpdate.Value.ToString("o"));
var serverResult = await _httpClient.GetAsync($"api/messages/GetMessagesByConversation?senderId={senderId}&conversationId={conversationId}&lastUpdate={lastUpdateString}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var messages = dto.ToDomain();
foreach (var message in messages)
{
message.Direction = MessageDirection.In;
message.Read = false;
}
result.Success = true;
result.Value = messages;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Bestätigen des erfolgreichen Erhalts von Nachrichten für einen Benutzer
///
/// Id des Benutzers (DogOwner | DogWalker)
/// Letztes Update
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> ConfirmMessagesAsync(string senderId, DateTimeOffset? lastUpdate, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var confirmation = new MessageConfirmationDto()
{
SenderId = senderId,
LastUpdate = lastUpdate,
ConversationId = string.Empty
};
var json = JsonSerializer.Serialize(confirmation, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/messages/ConfirmMessages", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Bestätigen des erfolgreichen Erhalts von Nachrichten für einen Benutzer für eine Konversation
///
/// Id des Benutzers (DogOwner | DogWalker)
/// Id der Konversation
/// Letztes Update
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> ConfirmMessagesAsync(string senderId, string conversationId, DateTimeOffset? lastUpdate, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var confirmation = new MessageConfirmationDto()
{
SenderId = senderId,
LastUpdate = lastUpdate,
ConversationId = conversationId
};
var json = JsonSerializer.Serialize(confirmation, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/messages/ConfirmMessagesByConversation", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Anlegen einer Nachricht am Server
///
/// Id des Benutzers (DogOwner | DogWalker)
/// Id des Benutzers Empfänger
/// Nachricht
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> AddMessageAsync(string senderId, string receiverId, Message message, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>()
{
Success = false
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var messageDto = message.ToDto();
messageDto.SenderId = senderId;
messageDto.ReceiverId = receiverId;
var json = JsonSerializer.Serialize(messageDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/messages/AddMessage", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var messageResponse = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var createResponse = new CreateResponse
{
Status = (CreateStatus)messageResponse.Status,
Value = messageResponse.Value.ToDomain()
};
createResponse.Value.Direction = MessageDirection.Out;
result.Success = true;
result.Value = createResponse;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen der Systemnachrichten vom Server für Sync
///
/// Id des AppUsers
/// Wann wurden das letzte mal Daten erfolgreich geholt? Wenn null, dann alle Daten
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetSystemMessagesForSyncAsync(string appUserId, DateTimeOffset? lastUpdate, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>();
try
{
SetClientTime();
SetAccessToken(accessToken);
var appUserIdString = HttpUtility.UrlEncode(appUserId);
var lastUpdateString = HttpUtility.UrlEncode(lastUpdate.ToString());
if (lastUpdate.HasValue)
lastUpdateString = HttpUtility.UrlEncode(lastUpdate.Value.ToString("o"));
var serverResult = await _httpClient.GetAsync($"api/messages/GetSystemMessagesForSync?appUserId={appUserIdString}&lastUpdate={lastUpdateString}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var list = dto.ToDomain();
result.Success = true;
result.Value = list;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Bestätigen des erfolgreichen Erhalts von SystemNachrichten für einen Benutzer
///
/// Id des Benutzers (DogOwner | DogWalker)
/// Letztes Update
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> ConfirmSystemMessagesAsync(string senderId, DateTimeOffset? lastUpdate, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var confirmation = new SystemMessageConfirmationDto()
{
SenderId = senderId,
LastUpdate = lastUpdate
};
var json = JsonSerializer.Serialize(confirmation, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/messages/ConfirmSystemMessages", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Entfernen von Systemnachrichten vom Server für einen AppUser und eine bestimmte Kombination aus Key und Table
///
/// Id des AppUsers
/// Typ des AppUsers
/// Key
/// Table
/// Alter als dieses Datum
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> RemoveSystemMessagesAsync(string appUserId, AppUserType appUserType, string key, string table, DateTimeOffset? created, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var confirmation = new SystemMessageRemoveDto()
{
AppUserId = appUserId,
AppUserType = (AppUserTypeDto) appUserType,
Key = key,
Table = table,
Created = created.Value
};
var json = JsonSerializer.Serialize(confirmation, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/messages/RemoveSystemMessages", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
#endregion
#region Listings
///
/// Holen der Branchen und Listungen Übersicht
///
/// Abfrageobjekt
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetListingsOverviewAsync(QueryDto query, string accessToken, CancellationToken token)
{
var result = new ListCommunicationResult>
{
Value = new List()
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(query, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/listings/GetOverview", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var listResponseDto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var listings = listResponseDto.List.ToDomain();
result.Success = true;
result.Value = listings;
result.Total = listResponseDto.Total;
result.Skip = listResponseDto.Skip;
result.Take = listResponseDto.Take;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen von Listungen nahe dem Benutzer vom Server.
///
/// Abfrageobjekt
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetListingsNearAsync(QueryDto query, string accessToken, CancellationToken token)
{
var result = new ListCommunicationResult>
{
Value = new List()
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(query, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/listings/GetNearListings", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var listResponseDto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var listings = listResponseDto.List.ToDomain();
result.Success = true;
result.Value = listings;
result.Total = listResponseDto.Total;
result.Skip = listResponseDto.Skip;
result.Take = listResponseDto.Take;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Gibt eine laufende Listung zurück
///
/// Id der Listung
/// gewünschte Sprache
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> GetListingRunningAsync(string id, string language, string accessToken, CancellationToken token)
{
var result = new CommunicationResult
{
Value = null
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var parameterId = HttpUtility.UrlEncode(id);
var parameterLanguage = HttpUtility.UrlEncode(language);
var serverResult = await _httpClient.GetAsync($"api/listings/GetRunningListing?id={parameterId}&language={parameterLanguage}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var listingDto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var listing = listingDto.ToDomain();
result.Success = true;
result.Value = listing;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen von Listungen je Branche mit Geo-Einschränkungen
///
/// Abfrageobjekt
/// Aktuelles Accesstoken
/// CancellationToken
/// ListCommunicationResult
public async Task>> GetListingsByBranchAsync(ListingQueryDto query, string accessToken, CancellationToken token)
{
var result = new ListCommunicationResult>
{
Value = new List()
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(query, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/listings/GetByBranch", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var listResponseDto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var listings = listResponseDto.List.ToDomain();
result.Success = true;
result.Value = listings;
result.Total = listResponseDto.Total;
result.Skip = listResponseDto.Skip;
result.Take = listResponseDto.Take;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
#endregion
#region Banners
///
/// Abfragen eines zufälligen Banners für eine Platzierung
///
/// Abfrageobjekt
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> GetBannerForLocationAsync(BannerQueryDto query, string accessToken, CancellationToken token)
{
var result = new CommunicationResult
{
Value = null
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(query, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/banners/GetBanner", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var responseDto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var banner = responseDto.ToDomain();
result.Success = true;
result.Value = banner;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Hinzufügen von Klicks zu einem Banner
///
/// Model für einen Klick für Banner
/// Aktuelles Accesstoken
/// CancellationToken
/// true wenn der Banner noch gültig ist, false sonst
public async Task> AddClicksAsync(BannerClickDto dtoModel, string accessToken, CancellationToken token)
{
var result = new CommunicationResult
{
Value = false
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(dtoModel, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/banners/ClickBanner", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var stringResult = await serverResult.Content.ReadAsStringAsync(token);
var responseDto = bool.Parse(stringResult);
result.Success = true;
result.Value = responseDto;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Hinzufügen von Views zu einem Banner
///
/// Model für einen View für Banner
/// Aktuelles Accesstoken
/// CancellationToken
/// true wenn der Banner noch gültig ist, false sonst
public async Task> AddViewsAsync(BannerViewDto dtoModel, string accessToken, CancellationToken token)
{
var result = new CommunicationResult
{
Value = false
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(dtoModel, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/banners/ViewBanner", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var stringResult = await serverResult.Content.ReadAsStringAsync(token);
var responseDto = bool.Parse(stringResult);
result.Success = true;
result.Value = responseDto;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
#endregion
#region Advertisements
///
/// Holen der Liste der Werbungs-Kategorien welche online sind
///
/// Gewünschte Sprache
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetAdvertisementCategoriesOnlineAsync(string language, string accessToken, CancellationToken token)
{
var result = new ListCommunicationResult>
{
Value = new List()
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var parameterLanguage = HttpUtility.UrlEncode(language);
var serverResult = await _httpClient.GetAsync($"api/advertisements/GetCategoriesOnline?language={parameterLanguage}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var categoriesResponseDto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var categories = categoriesResponseDto.ToDomain();
result.Success = true;
result.Value = categories;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen von ablaufenden Werbungen mit Geo-Einschränkungen
///
/// Abfrageobjekt
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetAdvertisementsNearEndAsync(QueryDto query, string accessToken, CancellationToken token)
{
var result = new ListCommunicationResult>
{
Value = new List()
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(query, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/advertisements/GetAdvertisementsNearEnd", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var listResponseDto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var advertisements = listResponseDto.List.ToDomain();
result.Success = true;
result.Value = advertisements;
result.Total = listResponseDto.Total;
result.Skip = listResponseDto.Skip;
result.Take = listResponseDto.Take;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Gibt eine laufende Werbung zurück
///
/// Id der Werbung
/// gewünschte Sprache
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> GetAdvertisementRunningAsync(string id, string language, string accessToken, CancellationToken token)
{
var result = new CommunicationResult
{
Value = null
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var parameterId = HttpUtility.UrlEncode(id);
var parameterLanguage = HttpUtility.UrlEncode(language);
var serverResult = await _httpClient.GetAsync($"api/advertisements/GetRunningAdvertisement?id={parameterId}&language={parameterLanguage}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var advertisementDto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var advertisement = advertisementDto.ToDomain();
result.Success = true;
result.Value = advertisement;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen von Werbungen je Kategorie mit Geo-Einschränkungen
///
/// Abfrageobjekt
/// Aktuelles Accesstoken
/// CancellationToken
/// ListCommunicationResult
public async Task>> GetAdvertisementsByCategoryAsync(AdvertisementQueryDto query, string accessToken, CancellationToken token)
{
var result = new ListCommunicationResult>
{
Value = new List()
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(query, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/advertisements/GetAdvertisementsByCategory", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var listResponseDto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var advertisements = listResponseDto.List.ToDomain();
result.Success = true;
result.Value = advertisements;
result.Total = listResponseDto.Total;
result.Skip = listResponseDto.Skip;
result.Take = listResponseDto.Take;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
#endregion
#region Dogs & Dogsraces
///
/// Holen der Hunderassen vom Server.
///
/// Wann wurden das letzte mal Daten erfolgreich geholt? Wenn null, dann alle Daten
/// Gewünschte Sprache
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetDogRacesAsync(DateTimeOffset? lastUpdate, string language, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>();
try
{
SetClientTime();
SetAccessToken(accessToken);
var lastUpdateString = HttpUtility.UrlEncode(lastUpdate.ToString());
if (lastUpdate.HasValue)
lastUpdateString = HttpUtility.UrlEncode(lastUpdate.Value.ToString("o"));
var serverResult = await _httpClient.GetAsync($"api/dogs/GetRaces?lastUpdate={lastUpdateString}&language={language}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var races = dto.ToDomain();
result.Success = true;
result.Value = races;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen einer Hunderassen vom Server.
///
/// Id der Hunderasse
/// Gewünschte Sprache
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> GetDogRaceAsync(string id, string language, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var parameter = HttpUtility.UrlEncode(id);
var parameter2 = HttpUtility.UrlEncode(language);
var serverResult = await _httpClient.GetAsync($"api/dogs/GetRace?id={parameter}&language={parameter2}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var dogRace = dto.ToDomain();
result.Success = true;
result.Value = dogRace;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen der Hunde vom Server
///
/// Id des AppUsers
/// Wann wurden das letzte mal Daten erfolgreich geholt? Wenn null, dann alle Daten
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetDogsAsync(string appUserId, DateTimeOffset? lastUpdate, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>();
try
{
SetClientTime();
SetAccessToken(accessToken);
var appUserIdString = HttpUtility.UrlEncode(appUserId);
var lastUpdateString = HttpUtility.UrlEncode(lastUpdate.ToString());
if (lastUpdate.HasValue)
lastUpdateString = HttpUtility.UrlEncode(lastUpdate.Value.ToString("o"));
var serverResult = await _httpClient.GetAsync($"api/dogs/GetDogs?appUserId={appUserIdString}&lastUpdate={lastUpdateString}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var dogs = dto.ToDomain();
result.Success = true;
result.Value = dogs;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen der Hunde vom Server als MinInfo
///
/// Id des AppUsers
/// Gewünschte Sprache
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetDogsMinAsync(string appUserId, string language, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>();
try
{
SetClientTime();
SetAccessToken(accessToken);
var appUserIdString = HttpUtility.UrlEncode(appUserId);
var languageString = HttpUtility.UrlEncode(language);
var serverResult = await _httpClient.GetAsync($"api/dogs/GetDogsMin?dogOwnerId={appUserIdString}&language={languageString}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var dogs = dto.ToDomain();
result.Success = true;
result.Value = dogs;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen eines Hundes vom Server
///
/// Id des Hundes
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> GetDogAsync(string dogId, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var parameter = HttpUtility.UrlEncode(dogId);
var serverResult = await _httpClient.GetAsync($"api/dogs/GetDog?dogId={parameter}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var dog = dto.ToDomain();
result.Success = true;
result.Value = dog;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Anlegen eines Hundes am Server
///
/// Hund DTO
/// Foto-Datei
/// Aktuelles Accesstoken
/// CancellationToken
/// Dateiname Photo
/// CommunicationResult
public async Task>> AddDogAsync(DogDto dogDto, string photoFileName, byte[] photoFile, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>()
{
Success = false
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(dogDto, _jsonOptions);
var multipartContent = new MultipartFormDataContent();
multipartContent.Add(new StringContent(json, Encoding.UTF8, "application/json"), "model");
if (!string.IsNullOrEmpty(photoFileName) && photoFile != null)
{
multipartContent.Add(new ByteArrayContent(photoFile), "photoUpdateFile", photoFileName);
}
var serverResult = await _httpClient.PostAsync($"api/dogs/Add", multipartContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var serverResponse = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var createResponse = new CreateResponse
{
Status = (CreateStatus)serverResponse.Status,
Value = serverResponse.Value.ToDomain()
};
result.Success = true;
result.Value = createResponse;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Aktualisieren eines Hundes am Server
///
/// Hund DTO
/// Foto-Datei
/// Aktuelles Accesstoken
/// CancellationToken
/// Dateiname Photo
/// CommunicationResult
public async Task> UpdateDogAsync(DogDto dogDto, string photoFileName, byte[] photoFile, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(dogDto, _jsonOptions);
var multipartContent = new MultipartFormDataContent();
multipartContent.Add(new StringContent(json, Encoding.UTF8, "application/json"), "model");
if (!string.IsNullOrEmpty(photoFileName) && photoFile != null)
{
multipartContent.Add(new ByteArrayContent(photoFile), "photoUpdateFile", photoFileName);
}
var serverResult = await _httpClient.PostAsync($"api/dogs/Update", multipartContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Löschen eines Hundes am Server
///
/// Hund DTO
/// Id des App-Users
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> DeleteDogAsync(DogDto dogDto, string appUserId, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(dogDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var paramAppUserId = HttpUtility.UrlEncode(appUserId);
var serverResult = await _httpClient.PostAsync($"api/dogs/Delete?appUserId={paramAppUserId}", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Gibt die Anzahl öffentlicher Anfragen für einen Benutzer zurück
///
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> CountPublicWalkRequestsAsync(string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var serverResult = await _httpClient.GetAsync($"api/walks/CountPublicWalkRequests", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var countString = await serverResult.Content.ReadAsStringAsync(token);
var count = int.Parse(countString);
result.Success = true;
result.Value = count;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
#endregion
#region Walks
///
/// Holen der öffentlichen Anfragen vom Server
///
/// Id des AppUsers
/// Wann wurden das letzte mal Daten erfolgreich geholt? Wenn null, dann alle Daten
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetPublicWalkRequestsForSyncAsync(string appUserId, DateTimeOffset? lastUpdate, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>();
try
{
SetClientTime();
SetAccessToken(accessToken);
var appUserIdString = HttpUtility.UrlEncode(appUserId);
var lastUpdateString = HttpUtility.UrlEncode(lastUpdate.ToString());
if (lastUpdate.HasValue)
lastUpdateString = HttpUtility.UrlEncode(lastUpdate.Value.ToString("o"));
var serverResult = await _httpClient.GetAsync($"api/walks/GetPublicWalkRequestsForSync?appUserId={appUserIdString}&lastUpdate={lastUpdateString}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var list = dto.ToDomain();
result.Success = true;
result.Value = list;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen der öffentlichen Anfragen vom Server für die letzten
///
/// Id des AppUsers
/// Gewünschte Sprache
/// Anzahl Datensätze
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetPublicWalkRequestsLatestAsync(string appUserId, string language, int take, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>();
try
{
SetClientTime();
SetAccessToken(accessToken);
var appUserIdString = HttpUtility.UrlEncode(appUserId);
var paramLanguage = HttpUtility.UrlEncode(language);
var serverResult = await _httpClient.GetAsync($"api/walks/GetPublicWalkRequestsLatest?appUserId={appUserIdString}&language={paramLanguage}&take={take}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var list = dto.ToDomain();
result.Success = true;
result.Value = list;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen von öffentlichen Anfragen vom Server.
///
/// Abfrageobjekt
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetPublicWalkRequestsAsync(PublicWalkRequestQueryDto query, string accessToken, CancellationToken token)
{
var result = new ListCommunicationResult>
{
Value = new List()
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(query, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/walks/GetPublicWalkRequests", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var listResponseDto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var listings = listResponseDto.List.ToDomain();
result.Success = true;
result.Value = listings;
result.Total = listResponseDto.Total;
result.Skip = listResponseDto.Skip;
result.Take = listResponseDto.Take;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen von öffentlichen Anfragen vom Server - erweitert
///
/// Abfrageobjekt
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetPublicWalkRequestsExAsync(PublicWalkRequestQueryExDto query, string accessToken, CancellationToken token)
{
var result = new ListCommunicationResult>
{
Value = new List()
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(query, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/walks/GetPublicWalkRequestsEx", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var listResponseDto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var listings = listResponseDto.List.ToDomain();
result.Success = true;
result.Value = listings;
result.Total = listResponseDto.Total;
result.Skip = listResponseDto.Skip;
result.Take = listResponseDto.Take;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen von öffentlichen Anfragen vom Server mit einem Antwortstatus für einen DogWalker
///
/// Abfrageobjekt
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetPublicWalkRequestsWithResponseStatusAsync(PublicWalkRequestAndResponseStatusQueryDto query, string accessToken, CancellationToken token)
{
var result = new ListCommunicationResult>
{
Value = new List()
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(query, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/walks/GetPublicWalkRequestsAndResponseStatus", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var listResponseDto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var listings = listResponseDto.List.ToDomain();
result.Success = true;
result.Value = listings;
result.Total = listResponseDto.Total;
result.Skip = listResponseDto.Skip;
result.Take = listResponseDto.Take;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen von öffentlichen Anfragen vom Server mit einem Antwortstatus für einen DogWalker mit erweiterten Suchparametern
///
/// Abfrageobjekt
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetPublicWalkRequestsWithResponseStatusExAsync(PublicWalkRequestAndResponseStatusQueryExDto query, string accessToken, CancellationToken token)
{
var result = new ListCommunicationResult>
{
Value = new List()
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(query, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/walks/GetPublicWalkRequestsAndResponseStatusEx", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var listResponseDto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var listings = listResponseDto.List.ToDomain();
result.Success = true;
result.Value = listings;
result.Total = listResponseDto.Total;
result.Skip = listResponseDto.Skip;
result.Take = listResponseDto.Take;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Holen einer öffntlichen Anfrage vom Server.
///
/// Id der Anfrage
/// Gewünschte Sprache
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> GetPublicWalkRequestWithNamesAsync(string id, string language, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var param = HttpUtility.UrlEncode(id);
var param2 = HttpUtility.UrlEncode(language);
var serverResult = await _httpClient.GetAsync($"api/walks/GetPublicWalkRequests?id={param}&language={param2}", token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
var request = dto.ToDomain();
result.Success = true;
result.Value = request;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Anlegen einer öffentlichen Anfrage am Server
///
/// Öffentliche Anfrage
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> CreatePublicWalkRequestAsync(PublicWalkRequestCreateDto requestDto, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>()
{
Success = false
};
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(requestDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/walks/CreatePublicWalkRequest", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var serverResponse = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token);
var createResponse = new CreateResponse
{
Status = (CreateStatus)serverResponse.Status,
Value = serverResponse.Value.ToDomain()
};
result.Success = true;
result.Value = createResponse;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Aktualisieren einer öffentlichen Anfrage am Server
///
/// Anfrage DTO
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> UpdatePublicWalkRequestAsync(PublicWalkRequestDto requestDto, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(requestDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/walks/UpdatePublicWalkRequest", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Löschen einer öffentlichen Anfrage am Server
///
/// Anfrage DTO
/// Id des App-Users
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> DeletePublicWalkRequestAsync(PublicWalkRequestDto requestDto, string appUserId, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(requestDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var paramAppUserId = HttpUtility.UrlEncode(appUserId);
var serverResult = await _httpClient.PostAsync($"api/walks/DeletePublicWalkRequest?appUserId={paramAppUserId}", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
result.Success = true;
result.Value = true;
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///
/// Auswählen eines Angebotes zu einer öffentlichen Anfrage am Server
///
/// Anfrage DTO
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> AcceptPublicWalkRequestAsync(PublicWalkRequestAcceptDto requestDto, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
try
{
SetClientTime();
SetAccessToken(accessToken);
var json = JsonSerializer.Serialize(requestDto, _jsonOptions);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var serverResult = await _httpClient.PostAsync($"api/walks/AcceptPublicWalkRequest", stringContent, token).ConfigureAwait(false);
if (serverResult.IsSuccessStatusCode)
{
SetIsOnline(true);
var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token);
result.Success = true;
result.Value = dto.ToDomain();
}
else
{
var errorCodeString = await serverResult.Content.ReadAsStringAsync(token);
var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString);
result.ErrorCode = errorCode;
switch (errorCode)
{
case CommunicationErrors.Common_NotFound:
result.ErrorMessage = Errors.Common_NotFound;
break;
case CommunicationErrors.Common_Api_HeaderMissing:
result.ErrorMessage = Errors.Api_HeaderMissing;
break;
case CommunicationErrors.Common_Model_Invalid:
result.ErrorMessage = Errors.Common_Undefined;
break;
default:
result.ErrorMessage = Errors.Common_Undefined;
result.ErrorCode = CommunicationErrors.Undefined;
break;
}
}
}
catch (Exception ex)
{
#if DEBUG
result.ErrorMessage = $"{ex.Message} - {ex.StackTrace}";
#else
result.ErrorMessage = Errors.Common_Undefined;
#endif
result.ErrorCode = CommunicationErrors.Undefined;
}
return result;
}
///