diff --git a/Oqtane.Client/Modules/HtmlText/Services/HtmlTextService.cs b/Oqtane.Client/Modules/HtmlText/Services/HtmlTextService.cs index 1b3a7834..d2154535 100644 --- a/Oqtane.Client/Modules/HtmlText/Services/HtmlTextService.cs +++ b/Oqtane.Client/Modules/HtmlText/Services/HtmlTextService.cs @@ -11,13 +11,13 @@ namespace Oqtane.Modules.HtmlText.Services { public class HtmlTextService : ServiceBase, IHtmlTextService { - private readonly HttpClient _http; + private readonly NavigationManager _navigationManager; private readonly SiteState _siteState; - public HtmlTextService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public HtmlTextService(HttpClient http, SiteState siteState, NavigationManager navigationManager) : base(http) { - _http = http; + _siteState = siteState; _navigationManager = navigationManager; } @@ -31,7 +31,7 @@ namespace Oqtane.Modules.HtmlText.Services { //because GetJsonAsync() returns an error if no content exists for the ModuleId ( https://github.com/aspnet/AspNetCore/issues/14041 ) //null value is transfered as empty list - var htmlTextList = await _http.GetJsonAsync>(ApiUrl + "/" + moduleId + "?entityid=" + moduleId); + var htmlTextList = await GetJsonAsync>(ApiUrl + "/" + moduleId + "?entityid=" + moduleId); htmlText = htmlTextList.FirstOrDefault(); } catch @@ -44,17 +44,18 @@ namespace Oqtane.Modules.HtmlText.Services public async Task AddHtmlTextAsync(HtmlTextInfo htmlText) { - await _http.PostJsonAsync(ApiUrl + "?entityid=" + htmlText.ModuleId, htmlText); + await PostJsonAsync(ApiUrl + "?entityid=" + htmlText.ModuleId, htmlText); } public async Task UpdateHtmlTextAsync(HtmlTextInfo htmlText) { - await _http.PutJsonAsync(ApiUrl + "/" + htmlText.HtmlTextId + "?entityid=" + htmlText.ModuleId, htmlText); + await PutJsonAsync(ApiUrl + "/" + htmlText.HtmlTextId + "?entityid=" + htmlText.ModuleId, htmlText); } + public async Task DeleteHtmlTextAsync(int moduleId) { - await _http.DeleteAsync(ApiUrl + "/" + moduleId + "?entityid=" + moduleId); + await DeleteAsync(ApiUrl + "/" + moduleId + "?entityid=" + moduleId); } } } diff --git a/Oqtane.Client/Oqtane.Client.csproj b/Oqtane.Client/Oqtane.Client.csproj index b1cbb15f..68dad12d 100644 --- a/Oqtane.Client/Oqtane.Client.csproj +++ b/Oqtane.Client/Oqtane.Client.csproj @@ -29,8 +29,8 @@ - + diff --git a/Oqtane.Client/Providers/IdentityAuthenticationStateProvider.cs b/Oqtane.Client/Providers/IdentityAuthenticationStateProvider.cs index 7f21c65a..24c36822 100644 --- a/Oqtane.Client/Providers/IdentityAuthenticationStateProvider.cs +++ b/Oqtane.Client/Providers/IdentityAuthenticationStateProvider.cs @@ -1,5 +1,6 @@ using System; using System.Net.Http; +using System.Net.Http.Json; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; @@ -29,7 +30,7 @@ namespace Oqtane.Providers // get HttpClient lazily from IServiceProvider as you cannot use standard dependency injection due to the AuthenticationStateProvider being initialized prior to NavigationManager ( https://github.com/aspnet/AspNetCore/issues/11867 ) var http = _serviceProvider.GetRequiredService(); string apiurl = ServiceBase.CreateApiUrl(_siteState.Alias, _navigationManager.Uri, "User") + "/authenticate"; - User user = await http.GetJsonAsync(apiurl); + User user = await http.GetFromJsonAsync(apiurl); ClaimsIdentity identity = new ClaimsIdentity(); if (user.IsAuthenticated) diff --git a/Oqtane.Client/Services/AliasService.cs b/Oqtane.Client/Services/AliasService.cs index d7fdf3e6..75cdd13b 100644 --- a/Oqtane.Client/Services/AliasService.cs +++ b/Oqtane.Client/Services/AliasService.cs @@ -2,23 +2,24 @@ using System.Threading.Tasks; using System.Net.Http; using System.Linq; -using Microsoft.AspNetCore.Components; +//using Microsoft.AspNetCore.Components; using System.Collections.Generic; using Oqtane.Shared; using System.Net; using System; +using System.Net.Http.Json; +using Microsoft.AspNetCore.Components; namespace Oqtane.Services { public class AliasService : ServiceBase, IAliasService { - private readonly HttpClient _http; + private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public AliasService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public AliasService(HttpClient http, SiteState siteState, NavigationManager navigationManager) :base(http) { - _http = http; _siteState = siteState; _navigationManager = navigationManager; } @@ -30,13 +31,13 @@ namespace Oqtane.Services public async Task> GetAliasesAsync() { - List aliases = await _http.GetJsonAsync>(Apiurl); + List aliases = await GetJsonAsync>(Apiurl); return aliases.OrderBy(item => item.Name).ToList(); } public async Task GetAliasAsync(int aliasId) { - return await _http.GetJsonAsync($"{Apiurl}/{aliasId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/{aliasId.ToString()}"); } public async Task GetAliasAsync(string url, DateTime lastSyncDate) @@ -51,21 +52,21 @@ namespace Oqtane.Services { name = name.Substring(0, name.Length - 1); } - return await _http.GetJsonAsync($"{Apiurl}/name/{WebUtility.UrlEncode(name)}?lastsyncdate={lastSyncDate.ToString("yyyyMMddHHmmssfff")}"); + return await GetJsonAsync($"{Apiurl}/name/{WebUtility.UrlEncode(name)}?lastsyncdate={lastSyncDate.ToString("yyyyMMddHHmmssfff")}"); } public async Task AddAliasAsync(Alias alias) { - return await _http.PostJsonAsync(Apiurl, alias); + return await PostJsonAsync(Apiurl, alias); } public async Task UpdateAliasAsync(Alias alias) { - return await _http.PutJsonAsync($"{Apiurl}/{alias.AliasId.ToString()}", alias); + return await PutJsonAsync($"{Apiurl}/{alias.AliasId.ToString()}", alias); } public async Task DeleteAliasAsync(int aliasId) { - await _http.DeleteAsync($"{Apiurl}/{aliasId.ToString()}"); + await DeleteAsync($"{Apiurl}/{aliasId.ToString()}"); } } } diff --git a/Oqtane.Client/Services/FileService.cs b/Oqtane.Client/Services/FileService.cs index 195e62f2..09389f1e 100644 --- a/Oqtane.Client/Services/FileService.cs +++ b/Oqtane.Client/Services/FileService.cs @@ -13,15 +13,13 @@ namespace Oqtane.Services { public class FileService : ServiceBase, IFileService { - private readonly HttpClient _http; private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; private readonly IJSRuntime _jsRuntime; public FileService(HttpClient http, SiteState siteState, NavigationManager navigationManager, - IJSRuntime jsRuntime) + IJSRuntime jsRuntime) : base(http) { - _http = http; _siteState = siteState; _navigationManager = navigationManager; _jsRuntime = jsRuntime; @@ -39,7 +37,7 @@ namespace Oqtane.Services public async Task> GetFilesAsync(string folder) { - return await _http.GetJsonAsync>($"{Apiurl}?folder={folder}"); + return await GetJsonAsync>($"{Apiurl}?folder={folder}"); } public async Task> GetFilesAsync(int siteId, string folderPath) @@ -51,39 +49,32 @@ namespace Oqtane.Services var path = WebUtility.UrlEncode(folderPath); - return await _http.GetJsonAsync>($"{Apiurl}/{siteId}/{path}"); + return await GetJsonAsync>($"{Apiurl}/{siteId}/{path}"); } public async Task GetFileAsync(int fileId) { - try - { - return await _http.GetJsonAsync($"{Apiurl}/{fileId.ToString()}"); - } - catch - { - return null; - } + return await GetJsonAsync($"{Apiurl}/{fileId.ToString()}"); } public async Task AddFileAsync(File file) { - return await _http.PostJsonAsync(Apiurl, file); + return await PostJsonAsync(Apiurl, file); } public async Task UpdateFileAsync(File file) { - return await _http.PutJsonAsync($"{Apiurl}/{file.FileId.ToString()}", file); + return await PutJsonAsync($"{Apiurl}/{file.FileId.ToString()}", file); } public async Task DeleteFileAsync(int fileId) { - await _http.DeleteAsync($"{Apiurl}/{fileId.ToString()}"); + await DeleteAsync($"{Apiurl}/{fileId.ToString()}"); } public async Task UploadFileAsync(string url, int folderId) { - return await _http.GetJsonAsync($"{Apiurl}/upload?url={WebUtility.UrlEncode(url)}&folderid={folderId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/upload?url={WebUtility.UrlEncode(url)}&folderid={folderId.ToString()}"); } public async Task UploadFilesAsync(int folderId, string[] files, string id) @@ -133,7 +124,7 @@ namespace Oqtane.Services public async Task DownloadFileAsync(int fileId) { - return await _http.GetByteArrayAsync($"{Apiurl}/download/{fileId.ToString()}"); + return await GetByteArrayAsync($"{Apiurl}/download/{fileId.ToString()}"); } } } diff --git a/Oqtane.Client/Services/FolderService.cs b/Oqtane.Client/Services/FolderService.cs index 642aeafa..9d63ae8d 100644 --- a/Oqtane.Client/Services/FolderService.cs +++ b/Oqtane.Client/Services/FolderService.cs @@ -13,13 +13,11 @@ namespace Oqtane.Services { public class FolderService : ServiceBase, IFolderService { - private readonly HttpClient _http; private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public FolderService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public FolderService(HttpClient http, SiteState siteState, NavigationManager navigationManager):base(http) { - _http = http; _siteState = siteState; _navigationManager = navigationManager; } @@ -28,14 +26,14 @@ namespace Oqtane.Services public async Task> GetFoldersAsync(int siteId) { - List folders = await _http.GetJsonAsync>($"{ApiUrl}?siteid={siteId.ToString()}"); + List folders = await GetJsonAsync>($"{ApiUrl}?siteid={siteId.ToString()}"); folders = GetFoldersHierarchy(folders); return folders; } public async Task GetFolderAsync(int folderId) { - return await _http.GetJsonAsync($"{ApiUrl}/{folderId.ToString()}"); + return await GetJsonAsync($"{ApiUrl}/{folderId.ToString()}"); } public async Task GetFolderAsync(int siteId, [NotNull] string folderPath) @@ -47,17 +45,17 @@ namespace Oqtane.Services var path = WebUtility.UrlEncode(folderPath); - return await _http.GetJsonAsync($"{ApiUrl}/{siteId}/{path}"); + return await GetJsonAsync($"{ApiUrl}/{siteId}/{path}"); } public async Task AddFolderAsync(Folder folder) { - return await _http.PostJsonAsync(ApiUrl, folder); + return await PostJsonAsync(ApiUrl, folder); } public async Task UpdateFolderAsync(Folder folder) { - return await _http.PutJsonAsync($"{ApiUrl}/{folder.FolderId.ToString()}", folder); + return await PutJsonAsync($"{ApiUrl}/{folder.FolderId.ToString()}", folder); } public async Task UpdateFolderOrderAsync(int siteId, int folderId, int? parentId) @@ -65,12 +63,12 @@ namespace Oqtane.Services var parent = parentId == null ? string.Empty : parentId.ToString(); - await _http.PutJsonAsync($"{ApiUrl}/?siteid={siteId.ToString()}&folderid={folderId.ToString()}&parentid={parent}", null); + await PutAsync($"{ApiUrl}/?siteid={siteId.ToString()}&folderid={folderId.ToString()}&parentid={parent}"); } public async Task DeleteFolderAsync(int folderId) { - await _http.DeleteAsync($"{ApiUrl}/{folderId.ToString()}"); + await DeleteAsync($"{ApiUrl}/{folderId.ToString()}"); } private static List GetFoldersHierarchy(List folders) diff --git a/Oqtane.Client/Services/InstallationService.cs b/Oqtane.Client/Services/InstallationService.cs index 40da3010..bb39ef00 100644 --- a/Oqtane.Client/Services/InstallationService.cs +++ b/Oqtane.Client/Services/InstallationService.cs @@ -8,13 +8,11 @@ namespace Oqtane.Services { public class InstallationService : ServiceBase, IInstallationService { - private readonly HttpClient _http; private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public InstallationService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public InstallationService(HttpClient http, SiteState siteState, NavigationManager navigationManager):base(http) { - _http = http; _siteState = siteState; _navigationManager = navigationManager; } @@ -23,17 +21,17 @@ namespace Oqtane.Services public async Task IsInstalled() { - return await _http.GetJsonAsync($"{ApiUrl}/installed"); + return await GetJsonAsync($"{ApiUrl}/installed"); } public async Task Install(InstallConfig config) { - return await _http.PostJsonAsync(ApiUrl, config); + return await PostJsonAsync(ApiUrl, config); } public async Task Upgrade() { - return await _http.GetJsonAsync($"{ApiUrl}/upgrade"); + return await GetJsonAsync($"{ApiUrl}/upgrade"); } } } diff --git a/Oqtane.Client/Services/JobLogService.cs b/Oqtane.Client/Services/JobLogService.cs index f564f46c..9d270357 100644 --- a/Oqtane.Client/Services/JobLogService.cs +++ b/Oqtane.Client/Services/JobLogService.cs @@ -10,13 +10,12 @@ namespace Oqtane.Services { public class JobLogService : ServiceBase, IJobLogService { - private readonly HttpClient _http; private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public JobLogService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public JobLogService(HttpClient http, SiteState siteState, NavigationManager navigationManager) :base(http) + { - _http = http; _siteState = siteState; _navigationManager = navigationManager; } @@ -28,27 +27,27 @@ namespace Oqtane.Services public async Task> GetJobLogsAsync() { - List joblogs = await _http.GetJsonAsync>(Apiurl); + List joblogs = await GetJsonAsync>(Apiurl); return joblogs.OrderBy(item => item.StartDate).ToList(); } public async Task GetJobLogAsync(int jobLogId) { - return await _http.GetJsonAsync($"{Apiurl}/{jobLogId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/{jobLogId.ToString()}"); } public async Task AddJobLogAsync(JobLog joblog) { - return await _http.PostJsonAsync(Apiurl, joblog); + return await PostJsonAsync(Apiurl, joblog); } public async Task UpdateJobLogAsync(JobLog joblog) { - return await _http.PutJsonAsync($"{Apiurl}/{joblog.JobLogId.ToString()}", joblog); + return await PutJsonAsync($"{Apiurl}/{joblog.JobLogId.ToString()}", joblog); } public async Task DeleteJobLogAsync(int jobLogId) { - await _http.DeleteAsync($"{Apiurl}/{jobLogId.ToString()}"); + await DeleteAsync($"{Apiurl}/{jobLogId.ToString()}"); } } } diff --git a/Oqtane.Client/Services/JobService.cs b/Oqtane.Client/Services/JobService.cs index b06b2fd7..92bef73d 100644 --- a/Oqtane.Client/Services/JobService.cs +++ b/Oqtane.Client/Services/JobService.cs @@ -10,13 +10,13 @@ namespace Oqtane.Services { public class JobService : ServiceBase, IJobService { - private readonly HttpClient _http; + private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public JobService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public JobService(HttpClient http, SiteState siteState, NavigationManager navigationManager) : base(http) { - _http = http; + _siteState = siteState; _navigationManager = navigationManager; } @@ -28,37 +28,37 @@ namespace Oqtane.Services public async Task> GetJobsAsync() { - List jobs = await _http.GetJsonAsync>(Apiurl); + List jobs = await GetJsonAsync>(Apiurl); return jobs.OrderBy(item => item.Name).ToList(); } public async Task GetJobAsync(int jobId) { - return await _http.GetJsonAsync($"{Apiurl}/{jobId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/{jobId.ToString()}"); } public async Task AddJobAsync(Job job) { - return await _http.PostJsonAsync(Apiurl, job); + return await PostJsonAsync(Apiurl, job); } public async Task UpdateJobAsync(Job job) { - return await _http.PutJsonAsync($"{Apiurl}/{job.JobId.ToString()}", job); + return await PutJsonAsync($"{Apiurl}/{job.JobId.ToString()}", job); } public async Task DeleteJobAsync(int jobId) { - await _http.DeleteAsync($"{Apiurl}/{jobId.ToString()}"); + await DeleteAsync($"{Apiurl}/{jobId.ToString()}"); } public async Task StartJobAsync(int jobId) { - await _http.GetAsync($"{Apiurl}/start/{jobId.ToString()}"); + await GetAsync($"{Apiurl}/start/{jobId.ToString()}"); } public async Task StopJobAsync(int jobId) { - await _http.GetAsync($"{Apiurl}/stop/{jobId.ToString()}"); + await GetAsync($"{Apiurl}/stop/{jobId.ToString()}"); } } } diff --git a/Oqtane.Client/Services/LogService.cs b/Oqtane.Client/Services/LogService.cs index a679ee3d..b763dbb9 100644 --- a/Oqtane.Client/Services/LogService.cs +++ b/Oqtane.Client/Services/LogService.cs @@ -12,13 +12,13 @@ namespace Oqtane.Services { public class LogService : ServiceBase, ILogService { - private readonly HttpClient _http; + private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public LogService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public LogService(HttpClient http, SiteState siteState, NavigationManager navigationManager) : base(http) { - _http = http; + _siteState = siteState; _navigationManager = navigationManager; } @@ -30,12 +30,12 @@ namespace Oqtane.Services public async Task> GetLogsAsync(int siteId, string level, string function, int rows) { - return await _http.GetJsonAsync>($"{Apiurl}?siteid={siteId.ToString()}&level={level}&function={function}&rows={rows.ToString()}"); + return await GetJsonAsync>($"{Apiurl}?siteid={siteId.ToString()}&level={level}&function={function}&rows={rows.ToString()}"); } public async Task GetLogAsync(int logId) { - return await _http.GetJsonAsync($"{Apiurl}/{logId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/{logId.ToString()}"); } public async Task Log(int? pageId, int? moduleId, int? userId, string category, string feature, LogFunction function, LogLevel level, Exception exception, string message, params object[] args) @@ -69,7 +69,7 @@ namespace Oqtane.Services log.Message = message; log.MessageTemplate = ""; log.Properties = JsonSerializer.Serialize(args); - await _http.PostJsonAsync(CreateCrossTenantUrl(Apiurl, alias), log); + await PostJsonAsync(CreateCrossTenantUrl(Apiurl, alias), log); } } } diff --git a/Oqtane.Client/Services/ModuleDefinitionService.cs b/Oqtane.Client/Services/ModuleDefinitionService.cs index 119aa9dd..3f0d3feb 100644 --- a/Oqtane.Client/Services/ModuleDefinitionService.cs +++ b/Oqtane.Client/Services/ModuleDefinitionService.cs @@ -17,7 +17,7 @@ namespace Oqtane.Services private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public ModuleDefinitionService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public ModuleDefinitionService(HttpClient http, SiteState siteState, NavigationManager navigationManager) : base(http) { _http = http; _siteState = siteState; @@ -31,28 +31,28 @@ namespace Oqtane.Services public async Task> GetModuleDefinitionsAsync(int siteId) { - List moduledefinitions = await _http.GetJsonAsync>($"{Apiurl}?siteid={siteId.ToString()}"); + List moduledefinitions = await GetJsonAsync>($"{Apiurl}?siteid={siteId.ToString()}"); return moduledefinitions.OrderBy(item => item.Name).ToList(); } public async Task GetModuleDefinitionAsync(int moduleDefinitionId, int siteId) { - return await _http.GetJsonAsync($"{Apiurl}/{moduleDefinitionId.ToString()}?siteid={siteId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/{moduleDefinitionId.ToString()}?siteid={siteId.ToString()}"); } public async Task UpdateModuleDefinitionAsync(ModuleDefinition moduleDefinition) { - await _http.PutJsonAsync($"{Apiurl}/{moduleDefinition.ModuleDefinitionId.ToString()}", moduleDefinition); + await PutJsonAsync($"{Apiurl}/{moduleDefinition.ModuleDefinitionId.ToString()}", moduleDefinition); } public async Task InstallModuleDefinitionsAsync() { - await _http.GetJsonAsync>($"{Apiurl}/install"); + await GetJsonAsync>($"{Apiurl}/install"); } public async Task DeleteModuleDefinitionAsync(int moduleDefinitionId, int siteId) { - await _http.DeleteAsync($"{Apiurl}/{moduleDefinitionId.ToString()}?siteid={siteId.ToString()}"); + await DeleteAsync($"{Apiurl}/{moduleDefinitionId.ToString()}?siteid={siteId.ToString()}"); } public async Task LoadModuleDefinitionsAsync(int siteId, Runtime runtime) @@ -94,7 +94,7 @@ namespace Oqtane.Services } public async Task CreateModuleDefinitionAsync(ModuleDefinition moduleDefinition, int moduleId) { - await _http.PostJsonAsync($"{Apiurl}?moduleid={moduleId.ToString()}", moduleDefinition); + await PostJsonAsync($"{Apiurl}?moduleid={moduleId.ToString()}", moduleDefinition); } } } diff --git a/Oqtane.Client/Services/ModuleService.cs b/Oqtane.Client/Services/ModuleService.cs index c78403e4..897d19e4 100644 --- a/Oqtane.Client/Services/ModuleService.cs +++ b/Oqtane.Client/Services/ModuleService.cs @@ -10,13 +10,13 @@ namespace Oqtane.Services { public class ModuleService : ServiceBase, IModuleService { - private readonly HttpClient _http; + private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public ModuleService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public ModuleService(HttpClient http, SiteState siteState, NavigationManager navigationManager) : base(http) { - _http = http; + _siteState = siteState; _navigationManager = navigationManager; } @@ -28,7 +28,7 @@ namespace Oqtane.Services public async Task> GetModulesAsync(int siteId) { - List modules = await _http.GetJsonAsync>($"{Apiurl}?siteid={siteId.ToString()}"); + List modules = await GetJsonAsync>($"{Apiurl}?siteid={siteId.ToString()}"); modules = modules .OrderBy(item => item.Order) .ToList(); @@ -37,32 +37,32 @@ namespace Oqtane.Services public async Task GetModuleAsync(int moduleId) { - return await _http.GetJsonAsync($"{Apiurl}/{moduleId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/{moduleId.ToString()}"); } public async Task AddModuleAsync(Module module) { - return await _http.PostJsonAsync(Apiurl, module); + return await PostJsonAsync(Apiurl, module); } public async Task UpdateModuleAsync(Module module) { - return await _http.PutJsonAsync($"{Apiurl}/{module.ModuleId.ToString()}", module); + return await PutJsonAsync($"{Apiurl}/{module.ModuleId.ToString()}", module); } public async Task DeleteModuleAsync(int moduleId) { - await _http.DeleteAsync($"{Apiurl}/{moduleId.ToString()}"); + await DeleteAsync($"{Apiurl}/{moduleId.ToString()}"); } public async Task ImportModuleAsync(int moduleId, string content) { - return await _http.PostJsonAsync($"{Apiurl}/import?moduleid={moduleId}", content); + return await PostJsonAsync($"{Apiurl}/import?moduleid={moduleId}", content); } public async Task ExportModuleAsync(int moduleId) { - return await _http.GetStringAsync($"{Apiurl}/export?moduleid={moduleId.ToString()}"); + return await GetStringAsync($"{Apiurl}/export?moduleid={moduleId.ToString()}"); } } } diff --git a/Oqtane.Client/Services/NotificationService.cs b/Oqtane.Client/Services/NotificationService.cs index 46b813b6..35e5ac84 100644 --- a/Oqtane.Client/Services/NotificationService.cs +++ b/Oqtane.Client/Services/NotificationService.cs @@ -10,13 +10,11 @@ namespace Oqtane.Services { public class NotificationService : ServiceBase, INotificationService { - private readonly HttpClient _http; private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public NotificationService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public NotificationService(HttpClient http, SiteState siteState, NavigationManager navigationManager) : base(http) { - _http = http; _siteState = siteState; _navigationManager = navigationManager; } @@ -28,28 +26,29 @@ namespace Oqtane.Services public async Task> GetNotificationsAsync(int siteId, string direction, int userId) { - var notifications = await _http.GetJsonAsync>($"{Apiurl}?siteid={siteId.ToString()}&direction={direction.ToLower()}&userid={userId.ToString()}"); - + var notifications = await GetJsonAsync>($"{Apiurl}?siteid={siteId.ToString()}&direction={direction.ToLower()}&userid={userId.ToString()}"); + return notifications.OrderByDescending(item => item.CreatedOn).ToList(); } public async Task GetNotificationAsync(int notificationId) { - return await _http.GetJsonAsync($"{Apiurl}/{notificationId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/{notificationId.ToString()}"); } public async Task AddNotificationAsync(Notification notification) { - return await _http.PostJsonAsync(Apiurl, notification); + return await PostJsonAsync(Apiurl, notification); } public async Task UpdateNotificationAsync(Notification notification) { - return await _http.PutJsonAsync($"{Apiurl}/{notification.NotificationId.ToString()}", notification); + return await PutJsonAsync($"{Apiurl}/{notification.NotificationId.ToString()}", notification); } + public async Task DeleteNotificationAsync(int notificationId) { - await _http.DeleteAsync($"{Apiurl}/{notificationId.ToString()}"); + await DeleteAsync($"{Apiurl}/{notificationId.ToString()}"); } } } diff --git a/Oqtane.Client/Services/PackageService.cs b/Oqtane.Client/Services/PackageService.cs index 952ea4cb..2876b2e0 100644 --- a/Oqtane.Client/Services/PackageService.cs +++ b/Oqtane.Client/Services/PackageService.cs @@ -10,13 +10,13 @@ namespace Oqtane.Services { public class PackageService : ServiceBase, IPackageService { - private readonly HttpClient _http; + private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public PackageService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public PackageService(HttpClient http, SiteState siteState, NavigationManager navigationManager) : base(http) { - _http = http; + _siteState = siteState; _navigationManager = navigationManager; } @@ -28,13 +28,13 @@ namespace Oqtane.Services public async Task> GetPackagesAsync(string tag) { - List packages = await _http.GetJsonAsync>($"{Apiurl}?tag={tag}"); + List packages = await GetJsonAsync>($"{Apiurl}?tag={tag}"); return packages.OrderByDescending(item => item.Downloads).ToList(); } public async Task DownloadPackageAsync(string packageId, string version, string folder) { - await _http.PostJsonAsync($"{Apiurl}?packageid={packageId}&version={version}&folder={folder}", null); + await PostAsync($"{Apiurl}?packageid={packageId}&version={version}&folder={folder}"); } } } diff --git a/Oqtane.Client/Services/PageModuleService.cs b/Oqtane.Client/Services/PageModuleService.cs index 11fece8c..6c598deb 100644 --- a/Oqtane.Client/Services/PageModuleService.cs +++ b/Oqtane.Client/Services/PageModuleService.cs @@ -8,13 +8,13 @@ namespace Oqtane.Services { public class PageModuleService : ServiceBase, IPageModuleService { - private readonly HttpClient _http; + private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public PageModuleService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public PageModuleService(HttpClient http, SiteState siteState, NavigationManager navigationManager) : base(http) { - _http = http; + _siteState = siteState; _navigationManager = navigationManager; } @@ -26,32 +26,32 @@ namespace Oqtane.Services public async Task GetPageModuleAsync(int pageModuleId) { - return await _http.GetJsonAsync($"{Apiurl}/{pageModuleId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/{pageModuleId.ToString()}"); } public async Task GetPageModuleAsync(int pageId, int moduleId) { - return await _http.GetJsonAsync($"{Apiurl}/{pageId.ToString()}/{moduleId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/{pageId.ToString()}/{moduleId.ToString()}"); } public async Task AddPageModuleAsync(PageModule pageModule) { - return await _http.PostJsonAsync(Apiurl, pageModule); + return await PostJsonAsync(Apiurl, pageModule); } public async Task UpdatePageModuleAsync(PageModule pageModule) { - return await _http.PutJsonAsync($"{Apiurl}/{pageModule.PageModuleId.ToString()}", pageModule); + return await PutJsonAsync($"{Apiurl}/{pageModule.PageModuleId.ToString()}", pageModule); } public async Task UpdatePageModuleOrderAsync(int pageId, string pane) { - await _http.PutJsonAsync($"{Apiurl}/?pageid={pageId.ToString()}&pane={pane}", null); + await PutAsync($"{Apiurl}/?pageid={pageId.ToString()}&pane={pane}"); } public async Task DeletePageModuleAsync(int pageModuleId) { - await _http.DeleteAsync($"{Apiurl}/{pageModuleId.ToString()}"); + await DeleteAsync($"{Apiurl}/{pageModuleId.ToString()}"); } } } diff --git a/Oqtane.Client/Services/PageService.cs b/Oqtane.Client/Services/PageService.cs index 6cc832c7..3edc6db2 100644 --- a/Oqtane.Client/Services/PageService.cs +++ b/Oqtane.Client/Services/PageService.cs @@ -12,13 +12,13 @@ namespace Oqtane.Services { public class PageService : ServiceBase, IPageService { - private readonly HttpClient _http; + private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public PageService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public PageService(HttpClient http, SiteState siteState, NavigationManager navigationManager) : base(http) { - _http = http; + _siteState = siteState; _navigationManager = navigationManager; } @@ -30,26 +30,26 @@ namespace Oqtane.Services public async Task> GetPagesAsync(int siteId) { - List pages = await _http.GetJsonAsync>($"{Apiurl}?siteid={siteId.ToString()}"); + List pages = await GetJsonAsync>($"{Apiurl}?siteid={siteId.ToString()}"); pages = GetPagesHierarchy(pages); return pages; } public async Task GetPageAsync(int pageId) { - return await _http.GetJsonAsync($"{Apiurl}/{pageId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/{pageId.ToString()}"); } public async Task GetPageAsync(int pageId, int userId) { - return await _http.GetJsonAsync($"{Apiurl}/{pageId.ToString()}?userid={userId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/{pageId.ToString()}?userid={userId.ToString()}"); } public async Task GetPageAsync(string path, int siteId) { try { - return await _http.GetJsonAsync($"{Apiurl}/path/{siteId.ToString()}?path={WebUtility.UrlEncode(path)}"); + return await GetJsonAsync($"{Apiurl}/path/{siteId.ToString()}?path={WebUtility.UrlEncode(path)}"); } catch { @@ -59,17 +59,17 @@ namespace Oqtane.Services public async Task AddPageAsync(Page page) { - return await _http.PostJsonAsync(Apiurl, page); + return await PostJsonAsync(Apiurl, page); } public async Task AddPageAsync(int pageId, int userId) { - return await _http.PostJsonAsync($"{Apiurl}/{pageId.ToString()}?userid={userId.ToString()}", null); + return await PostJsonAsync($"{Apiurl}/{pageId.ToString()}?userid={userId.ToString()}", null); } public async Task UpdatePageAsync(Page page) { - return await _http.PutJsonAsync($"{Apiurl}/{page.PageId.ToString()}", page); + return await PutJsonAsync($"{Apiurl}/{page.PageId.ToString()}", page); } public async Task UpdatePageOrderAsync(int siteId, int pageId, int? parentId) @@ -77,12 +77,12 @@ namespace Oqtane.Services var parent = parentId == null ? string.Empty : parentId.ToString(); - await _http.PutJsonAsync($"{Apiurl}/?siteid={siteId.ToString()}&pageid={pageId.ToString()}&parentid={parent}", null); + await PutAsync($"{Apiurl}/?siteid={siteId.ToString()}&pageid={pageId.ToString()}&parentid={parent}"); } public async Task DeletePageAsync(int pageId) { - await _http.DeleteAsync($"{Apiurl}/{pageId.ToString()}"); + await DeleteAsync($"{Apiurl}/{pageId.ToString()}"); } private static List GetPagesHierarchy(List pages) diff --git a/Oqtane.Client/Services/ProfileService.cs b/Oqtane.Client/Services/ProfileService.cs index 4ceb4cac..f7a843b6 100644 --- a/Oqtane.Client/Services/ProfileService.cs +++ b/Oqtane.Client/Services/ProfileService.cs @@ -10,13 +10,13 @@ namespace Oqtane.Services { public class ProfileService : ServiceBase, IProfileService { - private readonly HttpClient _http; + private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public ProfileService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public ProfileService(HttpClient http, SiteState siteState, NavigationManager navigationManager) : base(http) { - _http = http; + _siteState = siteState; _navigationManager = navigationManager; } @@ -28,27 +28,27 @@ namespace Oqtane.Services public async Task> GetProfilesAsync(int siteId) { - List profiles = await _http.GetJsonAsync>($"{Apiurl}?siteid={siteId.ToString()}"); + List profiles = await GetJsonAsync>($"{Apiurl}?siteid={siteId.ToString()}"); return profiles.OrderBy(item => item.ViewOrder).ToList(); } public async Task GetProfileAsync(int profileId) { - return await _http.GetJsonAsync($"{Apiurl}/{profileId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/{profileId.ToString()}"); } public async Task AddProfileAsync(Profile profile) { - return await _http.PostJsonAsync(Apiurl, profile); + return await PostJsonAsync(Apiurl, profile); } public async Task UpdateProfileAsync(Profile profile) { - return await _http.PutJsonAsync($"{Apiurl}/{profile.SiteId.ToString()}", profile); + return await PutJsonAsync($"{Apiurl}/{profile.SiteId.ToString()}", profile); } public async Task DeleteProfileAsync(int profileId) { - await _http.DeleteAsync($"{Apiurl}/{profileId.ToString()}"); + await DeleteAsync($"{Apiurl}/{profileId.ToString()}"); } } } diff --git a/Oqtane.Client/Services/RoleService.cs b/Oqtane.Client/Services/RoleService.cs index 708f0721..cdd64c61 100644 --- a/Oqtane.Client/Services/RoleService.cs +++ b/Oqtane.Client/Services/RoleService.cs @@ -10,13 +10,13 @@ namespace Oqtane.Services { public class RoleService : ServiceBase, IRoleService { - private readonly HttpClient _http; + private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public RoleService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public RoleService(HttpClient http, SiteState siteState, NavigationManager navigationManager) : base(http) { - _http = http; + _siteState = siteState; _navigationManager = navigationManager; } @@ -28,27 +28,27 @@ namespace Oqtane.Services public async Task> GetRolesAsync(int siteId) { - List roles = await _http.GetJsonAsync>($"{Apiurl}?siteid={siteId.ToString()}"); + List roles = await GetJsonAsync>($"{Apiurl}?siteid={siteId.ToString()}"); return roles.OrderBy(item => item.Name).ToList(); } public async Task GetRoleAsync(int roleId) { - return await _http.GetJsonAsync($"{Apiurl}/{roleId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/{roleId.ToString()}"); } public async Task AddRoleAsync(Role role) { - return await _http.PostJsonAsync(Apiurl, role); + return await PostJsonAsync(Apiurl, role); } public async Task UpdateRoleAsync(Role role) { - return await _http.PutJsonAsync($"{Apiurl}/{role.RoleId.ToString()}", role); + return await PutJsonAsync($"{Apiurl}/{role.RoleId.ToString()}", role); } public async Task DeleteRoleAsync(int roleId) { - await _http.DeleteAsync($"{Apiurl}/{roleId.ToString()}"); + await DeleteAsync($"{Apiurl}/{roleId.ToString()}"); } } } diff --git a/Oqtane.Client/Services/ServiceBase.cs b/Oqtane.Client/Services/ServiceBase.cs index 13b5e1c6..a6b80fd6 100644 --- a/Oqtane.Client/Services/ServiceBase.cs +++ b/Oqtane.Client/Services/ServiceBase.cs @@ -1,10 +1,101 @@ using System; +using System.Net; +using System.Net.Http; +using System.Net.Http.Json; +using System.Threading; +using System.Threading.Tasks; using Oqtane.Models; +using Oqtane.Modules.HtmlText.Models; namespace Oqtane.Services { public class ServiceBase { + private readonly HttpClient _http; + + protected ServiceBase(HttpClient client) + { + _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); + } + + protected async Task GetByteArrayAsync(string uri) + { + return await _http.GetByteArrayAsync(uri); + } + + 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); + } + + protected async Task GetJsonAsync(string uri) + { + var response = await _http.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead, CancellationToken.None); + if (CheckResponse(response) && ValidateJsonContent(response.Content)) + { + return await response.Content.ReadFromJsonAsync(); + } + + return default; + } + + private bool CheckResponse(HttpResponseMessage response) + { + if (response.IsSuccessStatusCode) return true; + if (response.StatusCode != HttpStatusCode.NoContent && response.StatusCode != HttpStatusCode.NotFound) + { + //TODO: Log error here + } + + return false; + } + + protected async Task DeleteAsync(string uri) + { + await _http.DeleteAsync(uri); + } + + protected async Task GetStringAsync(string uri) + { + return await _http.GetStringAsync(uri); + } public static string CreateApiUrl(Alias alias, string absoluteUri, string serviceName) { @@ -25,8 +116,9 @@ namespace Oqtane.Services // build a url which ignores any subfolder for multi-tenancy apiurl = $"{uri.Scheme}://{uri.Authority}/~/"; } + apiurl += $"api/{serviceName}"; - + return apiurl; } @@ -37,6 +129,7 @@ namespace Oqtane.Services url += (url.Contains("?")) ? "&" : "?"; url += "aliasid=" + alias.AliasId.ToString(); } + return url; } } diff --git a/Oqtane.Client/Services/SettingService.cs b/Oqtane.Client/Services/SettingService.cs index 22ac7fc4..d966fb92 100644 --- a/Oqtane.Client/Services/SettingService.cs +++ b/Oqtane.Client/Services/SettingService.cs @@ -10,13 +10,13 @@ namespace Oqtane.Services { public class SettingService : ServiceBase, ISettingService { - private readonly HttpClient _http; + private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public SettingService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public SettingService(HttpClient http, SiteState siteState, NavigationManager navigationManager) : base(http) { - _http = http; + _siteState = siteState; _navigationManager = navigationManager; } @@ -99,7 +99,7 @@ namespace Oqtane.Services public async Task> GetSettingsAsync(string entityName, int entityId) { var dictionary = new Dictionary(); - var settings = await _http.GetJsonAsync>($"{Apiurl}?entityname={entityName}&entityid={entityId.ToString()}"); + var settings = await GetJsonAsync>($"{Apiurl}?entityname={entityName}&entityid={entityId.ToString()}"); foreach(Setting setting in settings.OrderBy(item => item.SettingName).ToList()) { @@ -110,7 +110,7 @@ namespace Oqtane.Services public async Task UpdateSettingsAsync(Dictionary settings, string entityName, int entityId) { - var settingsList = await _http.GetJsonAsync>($"{Apiurl}?entityname={entityName}&entityid={entityId.ToString()}"); + var settingsList = await GetJsonAsync>($"{Apiurl}?entityname={entityName}&entityid={entityId.ToString()}"); foreach (KeyValuePair kvp in settings) { @@ -138,22 +138,22 @@ namespace Oqtane.Services public async Task GetSettingAsync(int settingId) { - return await _http.GetJsonAsync($"{Apiurl}/{settingId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/{settingId.ToString()}"); } public async Task AddSettingAsync(Setting setting) { - return await _http.PostJsonAsync(Apiurl, setting); + return await PostJsonAsync(Apiurl, setting); } public async Task UpdateSettingAsync(Setting setting) { - return await _http.PutJsonAsync($"{Apiurl}/{setting.SettingId.ToString()}", setting); + return await PutJsonAsync($"{Apiurl}/{setting.SettingId.ToString()}", setting); } public async Task DeleteSettingAsync(int settingId) { - await _http.DeleteAsync($"{Apiurl}/{settingId.ToString()}"); + await DeleteAsync($"{Apiurl}/{settingId.ToString()}"); } diff --git a/Oqtane.Client/Services/SiteService.cs b/Oqtane.Client/Services/SiteService.cs index ce4a379c..4137b212 100644 --- a/Oqtane.Client/Services/SiteService.cs +++ b/Oqtane.Client/Services/SiteService.cs @@ -10,13 +10,13 @@ namespace Oqtane.Services { public class SiteService : ServiceBase, ISiteService { - private readonly HttpClient _http; + private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public SiteService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public SiteService(HttpClient http, SiteState siteState, NavigationManager navigationManager) : base(http) { - _http = http; + _siteState = siteState; _navigationManager = navigationManager; } @@ -28,28 +28,28 @@ namespace Oqtane.Services public async Task> GetSitesAsync(Alias alias) { - List sites = await _http.GetJsonAsync>(CreateCrossTenantUrl(Apiurl, alias)); + List sites = await GetJsonAsync>(CreateCrossTenantUrl(Apiurl, alias)); return sites.OrderBy(item => item.Name).ToList(); } public async Task GetSiteAsync(int siteId, Alias alias) { - return await _http.GetJsonAsync(CreateCrossTenantUrl($"{Apiurl}/{siteId.ToString()}", alias)); + return await GetJsonAsync(CreateCrossTenantUrl($"{Apiurl}/{siteId.ToString()}", alias)); } public async Task AddSiteAsync(Site site, Alias alias) { - return await _http.PostJsonAsync(CreateCrossTenantUrl(Apiurl, alias), site); + return await PostJsonAsync(CreateCrossTenantUrl(Apiurl, alias), site); } public async Task UpdateSiteAsync(Site site, Alias alias) { - return await _http.PutJsonAsync(CreateCrossTenantUrl($"{Apiurl}/{site.SiteId.ToString()}", alias), site); + return await PutJsonAsync(CreateCrossTenantUrl($"{Apiurl}/{site.SiteId.ToString()}", alias), site); } public async Task DeleteSiteAsync(int siteId, Alias alias) { - await _http.DeleteAsync(CreateCrossTenantUrl($"{Apiurl}/{siteId.ToString()}", alias)); + await DeleteAsync(CreateCrossTenantUrl($"{Apiurl}/{siteId.ToString()}", alias)); } } } diff --git a/Oqtane.Client/Services/SiteTemplateService.cs b/Oqtane.Client/Services/SiteTemplateService.cs index 410f2abc..94800c4f 100644 --- a/Oqtane.Client/Services/SiteTemplateService.cs +++ b/Oqtane.Client/Services/SiteTemplateService.cs @@ -10,13 +10,13 @@ namespace Oqtane.Services { public class SiteTemplateService : ServiceBase, ISiteTemplateService { - private readonly HttpClient _http; + private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public SiteTemplateService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public SiteTemplateService(HttpClient http, SiteState siteState, NavigationManager navigationManager) : base(http) { - _http = http; + _siteState = siteState; _navigationManager = navigationManager; } @@ -28,7 +28,7 @@ namespace Oqtane.Services public async Task> GetSiteTemplatesAsync() { - List siteTemplates = await _http.GetJsonAsync>(Apiurl); + List siteTemplates = await GetJsonAsync>(Apiurl); return siteTemplates.OrderBy(item => item.Name).ToList(); } } diff --git a/Oqtane.Client/Services/SqlService.cs b/Oqtane.Client/Services/SqlService.cs index 52371df8..456aff62 100644 --- a/Oqtane.Client/Services/SqlService.cs +++ b/Oqtane.Client/Services/SqlService.cs @@ -10,13 +10,13 @@ namespace Oqtane.Services { public class SqlService : ServiceBase, ISqlService { - private readonly HttpClient _http; + private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public SqlService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public SqlService(HttpClient http, SiteState siteState, NavigationManager navigationManager) : base(http) { - _http = http; + _siteState = siteState; _navigationManager = navigationManager; } @@ -28,7 +28,7 @@ namespace Oqtane.Services public async Task ExecuteQueryAsync(SqlQuery sqlquery) { - return await _http.PostJsonAsync(Apiurl, sqlquery); + return await PostJsonAsync(Apiurl, sqlquery); } } } diff --git a/Oqtane.Client/Services/TenantService.cs b/Oqtane.Client/Services/TenantService.cs index d325fd91..6ceaa7bf 100644 --- a/Oqtane.Client/Services/TenantService.cs +++ b/Oqtane.Client/Services/TenantService.cs @@ -10,13 +10,13 @@ namespace Oqtane.Services { public class TenantService : ServiceBase, ITenantService { - private readonly HttpClient _http; + private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public TenantService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public TenantService(HttpClient http, SiteState siteState, NavigationManager navigationManager) : base(http) { - _http = http; + _siteState = siteState; _navigationManager = navigationManager; } @@ -28,28 +28,28 @@ namespace Oqtane.Services public async Task> GetTenantsAsync() { - List tenants = await _http.GetJsonAsync>(Apiurl); + List tenants = await GetJsonAsync>(Apiurl); return tenants.OrderBy(item => item.Name).ToList(); } public async Task GetTenantAsync(int tenantId) { - return await _http.GetJsonAsync($"{Apiurl}/{tenantId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/{tenantId.ToString()}"); } public async Task AddTenantAsync(Tenant tenant) { - return await _http.PostJsonAsync(Apiurl, tenant); + return await PostJsonAsync(Apiurl, tenant); } public async Task UpdateTenantAsync(Tenant tenant) { - return await _http.PutJsonAsync($"{Apiurl}/{tenant.TenantId.ToString()}", tenant); + return await PutJsonAsync($"{Apiurl}/{tenant.TenantId.ToString()}", tenant); } public async Task DeleteTenantAsync(int tenantId) { - await _http.DeleteAsync($"{Apiurl}/{tenantId.ToString()}"); + await DeleteAsync($"{Apiurl}/{tenantId.ToString()}"); } } } diff --git a/Oqtane.Client/Services/ThemeService.cs b/Oqtane.Client/Services/ThemeService.cs index e872aae1..41a106ae 100644 --- a/Oqtane.Client/Services/ThemeService.cs +++ b/Oqtane.Client/Services/ThemeService.cs @@ -16,7 +16,7 @@ namespace Oqtane.Services private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public ThemeService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public ThemeService(HttpClient http, SiteState siteState, NavigationManager navigationManager) : base(http) { _http = http; _siteState = siteState; @@ -30,7 +30,7 @@ namespace Oqtane.Services public async Task> GetThemesAsync() { - List themes = await _http.GetJsonAsync>(Apiurl); + List themes = await GetJsonAsync>(Apiurl); // get list of loaded assemblies Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); @@ -105,12 +105,12 @@ namespace Oqtane.Services public async Task InstallThemesAsync() { - await _http.GetJsonAsync>($"{Apiurl}/install"); + await GetJsonAsync>($"{Apiurl}/install"); } public async Task DeleteThemeAsync(string themeName) { - await _http.DeleteAsync($"{Apiurl}/{themeName}"); + await DeleteAsync($"{Apiurl}/{themeName}"); } } } diff --git a/Oqtane.Client/Services/UserRoleService.cs b/Oqtane.Client/Services/UserRoleService.cs index 66e9feb9..a8050788 100644 --- a/Oqtane.Client/Services/UserRoleService.cs +++ b/Oqtane.Client/Services/UserRoleService.cs @@ -9,13 +9,13 @@ namespace Oqtane.Services { public class UserRoleService : ServiceBase, IUserRoleService { - private readonly HttpClient _http; + private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; - public UserRoleService(HttpClient http, SiteState siteState, NavigationManager navigationManager) + public UserRoleService(HttpClient http, SiteState siteState, NavigationManager navigationManager) : base(http) { - _http = http; + _siteState = siteState; _navigationManager = navigationManager; } @@ -27,27 +27,27 @@ namespace Oqtane.Services public async Task> GetUserRolesAsync(int siteId) { - return await _http.GetJsonAsync>($"{Apiurl}?siteid={siteId.ToString()}"); + return await GetJsonAsync>($"{Apiurl}?siteid={siteId.ToString()}"); } public async Task GetUserRoleAsync(int userRoleId) { - return await _http.GetJsonAsync($"{Apiurl}/{userRoleId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/{userRoleId.ToString()}"); } public async Task AddUserRoleAsync(UserRole userRole) { - return await _http.PostJsonAsync(Apiurl, userRole); + return await PostJsonAsync(Apiurl, userRole); } public async Task UpdateUserRoleAsync(UserRole userRole) { - return await _http.PutJsonAsync($"{Apiurl}/{userRole.UserRoleId.ToString()}", userRole); + return await PutJsonAsync($"{Apiurl}/{userRole.UserRoleId.ToString()}", userRole); } public async Task DeleteUserRoleAsync(int userRoleId) { - await _http.DeleteAsync($"{Apiurl}/{userRoleId.ToString()}"); + await DeleteAsync($"{Apiurl}/{userRoleId.ToString()}"); } } } diff --git a/Oqtane.Client/Services/UserService.cs b/Oqtane.Client/Services/UserService.cs index d16142b3..5e38485c 100644 --- a/Oqtane.Client/Services/UserService.cs +++ b/Oqtane.Client/Services/UserService.cs @@ -8,14 +8,14 @@ namespace Oqtane.Services { public class UserService : ServiceBase, IUserService { - private readonly HttpClient _http; + private readonly SiteState _siteState; private readonly NavigationManager _navigationManager; private readonly ISiteService _siteService; - public UserService(HttpClient http, SiteState siteState, NavigationManager navigationManager, ISiteService siteService) + public UserService(HttpClient http, SiteState siteState, NavigationManager navigationManager, ISiteService siteService) : base(http) { - _http = http; + _siteState = siteState; _navigationManager = navigationManager; _siteService = siteService; @@ -28,12 +28,12 @@ namespace Oqtane.Services public async Task GetUserAsync(int userId, int siteId) { - return await _http.GetJsonAsync($"{Apiurl}/{userId.ToString()}?siteid={siteId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/{userId.ToString()}?siteid={siteId.ToString()}"); } public async Task GetUserAsync(string username, int siteId) { - return await _http.GetJsonAsync($"{Apiurl}/name/{username}?siteid={siteId.ToString()}"); + return await GetJsonAsync($"{Apiurl}/name/{username}?siteid={siteId.ToString()}"); } public async Task AddUserAsync(User user) @@ -47,7 +47,7 @@ namespace Oqtane.Services try { - return await _http.PostJsonAsync(Apiurl, user); + return await PostJsonAsync(Apiurl, user); } catch { @@ -59,7 +59,7 @@ namespace Oqtane.Services { try { - return await _http.PostJsonAsync(CreateCrossTenantUrl(Apiurl, alias), user); + return await PostJsonAsync(CreateCrossTenantUrl(Apiurl, alias), user); } catch { @@ -69,37 +69,37 @@ namespace Oqtane.Services public async Task UpdateUserAsync(User user) { - return await _http.PutJsonAsync($"{Apiurl}/{user.UserId.ToString()}", user); + return await PutJsonAsync($"{Apiurl}/{user.UserId.ToString()}", user); } public async Task DeleteUserAsync(int userId) { - await _http.DeleteAsync($"{Apiurl}/{userId.ToString()}"); + await DeleteAsync($"{Apiurl}/{userId.ToString()}"); } public async Task LoginUserAsync(User user, bool setCookie, bool isPersistent) { - return await _http.PostJsonAsync($"{Apiurl}/login?setcookie={setCookie.ToString()}&persistent={isPersistent.ToString()}", user); + return await PostJsonAsync($"{Apiurl}/login?setcookie={setCookie.ToString()}&persistent={isPersistent.ToString()}", user); } public async Task LogoutUserAsync(User user) { // best practices recommend post is preferrable to get for logout - await _http.PostJsonAsync($"{Apiurl}/logout", user); + await PostJsonAsync($"{Apiurl}/logout", user); } public async Task VerifyEmailAsync(User user, string token) { - return await _http.PostJsonAsync($"{Apiurl}/verify?token={token}", user); + return await PostJsonAsync($"{Apiurl}/verify?token={token}", user); } public async Task ForgotPasswordAsync(User user) { - await _http.PostJsonAsync($"{Apiurl}/forgot", user); + await PostJsonAsync($"{Apiurl}/forgot", user); } public async Task ResetPasswordAsync(User user, string token) { - return await _http.PostJsonAsync($"{Apiurl}/reset?token={token}", user); + return await PostJsonAsync($"{Apiurl}/reset?token={token}", user); } } diff --git a/Oqtane.Server/Controllers/AliasController.cs b/Oqtane.Server/Controllers/AliasController.cs index 6e5f6379..eed15943 100644 --- a/Oqtane.Server/Controllers/AliasController.cs +++ b/Oqtane.Server/Controllers/AliasController.cs @@ -63,10 +63,12 @@ namespace Oqtane.Controllers } // get sync events - alias.SyncDate = DateTime.UtcNow; - alias.SyncEvents = _syncManager.GetSyncEvents(DateTime.ParseExact(lastsyncdate, "yyyyMMddHHmmssfff", CultureInfo.InvariantCulture)); - - return alias; + if (alias != null) + { + alias.SyncDate = DateTime.UtcNow; + alias.SyncEvents = _syncManager.GetSyncEvents(DateTime.ParseExact(lastsyncdate, "yyyyMMddHHmmssfff", CultureInfo.InvariantCulture)); + } + return alias; } // POST api/ diff --git a/Oqtane.Server/Startup.cs b/Oqtane.Server/Startup.cs index a5dfe59b..917632b6 100644 --- a/Oqtane.Server/Startup.cs +++ b/Oqtane.Server/Startup.cs @@ -42,12 +42,7 @@ namespace Oqtane // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { - services.AddMvc(opt => - { - // remove formatter that turns nulls into 204 - No Content responses - // sends JSON null instead - opt.OutputFormatters.RemoveType(); - }).AddNewtonsoftJson(); + services.AddMvc().AddNewtonsoftJson(); services.AddServerSideBlazor(); // setup HttpClient for server side in a client side compatible fashion ( with auth cookie )