Browse Source

feat: Refactor API scripting and model provider to support asynchronous operations

pull/25022/head
maliming 3 weeks ago
parent
commit
60ff0218ec
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 2
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApiExploring/AbpApiDefinitionController.cs
  2. 5
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs
  3. 7
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ProxyScripting/AbpServiceProxyScriptController.cs
  4. 5
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/IApiDescriptionModelProvider.cs
  5. 4
      framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/IProxyScriptManager.cs
  6. 2
      framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/IProxyScriptManagerCache.cs
  7. 13
      framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/ProxyScriptManager.cs
  8. 5
      framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/ProxyScriptManagerCache.cs

2
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApiExploring/AbpApiDefinitionController.cs

@ -1,4 +1,4 @@
using System.Threading.Tasks;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Http.Modeling;

5
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs

@ -51,11 +51,6 @@ public class AspNetCoreApiDescriptionModelProvider : IApiDescriptionModelProvide
Logger = NullLogger<AspNetCoreApiDescriptionModelProvider>.Instance;
}
public ApplicationApiDescriptionModel CreateApiModel(ApplicationApiDescriptionModelRequestDto input)
{
return AsyncHelper.RunSync(() => CreateApiModelAsync(input));
}
public virtual async Task<ApplicationApiDescriptionModel> CreateApiModelAsync(ApplicationApiDescriptionModelRequestDto input)
{
//TODO: Can cache the model?

7
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ProxyScripting/AbpServiceProxyScriptController.cs

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Volo.Abp.Auditing;
using Volo.Abp.Http;
@ -29,11 +30,11 @@ public class AbpServiceProxyScriptController : AbpController
[HttpGet]
[Produces(MimeTypes.Application.Javascript, MimeTypes.Text.Plain)]
public virtual ActionResult GetAll(ServiceProxyGenerationModel model)
public virtual async Task<ActionResult> GetAll(ServiceProxyGenerationModel model)
{
model.Normalize();
var script = ProxyScriptManager.GetScript(model.CreateOptions());
var script = await ProxyScriptManager.GetScriptAsync(model.CreateOptions());
return Content(
Options.MinifyGeneratedScript == true

5
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/IApiDescriptionModelProvider.cs

@ -4,8 +4,5 @@ namespace Volo.Abp.Http.Modeling;
public interface IApiDescriptionModelProvider
{
ApplicationApiDescriptionModel CreateApiModel(ApplicationApiDescriptionModelRequestDto input);
Task<ApplicationApiDescriptionModel> CreateApiModelAsync(ApplicationApiDescriptionModelRequestDto input)
=> Task.FromResult(CreateApiModel(input));
Task<ApplicationApiDescriptionModel> CreateApiModelAsync(ApplicationApiDescriptionModelRequestDto input);
}

4
framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/IProxyScriptManager.cs

@ -1,6 +1,8 @@
using System.Threading.Tasks;
namespace Volo.Abp.Http.ProxyScripting;
public interface IProxyScriptManager
{
string GetScript(ProxyScriptingModel scriptingModel);
Task<string> GetScriptAsync(ProxyScriptingModel scriptingModel);
}

2
framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/IProxyScriptManagerCache.cs

@ -6,5 +6,7 @@ public interface IProxyScriptManagerCache
{
string GetOrAdd(string key, Func<string> factory);
bool TryGet(string key, out string? value);
void Set(string key, string value);
}

13
framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/ProxyScriptManager.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
@ -32,23 +33,23 @@ public class ProxyScriptManager : IProxyScriptManager, ITransientDependency
_options = options.Value;
}
public string GetScript(ProxyScriptingModel scriptingModel)
public async Task<string> GetScriptAsync(ProxyScriptingModel scriptingModel)
{
var cacheKey = CreateCacheKey(scriptingModel);
if (scriptingModel.UseCache)
if (scriptingModel.UseCache && _cache.TryGet(cacheKey, out var cached))
{
return _cache.GetOrAdd(cacheKey, () => CreateScript(scriptingModel));
return cached!;
}
var script = CreateScript(scriptingModel);
var script = await CreateScriptAsync(scriptingModel);
_cache.Set(cacheKey, script);
return script;
}
private string CreateScript(ProxyScriptingModel scriptingModel)
private async Task<string> CreateScriptAsync(ProxyScriptingModel scriptingModel)
{
var apiModel = _modelProvider.CreateApiModel(new ApplicationApiDescriptionModelRequestDto { IncludeTypes = false });
var apiModel = await _modelProvider.CreateApiModelAsync(new ApplicationApiDescriptionModelRequestDto { IncludeTypes = false });
if (scriptingModel.IsPartialRequest())
{

5
framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/ProxyScriptManagerCache.cs

@ -19,6 +19,11 @@ public class ProxyScriptManagerCache : IProxyScriptManagerCache, ISingletonDepen
return _cache.GetOrAdd(key, factory);
}
public bool TryGet(string key, out string? value)
{
return _cache.TryGetValue(key, out value);
}
public void Set(string key, string value)
{
_cache[key] = value;

Loading…
Cancel
Save