You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.0 KiB
65 lines
2.0 KiB
using Application.Models;
|
|
using Infrastructure.Extensions;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace IoT.Shared.Controllers
|
|
{
|
|
public class BaseDeviceController : Controller
|
|
{
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
public BaseDeviceController(IHttpClientFactory httpClientFactory)
|
|
{
|
|
this._httpClientFactory = httpClientFactory;
|
|
}
|
|
|
|
[ApiExplorerSettings(IgnoreApi = true)]
|
|
public string GetApiJson()
|
|
{
|
|
var url = new UriBuilder
|
|
{
|
|
Host = Request.Host.Host,
|
|
Port = Request.Host.Port ?? 80,
|
|
Path = "/swagger/v1/swagger.json"
|
|
}.ToString();
|
|
var prefix = $"/{RouteData.Values["controller"]}/";
|
|
var hc = this._httpClientFactory.CreateClient();
|
|
var result = hc.GetStringAsync(url).Result;
|
|
var json = JsonConvert.DeserializeObject(result) as JObject;
|
|
var paths = json.Properties().FirstOrDefault(o => o.Name == "paths").Value as JObject;
|
|
var names = paths.Properties().Select(o => o.Name).ToList();
|
|
foreach (var item in names)
|
|
{
|
|
if (!item.StartsWith(prefix))
|
|
{
|
|
paths.Remove(item);
|
|
}
|
|
}
|
|
var realResult = JsonConvert.SerializeObject(json);
|
|
return realResult;
|
|
}
|
|
|
|
public ApiResponse AsyncAction(Action action)
|
|
{
|
|
try
|
|
{
|
|
Task.Run(() =>
|
|
{
|
|
action();
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return ApiResponse.Error(ex.GetMessage());
|
|
}
|
|
return ApiResponse.AsyncSuccess();
|
|
}
|
|
}
|
|
} |