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.
iot/projects/IoT.Shared/Infrastructure/OpenApiService.cs

61 lines
2.5 KiB

using Application.Domain.Entities;
using Infrastructure.Extensions;
using Microsoft.OpenApi.Readers;
using System;
using System.Linq;
namespace IoT.Shared.Infrastructure
{
public static class OpenApiService
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")]
public static void UpdateApi(Product product)
{
if (product == null || string.IsNullOrEmpty(product.ApiJson))
{
return;
}
try
{
var reader = new OpenApiStringReader(new OpenApiReaderSettings { }).Read(product.ApiJson, out OpenApiDiagnostic diagnostic);
foreach (var path in reader.Paths)
{
foreach (var operation in path.Value.Operations)
{
if (!product.Apis.Any(o => o.Name == operation.Value.Summary))
{
var postion = path.Key.LastIndexOf('/') + 1;
var api = new Api
{
Path = path.Key.Substring(0, postion),
Command = path.Key.Substring(postion),
Name = operation.Value.Summary,
Method = operation.Key.ToString()
};
product.Apis.Add(api);
foreach (var parameter in operation.Value.Parameters)
{
if (!api.Parameters.Any(o => o.Name == parameter.Name))
{
api.Parameters.Add(new Parameter
{
Name = parameter.Name,
Description = parameter.Description,
Required = parameter.Required,
Type = parameter.Schema.Type,
Minimum = parameter.Schema.Minimum?.ToString(),
Maxinum = parameter.Schema.Maximum?.ToString()
});
}
}
}
}
}
}
catch (Exception ex)
{
ex.PrintStack();
}
}
}
}