From 69bfd6f0e85d9b5f090393ff98bbbc3f1846a801 Mon Sep 17 00:00:00 2001 From: Pavel Vesely Date: Thu, 16 Apr 2020 10:04:02 +0200 Subject: [PATCH] Migration to using System.Net.Http.Json; part two - some cosmetics and bugs, - logging preparation, - error checking - Fixed bug with site.AllowRegistration in case of installation --- Oqtane.Client/Services/ServiceBase.cs | 130 +++++++++++++++++--------- Oqtane.Client/Services/UserService.cs | 37 +++----- 2 files changed, 96 insertions(+), 71 deletions(-) diff --git a/Oqtane.Client/Services/ServiceBase.cs b/Oqtane.Client/Services/ServiceBase.cs index a6b80fd6..85980739 100644 --- a/Oqtane.Client/Services/ServiceBase.cs +++ b/Oqtane.Client/Services/ServiceBase.cs @@ -5,7 +5,6 @@ using System.Net.Http.Json; using System.Threading; using System.Threading.Tasks; using Oqtane.Models; -using Oqtane.Modules.HtmlText.Models; namespace Oqtane.Services { @@ -18,51 +17,40 @@ namespace Oqtane.Services _http = client; } - protected async Task PutJsonAsync(string uri, T value) - { - var response = await _http.PutAsJsonAsync(uri, value); - var result = await response.Content.ReadFromJsonAsync(); - return result; - } - - protected async Task PutAsync(string uri) - { - await _http.PutAsync(uri, null); - } - - protected async Task PostAsync(string uri) - { - await _http.PostAsync(uri, null); - } protected async Task GetAsync(string uri) { - await _http.GetAsync(uri); + var response = await _http.GetAsync(uri); + CheckResponse(response); + } + + protected async Task GetStringAsync(string uri) + { + try + { + return await _http.GetStringAsync(uri); + } + catch (Exception e) + { + //TODO replace with logging + Console.WriteLine(e); + } + + return default; } protected async Task GetByteArrayAsync(string uri) { - return await _http.GetByteArrayAsync(uri); - } + try + { + return await _http.GetByteArrayAsync(uri); + } + catch (Exception e) + { + Console.WriteLine(e); + } - protected async Task PostJsonAsync(string uri, T value) - { - var response = await _http.PostAsJsonAsync(uri, value); - if (!ValidateJsonContent(response.Content)) return default; - - var result = await response.Content.ReadFromJsonAsync(); - return result; - } - - private static bool ValidateJsonContent(HttpContent content) - { - var mediaType = content?.Headers.ContentType?.MediaType; - return mediaType != null && mediaType.Equals("application/json", StringComparison.OrdinalIgnoreCase); - } - - protected async Task PostJsonAsync(string uri, T value) - { - return await PostJsonAsync(uri, value); + return default; } protected async Task GetJsonAsync(string uri) @@ -76,25 +64,75 @@ namespace Oqtane.Services return default; } + protected async Task PutAsync(string uri) + { + var response = await _http.PutAsync(uri, null); + CheckResponse(response); + } + + protected async Task PutJsonAsync(string uri, T value) + { + return await PutJsonAsync(uri, value); + } + + protected async Task PutJsonAsync(string uri, TValue value) + { + var response = await _http.PutAsJsonAsync(uri, value); + if (CheckResponse(response) && ValidateJsonContent(response.Content)) + { + var result = await response.Content.ReadFromJsonAsync(); + return result; + } + + return default; + } + + protected async Task PostAsync(string uri) + { + var response = await _http.PostAsync(uri, null); + CheckResponse(response); + } + + protected async Task PostJsonAsync(string uri, T value) + { + return await PostJsonAsync(uri, value); + } + + protected async Task PostJsonAsync(string uri, TValue value) + { + var response = await _http.PostAsJsonAsync(uri, value); + if (CheckResponse(response) && ValidateJsonContent(response.Content)) + { + var result = await response.Content.ReadFromJsonAsync(); + return result; + } + + return default; + } + + protected async Task DeleteAsync(string uri) + { + var response = await _http.DeleteAsync(uri); + CheckResponse(response); + } + private bool CheckResponse(HttpResponseMessage response) { if (response.IsSuccessStatusCode) return true; if (response.StatusCode != HttpStatusCode.NoContent && response.StatusCode != HttpStatusCode.NotFound) { - //TODO: Log error here + //TODO: Log errors here + Console.WriteLine($"Response status: {response.StatusCode} {response.ReasonPhrase}"); } return false; } - protected async Task DeleteAsync(string uri) + private static bool ValidateJsonContent(HttpContent content) { - await _http.DeleteAsync(uri); - } - - protected async Task GetStringAsync(string uri) - { - return await _http.GetStringAsync(uri); + var mediaType = content?.Headers.ContentType?.MediaType; + return mediaType != null && mediaType.Equals("application/json", StringComparison.OrdinalIgnoreCase); + //TODO Missing content JSON validation } public static string CreateApiUrl(Alias alias, string absoluteUri, string serviceName) diff --git a/Oqtane.Client/Services/UserService.cs b/Oqtane.Client/Services/UserService.cs index 5e38485c..dd515525 100644 --- a/Oqtane.Client/Services/UserService.cs +++ b/Oqtane.Client/Services/UserService.cs @@ -8,14 +8,12 @@ namespace Oqtane.Services { public class UserService : ServiceBase, IUserService { - private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - private readonly ISiteService _siteService; + private readonly ISiteService _siteService; public UserService(HttpClient http, SiteState siteState, NavigationManager navigationManager, ISiteService siteService) : base(http) { - _siteState = siteState; _navigationManager = navigationManager; _siteService = siteService; @@ -38,39 +36,29 @@ namespace Oqtane.Services public async Task AddUserAsync(User user) { - Site site = await _siteService.GetSiteAsync(_siteState.Alias.SiteId, _siteState.Alias); - - if (!site.AllowRegistration) + // On initial site creation alias is null and we always want to create host user + if (user.Username != Constants.HostUser && _siteState.Alias != null) { - return null; + Site site = await _siteService.GetSiteAsync(_siteState.Alias.SiteId, _siteState.Alias); + if (!site.AllowRegistration) + { + return null; + } } - try - { - return await PostJsonAsync(Apiurl, user); - } - catch - { - return null; - } + return await PostJsonAsync(Apiurl, user); } public async Task AddUserAsync(User user, Alias alias) { - try - { - return await PostJsonAsync(CreateCrossTenantUrl(Apiurl, alias), user); - } - catch - { - return null; - } + return await PostJsonAsync(CreateCrossTenantUrl(Apiurl, alias), user); } public async Task UpdateUserAsync(User user) { return await PutJsonAsync($"{Apiurl}/{user.UserId.ToString()}", user); } + public async Task DeleteUserAsync(int userId) { await DeleteAsync($"{Apiurl}/{userId.ToString()}"); @@ -84,7 +72,7 @@ namespace Oqtane.Services public async Task LogoutUserAsync(User user) { // best practices recommend post is preferrable to get for logout - await PostJsonAsync($"{Apiurl}/logout", user); + await PostJsonAsync($"{Apiurl}/logout", user); } public async Task VerifyEmailAsync(User user, string token) @@ -101,6 +89,5 @@ namespace Oqtane.Services { return await PostJsonAsync($"{Apiurl}/reset?token={token}", user); } - } }