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/v3/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/v3/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/v2/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; } /// /// Stornieren einer öffentlichen Anfrage am Server /// /// Anfrage DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> CancelPublicWalkRequestAsync(PublicWalkRequestCancelDto 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/CancelPublicWalkRequest", 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; } /// /// Holen der Antworten zu ö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>> GetPublicWalkResponsesForSyncAsync(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/GetPublicWalkResponseForWalker?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 einer Antworten zu öffentlichen Anfragen vom Server /// /// Id der Antwort zur öffentlichen Anfrage /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> GetPublicWalkResponseAsync(string publicWalkResponseId, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var param = HttpUtility.UrlEncode(publicWalkResponseId); var serverResult = await _httpClient.GetAsync($"api/walks/GetPublicWalkResponse?publicWalkResponseId={param}", token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token); var response = dto.ToDomain(); result.Success = true; result.Value = response; } 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 Antworten zu öffentlichen Anfragen vom Server /// /// Id der Antwort zur öffentlichen Anfrage /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> GetPublicWalkResponseWithNamesAsync(string publicWalkResponseId, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var param = HttpUtility.UrlEncode(publicWalkResponseId); var serverResult = await _httpClient.GetAsync($"api/walks/GetPublicWalkResponseWithNames?publicWalkResponseId={param}", token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token); var response = dto.ToDomain(); result.Success = true; result.Value = response; } 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 Antworten zu öffentlichen Anfragen vom Server /// /// Id des AppUsers /// Id der öffentlichen Anfrage /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> GetPublicWalkResponseForWalkerAsync(string appUserId, string publicWalkRequestId, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var param = HttpUtility.UrlEncode(appUserId); var param2 = HttpUtility.UrlEncode(publicWalkRequestId); var serverResult = await _httpClient.GetAsync($"api/walks/GetPublicWalkResponseForWalker?appUserId={param}&publicWalkRequestId={param2}", token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token); var response = dto.ToDomain(); result.Success = true; result.Value = response; } 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 Antwort zu einer öffentlichen Anfrage am Server /// /// Antwort zur Öffentliche Anfrage /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task>> CreatePublicWalkResponseAsync(PublicWalkResponseDto responseDto, string accessToken, CancellationToken token) { var result = new CommunicationResult>() { Success = false, Value = new CreateResponse() { Status = CreateStatus.Error, Value = null } }; try { SetClientTime(); SetAccessToken(accessToken); var json = JsonSerializer.Serialize(responseDto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/walks/CreatePublicWalkResponse", stringContent, token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var serverResponse = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token); if (serverResponse.Status == CreateStatusDto.Success) { result.Success = true; result.Value.Status = (CreateStatus)serverResponse.Status; result.Value.Value = serverResponse.Value.ToDomain(); } else { result.Success = false; result.Value.Status = (CreateStatus)serverResponse.Status; result.Value.Value = 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; } /// /// Aktualisieren einer Antwort zu einer öffentlichen Anfrage am Server /// /// Anfrage DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> UpdatePublicWalkResponseAsync(PublicWalkResponseDto responseDto, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var json = JsonSerializer.Serialize(responseDto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/walks/UpdatePublicWalkResponse", 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 Antwort zu einer öffentlichen Anfrage am Server /// /// Anfrage DTO /// Id des App-Users /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> DeletePublicWalkResponseAsync(PublicWalkResponseDto responseDto, string appUserId, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var json = JsonSerializer.Serialize(responseDto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var paramAppUserId = HttpUtility.UrlEncode(appUserId); var serverResult = await _httpClient.PostAsync($"api/walks/DeletePublicWalkResponse?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; } /// /// Holen von Antworten zu einer öffentlichen Anfrage vom Server. /// /// Abfrageobjekt /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task>> GetPublicWalkresponsesAsync(PublicWalkResponseQueryDto 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/GetPublicWalkResponses", 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; } /// /// Ablehnen eines Angebotes zu einer öffentlichen Anfrage am Server /// /// Anfrage DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> DeclinePublicWalkResponseAsync(PublicWalkResponseDeclineDto 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/DeclinePublicWalkResponse", 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; } /// /// Bearbeiten eines bereits gestellten Angebotes für eine offene Anfrage /// /// Anfrage DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> EditPublicWalkResponseAsync(PublicWalkResponseEditDto 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/EditPublicWalkResponse", 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; case CommunicationErrors.Common_Entity_Changed: result.ErrorMessage = Errors.Common_Undefined; break; case CommunicationErrors.Common_Relation_Changed: 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; } /// /// Stornieren eines bereits gestellten Angebotes für eine offene Anfrage /// /// Anfrage DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> CancelPublicWalkResponseAsync(PublicWalkResponseCancelDto 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/CancelPublicWalkResponse", 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; case CommunicationErrors.Common_Entity_Changed: result.ErrorMessage = Errors.Common_Undefined; break; case CommunicationErrors.Common_Relation_Changed: 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 Walsk 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>> GetWalksForSyncAsync(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/GetWalksForSync?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; } /// /// Anlegen eines Walks Anfrage am Server /// /// Walk /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task>> CreateWalkAsync(WalkDto walkDto, string accessToken, CancellationToken token) { var result = new CommunicationResult>() { Success = false }; try { SetClientTime(); SetAccessToken(accessToken); var json = JsonSerializer.Serialize(walkDto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/walks/CreateWalk", 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 eines Walks am Server /// /// Walk DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> UpdateWalkAsync(WalkDto walkDto, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var json = JsonSerializer.Serialize(walkDto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/walks/UpdateWalk", 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 eines Walks am Server /// /// Walk DTO /// Id des App-Users /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> DeleteWalkAsync(WalkDto walkDto, string appUserId, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var json = JsonSerializer.Serialize(walkDto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var paramAppUserId = HttpUtility.UrlEncode(appUserId); var serverResult = await _httpClient.PostAsync($"api/walks/DeleteWalk?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; } /// /// Holen von Walks mit Namen vom Server. /// /// Abfrageobjekt /// Aktuelles Accesstoken /// CancellationToken /// ListCommunicationResult public async Task>> GetWalksWithNamesAsync(WalksQueryDto 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/GetWalks", 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 Liste der nächsten Walks, laufende, nicht abgeschlossene, nicht bezahlte usw. für einen Hundebesitzer zurück. /// /// Id des App-Users /// Datum ab dem gesucht /// Gewünschte Sprache /// Datensätze auslassen /// Datensätze nehmen /// Aktuelles Accesstoken /// CancellationToken /// ListCommunicationResult public async Task>> GetNextWalksOwnerAsync(string appUserId, DateTimeOffset date, string language, int take, int skip, string accessToken, CancellationToken token) { var result = new ListCommunicationResult> { Value = new List() }; try { SetClientTime(); SetAccessToken(accessToken); var query = new WalksNextQueryDto() { AppUserId = appUserId, Date = date, Language = language, Skip = skip, Take = take }; var json = JsonSerializer.Serialize(query, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/walks/GetNextWalksOwner", 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 Liste der nächsten Walks, laufende, nicht abgeschlossene, nicht bezahlte usw. für einen Walker zurück. /// /// Id des App-Users /// Datum ab dem gesucht /// Gewünschte Sprache /// Datensätze auslassen /// Datensätze nehmen /// Aktuelles Accesstoken /// CancellationToken /// ListCommunicationResult public async Task>> GetNextWalksWalkerAsync(string appUserId, DateTimeOffset date, string language, int take, int skip, string accessToken, CancellationToken token) { var result = new ListCommunicationResult> { Value = new List() }; try { SetClientTime(); SetAccessToken(accessToken); var query = new WalksNextQueryDto() { AppUserId = appUserId, Date = date, Language = language, Skip = skip, Take = take }; var json = JsonSerializer.Serialize(query, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/walks/GetNextWalksWalker", 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 Walks mit Namen vom Server. /// Sondersituation: Walker bekommen keine Walks unter dem Status Authorized zurück /// /// Abfrageobjekt /// Aktuelles Accesstoken /// CancellationToken /// ListCommunicationResult public async Task>> GetWalksWithNamesWalkerAsync(WalksQueryDto 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/GetWalksWalker", 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 eines Walks mit Namen vom Server /// /// ID des Walks /// Gewünschte Sprache /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> GetWalkWithNamesAsync(string walkId, string language, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var parameter = HttpUtility.UrlEncode(walkId); var parameter2 = HttpUtility.UrlEncode(language); var serverResult = await _httpClient.GetAsync($"api/walks/GetWalkWithNames?walkId={parameter}&language={parameter2}", token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token); var walk = dto.ToDomain(); result.Success = true; result.Value = walk; } 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 einen Walk zurück, welcher einer öffentlichen Anfrage zugeordnet ist /// /// ID der öffentlichen Anfrage /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> GetWalkByPublicRequestAsync(string publicWalkRequestId, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var parameter = HttpUtility.UrlEncode(publicWalkRequestId); var serverResult = await _httpClient.GetAsync($"api/walks/GetWalkByPublicRequest?publicWalkRequestId={parameter}", token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token); var walk = dto.ToDomain(); result.Success = true; result.Value = walk; } 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; } /// /// Stornieren eines Walks am Server /// /// Anfrage DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> CancelWalkAsync(WalkCancelDto 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/v2/walks/CancelWalk", 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; } /// /// Starten eines Walks am Server /// /// Anfrage DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> StartWalkAsync(WalkStartDto 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/StartWalk", 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; } /// /// abschließen eines Walks am Server /// /// Anfrage DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> CompleteWalkAsync(WalkCompleteDto 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/CompleteWalkEx", 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 eines Walks am Server /// /// Anfrage DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> ConfirmWalkAsync(WalkConfirmDto 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/v2/walks/ConfirmWalk", stringContent, token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var serverResponse = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token); result.Success = true; result.Value = serverResponse.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; } /// /// Bestätigen eines Walks am Server mit Rating /// /// Anfrage DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> ConfirmWalkWithRatingAsync(WalkConfirmWithRatingDto 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/v2/walks/ConfirmWalkWithRating", stringContent, token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var serverResponse = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token); result.Success = true; result.Value = serverResponse.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; } /// /// Setzen des Zahlungsstatus eines Walks am Server /// /// Anfrage DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> SetWalkPaymentStatusAsync(WalkPaymentStatusDto 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/SetPaymentStatus", 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; } /// /// Prüfen ob ein Walk für einen Zeitraum gebucht werden kann /// /// Anfrage DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> IsWalkPossibleAsync(WalkPossibleDto 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/IsWalkPossible", stringContent, token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { var success = (bool.Parse(await serverResult.Content.ReadAsStringAsync(token))); 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.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 eines Walks wenn DIREKT! buchen möglich ist. /// Wird online versucht und erst dann lokal gespeichert /// /// Anfrage DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task>> CreateWalkDirectAsync(WalkCreateDto 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/v2/walks/CreateWalkDirect", 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; case CommunicationErrors.Walk_NotPossible: result.ErrorMessage = Errors.Walk_NotPossible; 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 Walks als Anfrage /// Wird online versucht und erst dann lokal gespeichert /// /// Anfrage DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task>> CreateWalkRequestAsync(WalkCreateDto 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/v2/walks/CreateWalkRequest", 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; case CommunicationErrors.Walk_NotPossible: result.ErrorMessage = Errors.Walk_NotPossible; 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; } /// /// Akzeptieren eines Walks am Server /// /// Anfrage DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> AcceptWalkAsync(WalkAcceptDto 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/v2/walks/AcceptWalk", 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; } /// /// Ablehnen eines Walks am Server /// /// Anfrage DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> DeclineWalkAsync(WalkDeclineDto 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/DeclineWalk", 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; } /// /// Reklamieren eines Walks am Server /// /// Anfrage DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> ComplainWalkAsync(WalkComplaintCreateDto 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/CreateWalkComplaint", stringContent, token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token); SetIsOnline(true); 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.Walk_NotCompleted: result.ErrorMessage = Errors.Walk_NotCompleted; break; case CommunicationErrors.Walk_NotFound: result.ErrorMessage = Errors.Walk_NotFound; break; 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 einer Reklamation zu einem Walk /// /// ID des Walks /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> GetWalkComplaintAsync(string walkId, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var parameter = HttpUtility.UrlEncode(walkId); var serverResult = await _httpClient.GetAsync($"api/walks/GetWalkComplaint?walkId={parameter}", 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; 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 Ratings /// /// Holen der Ratings 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>> GetRatingsForSyncAsync(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/ratings/GetRatingsForSync?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; } /// /// Gibt ein Rating basierend auf Abfragekriterien zurück /// /// Rating check DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> GetRatingExAsync(RatingCheckQueryDto ratingCheckDto, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var json = JsonSerializer.Serialize(ratingCheckDto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/ratings/GetRatingEx", 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; } /// /// Gibt ein Rating basierend auf der ID zurück /// /// Id des Ratings /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> GetRatingAsync(string ratingId, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var ratingIdString = HttpUtility.UrlEncode(ratingId); var serverResult = await _httpClient.GetAsync($"api/ratings/GetRating?ratingId={ratingIdString}", 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 rating = dto.ToDomain(); result.Value = rating; } } 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; } /// /// Anlegen eines Ratings am Server /// /// Rating /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task>> CreateRatingAsync(RatingDto ratingDto, string accessToken, CancellationToken token) { var result = new CommunicationResult>() { Success = false }; try { SetClientTime(); SetAccessToken(accessToken); var json = JsonSerializer.Serialize(ratingDto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/ratings/CreateRating", 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 eines Ratings am Server /// /// Rating DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> UpdateRatingAsync(RatingDto ratingDto, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var json = JsonSerializer.Serialize(ratingDto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/ratings/UpdateRating", 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 eines Ratings am Server /// /// Rating DTO /// Id des App-Users /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> DeleteRatingAsync(RatingDto ratingDto, string appUserId, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var json = JsonSerializer.Serialize(ratingDto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var paramAppUserId = HttpUtility.UrlEncode(appUserId); var serverResult = await _httpClient.PostAsync($"api/ratings/DeleteRating?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; } /// /// Prüfen ob bereits ein Rating vorhanden ist /// /// Rating check DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> HasRatedAsync(RatingCheckQueryDto ratingCheckDto, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var json = JsonSerializer.Serialize(ratingCheckDto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/ratings/HasRated", stringContent, token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { var success = (bool.Parse(await serverResult.Content.ReadAsStringAsync(token))); SetIsOnline(true); result.Success = success; 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; } /// /// Prüfen ob ein Rating vorgenommen werden kann /// /// Rating check DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> CanRateAsync(RatingCheckQueryDto ratingCheckDto, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var json = JsonSerializer.Serialize(ratingCheckDto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/ratings/CanRate", stringContent, token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { var success = (bool.Parse(await serverResult.Content.ReadAsStringAsync(token))); SetIsOnline(true); result.Success = success; 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; } /// /// Holen von Ratings mit Namen vom Server. /// /// Abfrageobjekt /// Aktuelles Accesstoken /// CancellationToken /// ListCommunicationResult public async Task>> GetRatingsWithNamesAsync(RatingsQueryDto 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/ratings/GetRatings", 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 Feedback /// /// Anlegen eines Feedbacks am Server /// /// Feedback /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task>> CreateFeedbackAsync(AppFeedbackCreateDto dto, string accessToken, CancellationToken token) { var result = new CommunicationResult>() { Success = false }; try { SetClientTime(); SetAccessToken(accessToken); var json = JsonSerializer.Serialize(dto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/feedback/CreateFeedback", 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; } #endregion #region Favourites /// /// Holen der Favoriten 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>> GetFavouritesForSyncAsync(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/favourites/GetFavouritesForSync?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; } /// /// Anlegen eines Favoriten am Server /// /// Favorit /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task>> CreateFavouriteAsync(FavouriteDto favouriteDto, string accessToken, CancellationToken token) { var result = new CommunicationResult>() { Success = false }; try { SetClientTime(); SetAccessToken(accessToken); var json = JsonSerializer.Serialize(favouriteDto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/favourites/CreateFavourite", 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 eines Favoriten am Server /// /// Favorit DTO /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> UpdateFavouriteAsync(FavouriteDto favouriteDto, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var json = JsonSerializer.Serialize(favouriteDto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/favourites/UpdateFavourite", 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 eines Favoriten am Server /// /// Favorit DTO /// Id des App-Users /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> DeleteFavouriteAsync(FavouriteDto favouriteDto, string appUserId, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var json = JsonSerializer.Serialize(favouriteDto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var paramAppUserId = HttpUtility.UrlEncode(appUserId); var serverResult = await _httpClient.PostAsync($"api/favourites/DeleteFavourite?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; } /// /// Holen der Favoriten für einen Benutzer. Wird mit einem Objekt-Typen verwendet /// /// Abfrageobjekt /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task>> GetFavouritesListAsync(FavouriteListQueryDto query, string accessToken, CancellationToken token) { var result = new CommunicationResult> { 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/favourites/GetFavouritesList", stringContent, token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var listResponseDto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token); var favourites = listResponseDto.ToDomain(); result.Success = true; result.Value = favourites; } 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 Pushnotifications /// /// Registrieren / Aktualisieren eines Gerätes für Pushnotifications /// /// Geräte-Informationen /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> RegisterPushnotificationsAsync(DeviceInstallationDto deviceInstallation, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var json = JsonSerializer.Serialize(deviceInstallation, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/pushnotifications/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.Pushnotification_Register_Failed: result.ErrorMessage = Errors.Pushnotification_Register_Failed; break; 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; } /// /// Deregistrieren eines Gerätes für Pushnotifications /// /// Id der Installation /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> UnregisterPushnotificationsAsync(string installationId, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var parameterId = HttpUtility.UrlEncode(installationId); var serverResult = await _httpClient.GetAsync($"api/pushnotifications/Unregister?installationId={parameterId}", 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.Pushnotification_Delete_Failed: result.ErrorMessage = Errors.Pushnotification_Delete_Failed; break; 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 Wallets /// /// Holen der Wallets eines AppUsers vom Server /// /// Id des AppUsers /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task>> GetWalletsAsync(string appUserId, string accessToken, CancellationToken token) { var result = new CommunicationResult>(); try { SetClientTime(); SetAccessToken(accessToken); var appUserIdString = HttpUtility.UrlEncode(appUserId); var serverResult = await _httpClient.GetAsync($"api/payments/GetWallets?appUserId={appUserIdString}", 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; } /// /// Gibt den Saldo eines Wallets zurück /// /// Id des App-Users /// Typ des Wallets /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> GetWalletBalanceAsync(string appUserId, WalletType walletType, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var appUserIdString = HttpUtility.UrlEncode(appUserId); var serverResult = await _httpClient.GetAsync($"api/payments/GetWalletBalance?appUserId={appUserIdString}&walletType={walletType}", token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var balance = long.Parse(await serverResult.Content.ReadAsStringAsync(token)); result.Success = true; result.Value = balance; } 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 Bankkonten /// /// Holt ein Bankkonto für einen App-User vom Server /// /// Id des App-Users /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> GetBankAccountsAsync(string appUserId, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var appUserIdString = HttpUtility.UrlEncode(appUserId); var serverResult = await _httpClient.GetAsync($"api/payments/GetBankAccount?appUserId={appUserIdString}", 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.BankAccount_NotFound: result.ErrorMessage = Errors.Common_NotFound; break; 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 oder Bearbeiten eines Bankkontos /// /// Bankkonto /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult mit einem AppUser der die entsprechende BankId gesetzt hat public async Task> CreateOrUpdateBankAccountAsync(BankAccountDto bankAccount, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var json = JsonSerializer.Serialize(bankAccount, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/payments/CreateOrUpdateBankAccount", 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.BankAccount_CreateOrUpdate_Unchanged: result.ErrorMessage = Errors.BankAccount_CreateOrUpdate_Unchanged; break; case CommunicationErrors.BankAccount_CreateOrUpdate_Failed: result.ErrorMessage = Errors.BankAccount_CreateOrUpdate_Failed; break; case CommunicationErrors.BankAccount_Iban_Invalid: result.ErrorMessage = Errors.BankAccount_Iban_Invalid; break; case CommunicationErrors.BankAccount_Bic_Invalid: result.ErrorMessage = Errors.BankAccount_Bic_Invalid; break; 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 Transaktionsgebühren /// /// Holen der Transaktionsgebühren vom Server. /// /// Wann wurden das letzte mal Daten erfolgreich geholt? Wenn null, dann alle Daten /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task>> GetTransactionFeesAsync(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/payments/GetTransactionFees?lastUpdate={lastUpdateString}", 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; } #endregion #region Payment /// /// Authorisierung der Bezahlung eines Walks mit dem Guthaben eines Wallets /// Das Geld wird vom Guthabenkonto auf das Transaktions-Konto gelegt /// /// Id des App-Users /// Id des Walks /// Zu zahlender Bertrag /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> AuthorizeWalkWithCreditAsync(string appUserId, string walkId, decimal ammount, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var model = new PayWalkWithCreditDto() { AppUserId = appUserId, WalkId = walkId, Ammount = ammount }; var json = JsonSerializer.Serialize(model, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/payments/AuthorizeWithCredit", 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.Walk_PaymentStatus_Invalid: result.ErrorMessage = Errors.Walk_PaymentStatus_Invalid; break; case CommunicationErrors.Walk_Cancelled: result.ErrorMessage = Errors.Walk_Cancelled; break; case CommunicationErrors.Walk_NotFound: result.ErrorMessage = Errors.Walk_NotFound; break; 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; } /// /// Authorisierung der Bezahlung eines Walks komplett mit einem Gutschein /// /// Id des Gutscheins /// Code des Gutscheins /// Id des App-Users /// Id des Walks /// Zu zahlender Bertrag /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> AuthorizeWalkWithVoucherAsync(string voucherId, string voucherCode, string appUserId, string walkId, decimal ammount, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var model = new PayWalkWithVoucherDto() { VoucherId = voucherId, Code = voucherCode, AppUserId = appUserId, WalkId = walkId, Ammount = ammount }; var json = JsonSerializer.Serialize(model, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/payments/AuthorizeWithVoucher", 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.Walk_VoucherInvalid: result.ErrorMessage = Errors.Walk_Voucher_Invalid; break; case CommunicationErrors.Walk_PaymentStatus_Invalid: result.ErrorMessage = Errors.Walk_PaymentStatus_Invalid; break; case CommunicationErrors.Walk_Cancelled: result.ErrorMessage = Errors.Walk_Cancelled; break; case CommunicationErrors.Walk_NotFound: result.ErrorMessage = Errors.Walk_NotFound; break; 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; } /// /// Einzahlen einer Summe auf das Transaktions-Konto eines App-Users mit Bezug auf einen Walk. /// Es können gebühren sofort auf das Transaktions-Konto des Mandanten abgeführt werden /// /// Id des App-Users /// Id des Walks /// Zu zahlender Bertrag /// Anfallende Gebühren /// Betrag der zusätzlich vom Guthabenkonto bezahlt werden muss /// Typ der Einzahlung /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult mit dem ReturnUrl, wenn erfolgreich public async Task> PayInAsync(string appUserId, string walkId, decimal ammount, decimal fees, decimal fromCredit, PayInType payInType, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var model = new PayInDto() { AppUserId = appUserId, WalkId = walkId, Ammount = ammount, Fees = fees, FromCredit = fromCredit, PayInType = payInType.ToDto() }; var json = JsonSerializer.Serialize(model, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/payments/PayIn", stringContent, token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token); result.Success = true; result.Value = dto.Url; } else { var errorCodeString = await serverResult.Content.ReadAsStringAsync(token); var errorCode = (CommunicationErrors)Enum.Parse(typeof(CommunicationErrors), errorCodeString); result.ErrorCode = errorCode; switch (errorCode) { case CommunicationErrors.PayIn_Failed: result.ErrorMessage = Errors.PayIn_Failed; break; case CommunicationErrors.Walk_PaymentStatus_Invalid: result.ErrorMessage = Errors.Walk_PaymentStatus_Invalid; break; case CommunicationErrors.Walk_Cancelled: result.ErrorMessage = Errors.Walk_Cancelled; break; case CommunicationErrors.Walk_NotFound: result.ErrorMessage = Errors.Walk_NotFound; break; 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 KYC /// /// Holen der letzten KYC-Dokumente eines App-Users /// /// Id des App-Users /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult mit dem aktuell gültigen KYC-Dokument oder null, wenn keines vorhanden public async Task> GetLatestKycDocumentAsync(string appUserId, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var appUserIdString = HttpUtility.UrlEncode(appUserId); var serverResult = await _httpClient.GetAsync($"api/payments/GetKycDocumentLatest?appUserId={appUserIdString}", 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.Kyc_NoDocument: result.ErrorMessage = Errors.Kyc_NoDocument; break; 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; } /// /// KYC-Dokument für einen App-User erstellen mit einer Seite /// /// Id des App-Users /// Dokument-Quelle /// Dateiname /// Datei als byte-Array /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult mit dem aktuell erstellten KYC-Dokument public async Task> CreateKycDocumentAsync(string appUserId, IdentityDocumentSource source, string fileOneName, byte[] fileOne, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var model = new IdentityDocumentCreateDto() { AppUserId = appUserId, Source = source.ToDto() }; var json = JsonSerializer.Serialize(model, _jsonOptions); var multipartContent = new MultipartFormDataContent(); multipartContent.Add(new StringContent(json, Encoding.UTF8, "application/json"), "model"); if (!string.IsNullOrEmpty(fileOneName) && fileOne != null) { multipartContent.Add(new ByteArrayContent(fileOne), "files", fileOneName); } var serverResult = await _httpClient.PostAsync($"api/payments/CreatKycDocument", multipartContent, 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.Kyc_CreationFailed: result.ErrorMessage = Errors.Kyc_CreationFailed; break; case CommunicationErrors.Kyc_NoFiles: result.ErrorMessage = Errors.Kyc_NoFiles; break; 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; } /// /// KYC-Dokument für einen App-User erstellen mit zwei Seiten /// /// Id des App-Users /// Dokument-Quelle /// Dateiname Datei 1 /// Datei 1 als byte-Array /// Dateiname Datei 2 /// Datei 2 als byte-Array /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult mit dem aktuell erstellten KYC-Dokument public async Task> CreateKycDocumentAsync(string appUserId, IdentityDocumentSource source, string fileOneName, byte[] fileOne, string fileTwoName, byte[] fileTwo, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var model = new IdentityDocumentCreateDto() { AppUserId = appUserId, Source = source.ToDto() }; var json = JsonSerializer.Serialize(model, _jsonOptions); var multipartContent = new MultipartFormDataContent(); multipartContent.Add(new StringContent(json, Encoding.UTF8, "application/json"), "model"); if (!string.IsNullOrEmpty(fileOneName) && fileOne != null) { multipartContent.Add(new ByteArrayContent(fileOne), "files", fileOneName); } if (!string.IsNullOrEmpty(fileTwoName) && fileTwo != null) { multipartContent.Add(new ByteArrayContent(fileTwo), "files", fileTwoName); } var serverResult = await _httpClient.PostAsync($"api/payments/CreatKycDocument", multipartContent, 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.Kyc_CreationFailed: result.ErrorMessage = Errors.Kyc_CreationFailed; break; case CommunicationErrors.Kyc_NoFiles: result.ErrorMessage = Errors.Kyc_NoFiles; break; 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 Payouts /// /// Holen der Auszahlungen 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>> GetPayoutsForSyncAsync(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/payments/GetPayoutsForSync?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 von Auszahlungen vom Server. /// /// Abfrageobjekt /// Aktuelles Accesstoken /// CancellationToken /// ListCommunicationResult public async Task>> GetPayoutsAsync(PayoutQueryDto 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/payments/GetPayouts", 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; } /// /// Erstellen einer Auszahlung /// /// Id des App-Users /// Betrag der ausgezahlt werden soll /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> CreatePayoutAsync(string appUserId, decimal ammount, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var model = new PayoutCreateDto() { AppUserId = appUserId, Ammount = ammount }; var json = JsonSerializer.Serialize(model, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/payments/CreatePayout", 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.PayOut_Failed: result.ErrorMessage = Errors.PayOut_Failed; break; 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 eine Auszahlung eines App-Users zurück /// /// Id des App-Users /// Id der Auszahlung /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> GetPayoutAsync(string appUserId, string payoutId, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var appUserIdString = HttpUtility.UrlEncode(appUserId); var payoutIdString = HttpUtility.UrlEncode(payoutId); var serverResult = await _httpClient.GetAsync($"api/payments/GetPayout?appUserId={appUserIdString}&payoutId={payoutIdString}", 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.PayOut_NotFound: result.ErrorMessage = Errors.PayOut_NotFound; break; 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 AppVersion /// /// Holen der aktuellen Version am Server für die gewählte Plattform /// /// Plattform /// Sprache /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> CheckCurrentAppVersionAsync(PlatformDto platform, string language, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var languageString = HttpUtility.UrlEncode(language); var serverResult = await _httpClient.GetAsync($"api/status/CheckAppVersion?platform={platform}&language={languageString}", 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; 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 aktuellen Version am Server für die gewählte Plattform /// /// Plattform /// Sprache /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> GetCurrentAppVersionAsync(PlatformDto platform, string language, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var languageString = HttpUtility.UrlEncode(language); var serverResult = await _httpClient.GetAsync($"api/status/AppVersion?platform={platform}&language={languageString}", 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; 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 Vouchers - Gutscheine /// /// Validieren eines Gutschein-Codes für einen AppUser /// /// Gutscheincode /// Id des AppUsers /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> CheckVoucherCodeAsync(string voucherCode, string appUserId, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var model = new CheckVoucherCodeDto() { Code = voucherCode, AppUserId = appUserId }; var json = JsonSerializer.Serialize(model, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/vouchers/CheckVoucherCode", 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; } /// /// Versucht einen Gutscheincode mit Hilfe eines QR-Codes zu holen /// /// QR-Code in Text /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> GetVoucherCodeByQrCode(string qrCode, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var model = new GetVoucherByQrCodeDto() { QrCode = qrCode }; var json = JsonSerializer.Serialize(model, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/vouchers/GetVoucherCodeByQrCode", stringContent, token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var dto = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token); result.Success = true; result.Value = dto.Code; } 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; } /// /// Versucht einen Gutschein zu reservieren /// /// Id des Gutscheins /// Code des Gutscheins /// Id des AppUsers /// Id des Walks /// Betrag - kann kleiner Gutscheinwert sein /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> ReserveVoucherAsync(string voucherId, string code, string appUserId, string walkId, decimal ammount, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var model = new ReserveVoucherDto() { VoucherId = voucherId, Code = code, AppUserId = appUserId, WalkId = walkId, Ammount = ammount }; var json = JsonSerializer.Serialize(model, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/vouchers/ReserveVoucher", 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; } /// /// Stornieren eines Gutscheins /// /// Id des Gutscheins /// Id des AppUsers /// Id des Walks /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> CancelVoucherAsync(string voucherId, string appUserId, string walkId, string accessToken, CancellationToken token) { var result = new CommunicationResult(){Value = false}; try { SetClientTime(); SetAccessToken(accessToken); var model = new CancelVoucherDto() { VoucherId = voucherId, AppUserId = appUserId, WalkId = walkId }; var json = JsonSerializer.Serialize(model, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/vouchers/CancelVoucher", 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 Abos - Subscriptions /// /// Gibt eine Liste von verfügbaren Abos zurück /// /// AppMode - Hundebesitzer oder Walker /// Sprache /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task>> GetSubscriptionsAsync(AppMode appMode, string language, string accessToken, CancellationToken token) { var result = new CommunicationResult>(); try { SetClientTime(); SetAccessToken(accessToken); var serverResult = await _httpClient.GetAsync($"api/subscriptions/GetAll?appMode={appMode}&language={language}", token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var dto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token); var subscriptions = dto.ToDomain(); result.Success = true; result.Value = subscriptions; } 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 gebuchten Abos zurück /// /// Sprache /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task>> GetMySubscriptionsAsync(string language, string accessToken, CancellationToken token) { var result = new CommunicationResult>(); try { SetClientTime(); SetAccessToken(accessToken); var serverResult = await _httpClient.GetAsync($"api/subscriptions/GetMy?language={language}", token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var dto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token); var subscriptions = dto.ToDomain(); result.Success = true; result.Value = subscriptions; } 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 gebuchten und aktiven Abos zurück /// /// Sprache /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task>> GetMyActiveSubscriptionsAsync(string language, string accessToken, CancellationToken token) { var result = new CommunicationResult>(); try { SetClientTime(); SetAccessToken(accessToken); var serverResult = await _httpClient.GetAsync($"api/subscriptions/GetMy/active?language={language}", token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var dto = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token); var subscriptions = dto.ToDomain(); result.Success = true; result.Value = subscriptions; } 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; } /// /// Erstellen einer Abo-Buchung /// /// AppUserId /// Id des Abos /// Sprache /// InAppBillingPurchase serialisiert als JSON /// Optional: Ablaufdatum das gesetzt werden soll /// Plattform auf der das Abo erstellt wird /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task>> CreateSubscriptionAsync(string appUserId, string subscriptionId, string language, string inAppBillingPurchase, DateTime? expirationDateToSet, Platform platform, string accessToken, CancellationToken token) { var result = new CommunicationResult>() { Success = false, Value = new CreateResponse() { Status = CreateStatus.Error, Value = null } }; try { SetClientTime(); SetAccessToken(accessToken); var dto = new AppUserCreateSubscriptionDto() { AppUserId = appUserId, SubscriptionId = subscriptionId, Language = language, InAppBillingPurchaseJson = inAppBillingPurchase, ExpirationDateToSet = expirationDateToSet, Platform = platform.ToDto() }; var json = JsonSerializer.Serialize(dto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/subscriptions/Create", stringContent, token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var serverResponse = await serverResult.Content.ReadFromJsonAsync>(_jsonOptions, token); if (serverResponse.Status == CreateStatusDto.Success) { result.Success = true; result.Value.Status = (CreateStatus)serverResponse.Status; result.Value.Value = serverResponse.Value.ToDomain(); } else { result.Success = false; result.Value.Status = (CreateStatus)serverResponse.Status; result.Value.Value = 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; } /// /// Wechseln eines Abos in der Gruppe für eine Abo-Buchung /// /// Id der Abo-Buchung /// AppUserId /// Id des Abos auf das gechselt werden soll /// Sprache /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> SwitchSubscriptionAsync(long bookingId, string appUserId, string subscriptionId, string language, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var dto = new AppUserSwitchSubscriptionDto() { BookingId = bookingId, AppUserId = appUserId, SubscriptionId = subscriptionId, Language = language }; var json = JsonSerializer.Serialize(dto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/subscriptions/Switch", stringContent, token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var serverResponse = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token); result.Success = true; result.Value = serverResponse.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; } /// /// Stornieren einer Abo-Buchung /// /// Abo-Buchungs ID /// AppUserId /// Sprache /// Plattform auf der das Abo erstellt wurde /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> CancelSubscriptionAsync(long bookingId, string appUserId, string language, Platform platform, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var dto = new AppUserCancelSubscriptionDto() { BookingId = bookingId, AppUserId = appUserId, Language = language, Platform = platform.ToDto() }; var json = JsonSerializer.Serialize(dto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/subscriptions/Cancel", stringContent, token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var serverResponse = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token); result.Success = true; result.Value = serverResponse.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; } /// /// Verlängern einer Abo-Buchung /// /// Abo-Buchungs ID /// AppUserId /// Sprache /// Plattform auf der das Abo erstellt wurde /// Aktuelles Accesstoken /// CancellationToken /// CommunicationResult public async Task> RenewSubscriptionAsync(long bookingId, string appUserId, string language, Platform platform, string accessToken, CancellationToken token) { var result = new CommunicationResult(); try { SetClientTime(); SetAccessToken(accessToken); var dto = new AppUserRenewSubscriptionDto() { BookingId = bookingId, AppUserId = appUserId, Language = language, Platform = platform.ToDto() }; var json = JsonSerializer.Serialize(dto, _jsonOptions); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var serverResult = await _httpClient.PostAsync($"api/subscriptions/Renew", stringContent, token).ConfigureAwait(false); if (serverResult.IsSuccessStatusCode) { SetIsOnline(true); var serverResponse = await serverResult.Content.ReadFromJsonAsync(_jsonOptions, token); result.Success = true; result.Value = serverResponse.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; } #endregion #region Private /// /// Setzt den Status des Communicationservice auf Online. /// Wird von Methoden verwendet wenn diese erfolgreich kommunizieren konnten /// /// true wenn online, false sonst private void SetIsOnline(bool isOnline) { _isOnline = isOnline; } /// /// Setzt das Access-Token für den HTTP-Client /// /// Access token private void SetAccessToken(string token) { var authHeader = new AuthenticationHeaderValue("bearer", token); _httpClient.DefaultRequestHeaders.Authorization = authHeader; } /// /// Fügt die Zeit des Clients in den HTTP-Header /// private void SetClientTime() { _httpClient.DefaultRequestHeaders.Remove("clientTime"); _httpClient.DefaultRequestHeaders.Add("clientTime", DateTimeOffset.UtcNow.ToString("O")); } #endregion } }