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
This commit is contained in:
		| @ -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<T> PutJsonAsync<T>(string uri, T value) | ||||
|         { | ||||
|             var response = await _http.PutAsJsonAsync(uri, value); | ||||
|             var result = await response.Content.ReadFromJsonAsync<T>(); | ||||
|             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<string> GetStringAsync(string uri) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 return await _http.GetStringAsync(uri); | ||||
|             } | ||||
|             catch (Exception e) | ||||
|             { | ||||
|                 //TODO replace with logging | ||||
|                 Console.WriteLine(e); | ||||
|             } | ||||
|  | ||||
|             return default; | ||||
|         } | ||||
|  | ||||
|         protected async Task<byte[]> GetByteArrayAsync(string uri) | ||||
|         { | ||||
|             return await _http.GetByteArrayAsync(uri); | ||||
|         } | ||||
|             try | ||||
|             { | ||||
|                 return await _http.GetByteArrayAsync(uri); | ||||
|             } | ||||
|             catch (Exception e) | ||||
|             { | ||||
|                 Console.WriteLine(e); | ||||
|             } | ||||
|  | ||||
|         protected async Task<R> PostJsonAsync<T, R>(string uri, T value) | ||||
|         { | ||||
|             var response = await _http.PostAsJsonAsync(uri, value); | ||||
|             if (!ValidateJsonContent(response.Content)) return default; | ||||
|              | ||||
|             var result = await response.Content.ReadFromJsonAsync<R>(); | ||||
|             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<T> PostJsonAsync<T>(string uri, T value) | ||||
|         { | ||||
|             return await PostJsonAsync<T, T>(uri, value); | ||||
|             return default; | ||||
|         } | ||||
|  | ||||
|         protected async Task<T> GetJsonAsync<T>(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<T> PutJsonAsync<T>(string uri, T value) | ||||
|         { | ||||
|             return await PutJsonAsync<T, T>(uri, value); | ||||
|         } | ||||
|  | ||||
|         protected async Task<TResult> PutJsonAsync<TValue, TResult>(string uri, TValue value) | ||||
|         { | ||||
|             var response = await _http.PutAsJsonAsync(uri, value); | ||||
|             if (CheckResponse(response) && ValidateJsonContent(response.Content)) | ||||
|             { | ||||
|                 var result = await response.Content.ReadFromJsonAsync<TResult>(); | ||||
|                 return result; | ||||
|             } | ||||
|  | ||||
|             return default; | ||||
|         } | ||||
|  | ||||
|         protected async Task PostAsync(string uri) | ||||
|         { | ||||
|             var response = await _http.PostAsync(uri, null); | ||||
|             CheckResponse(response); | ||||
|         } | ||||
|  | ||||
|         protected async Task<T> PostJsonAsync<T>(string uri, T value) | ||||
|         { | ||||
|             return await PostJsonAsync<T, T>(uri, value); | ||||
|         } | ||||
|  | ||||
|         protected async Task<TResult> PostJsonAsync<TValue, TResult>(string uri, TValue value) | ||||
|         { | ||||
|             var response = await _http.PostAsJsonAsync(uri, value); | ||||
|             if (CheckResponse(response) && ValidateJsonContent(response.Content)) | ||||
|             { | ||||
|                 var result = await response.Content.ReadFromJsonAsync<TResult>(); | ||||
|                 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<string> 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) | ||||
|  | ||||
| @ -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<User> 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<User>(Apiurl, user); | ||||
|             } | ||||
|             catch | ||||
|             { | ||||
|                 return null; | ||||
|             } | ||||
|             return await PostJsonAsync<User>(Apiurl, user); | ||||
|         } | ||||
|  | ||||
|         public async Task<User> AddUserAsync(User user, Alias alias) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 return await PostJsonAsync<User>(CreateCrossTenantUrl(Apiurl, alias), user); | ||||
|             } | ||||
|             catch | ||||
|             { | ||||
|                 return null; | ||||
|             } | ||||
|             return await PostJsonAsync<User>(CreateCrossTenantUrl(Apiurl, alias), user); | ||||
|         } | ||||
|  | ||||
|         public async Task<User> UpdateUserAsync(User user) | ||||
|         { | ||||
|             return await PutJsonAsync<User>($"{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<User> VerifyEmailAsync(User user, string token) | ||||
| @ -101,6 +89,5 @@ namespace Oqtane.Services | ||||
|         { | ||||
|             return await PostJsonAsync<User>($"{Apiurl}/reset?token={token}", user); | ||||
|         } | ||||
|  | ||||
|     } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Pavel Vesely
					Pavel Vesely