PowerShell 技能连载 - OpenAPI 集成技巧

在 PowerShell 中集成 OpenAPI 是一项重要任务,本文将介绍一些实用的 OpenAPI 集成技巧。

首先,让我们看看基本的 OpenAPI 操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 创建 OpenAPI 客户端生成函数
function New-OpenAPIClient {
param(
[string]$OpenAPISpec,
[string]$OutputPath,
[string]$ClientName,
[string]$Namespace
)

try {
$swagger = Get-Content $OpenAPISpec -Raw | ConvertFrom-Json

$clientCode = @"
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Text.Json;

namespace $Namespace {
public class $ClientName {
private readonly HttpClient _client;
private readonly string _baseUrl;

public $ClientName(string baseUrl) {
_baseUrl = baseUrl;
_client = new HttpClient();
}

// API 方法将在这里生成
}
}
"@

$clientCode | Out-File -FilePath "$OutputPath\$ClientName.cs" -Encoding UTF8
Write-Host "OpenAPI 客户端生成成功:$OutputPath\$ClientName.cs"
}
catch {
Write-Host "OpenAPI 客户端生成失败:$_"
}
}

OpenAPI 验证:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 创建 OpenAPI 规范验证函数
function Test-OpenAPISpec {
param(
[string]$OpenAPISpec,
[string[]]$ValidationRules
)

try {
$swagger = Get-Content $OpenAPISpec -Raw | ConvertFrom-Json
$results = @()

foreach ($rule in $ValidationRules) {
switch ($rule) {
'Version' {
if (-not $swagger.openapi) {
$results += "OpenAPI 版本未指定"
}
}
'Paths' {
if (-not $swagger.paths) {
$results += "未定义任何 API 路径"
}
}
'Schemas' {
if (-not $swagger.components.schemas) {
$results += "未定义任何数据模型"
}
}
'Security' {
if (-not $swagger.security) {
$results += "未定义安全要求"
}
}
}
}

return [PSCustomObject]@{
SpecFile = $OpenAPISpec
ValidationResults = $results
IsValid = $results.Count -eq 0
}
}
catch {
Write-Host "OpenAPI 规范验证失败:$_"
}
}

OpenAPI 文档生成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 创建 OpenAPI 文档生成函数
function New-OpenAPIDoc {
param(
[string]$OpenAPISpec,
[string]$OutputPath,
[ValidateSet('HTML', 'Markdown', 'PDF')]
[string]$Format
)

try {
$swagger = Get-Content $OpenAPISpec -Raw | ConvertFrom-Json

switch ($Format) {
'HTML' {
$template = Get-Content ".\templates\openapi-html.html" -Raw
$doc = $template.Replace("{{spec}}", $swagger)
$doc | Out-File -FilePath "$OutputPath\api-doc.html" -Encoding UTF8
}
'Markdown' {
$doc = "# API 文档`n`n"
foreach ($path in $swagger.paths.PSObject.Properties) {
$doc += "## $($path.Name)`n`n"
foreach ($method in $path.Value.PSObject.Properties) {
$doc += "### $($method.Name)`n`n"
$doc += "**描述:** $($method.Value.summary)`n`n"
$doc += "**参数:**`n`n"
foreach ($param in $method.Value.parameters) {
$doc += "- $($param.name) ($($param.in)): $($param.description)`n"
}
$doc += "`n"
}
}
$doc | Out-File -FilePath "$OutputPath\api-doc.md" -Encoding UTF8
}
'PDF' {
# 使用 Markdown 生成 PDF
$markdown = New-OpenAPIDoc -OpenAPISpec $OpenAPISpec -OutputPath $OutputPath -Format Markdown
pandoc "$OutputPath\api-doc.md" -o "$OutputPath\api-doc.pdf"
}
}

Write-Host "OpenAPI 文档生成成功:$OutputPath"
}
catch {
Write-Host "OpenAPI 文档生成失败:$_"
}
}

OpenAPI 测试用例生成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 创建 OpenAPI 测试用例生成函数
function New-OpenAPITests {
param(
[string]$OpenAPISpec,
[string]$OutputPath,
[string]$TestFramework
)

try {
$swagger = Get-Content $OpenAPISpec -Raw | ConvertFrom-Json

switch ($TestFramework) {
'Pester' {
$testCode = "Describe 'API Tests' {`n"
foreach ($path in $swagger.paths.PSObject.Properties) {
foreach ($method in $path.Value.PSObject.Properties) {
$testCode += " It 'Should $($method.Value.summary)' {`n"
$testCode += " # 测试代码将在这里生成`n"
$testCode += " }`n"
}
}
$testCode += "}"
$testCode | Out-File -FilePath "$OutputPath\api.tests.ps1" -Encoding UTF8
}
'xUnit' {
$testCode = "using Xunit;`n`n"
$testCode += "public class APITests {`n"
foreach ($path in $swagger.paths.PSObject.Properties) {
foreach ($method in $path.Value.PSObject.Properties) {
$testCode += " [Fact]`n"
$testCode += " public void Should_$($method.Value.summary.Replace(' ', '_'))() {`n"
$testCode += " // 测试代码将在这里生成`n"
$testCode += " }`n"
}
}
$testCode += "}"
$testCode | Out-File -FilePath "$OutputPath\ApiTests.cs" -Encoding UTF8
}
}

Write-Host "OpenAPI 测试用例生成成功:$OutputPath"
}
catch {
Write-Host "OpenAPI 测试用例生成失败:$_"
}
}

OpenAPI 监控:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 创建 OpenAPI 监控函数
function Monitor-OpenAPIEndpoints {
param(
[string]$OpenAPISpec,
[string]$BaseUrl,
[int]$Interval = 300,
[int]$Duration = 3600
)

try {
$swagger = Get-Content $OpenAPISpec -Raw | ConvertFrom-Json
$endTime = Get-Date
$startTime = $endTime.AddSeconds(-$Duration)
$results = @()

while ($startTime -lt $endTime) {
foreach ($path in $swagger.paths.PSObject.Properties) {
foreach ($method in $path.Value.PSObject.Properties) {
$url = "$BaseUrl$($path.Name)"
$response = Invoke-RestMethod -Uri $url -Method $method.Name

$results += [PSCustomObject]@{
Time = Get-Date
Endpoint = $url
Method = $method.Name
StatusCode = $response.StatusCode
ResponseTime = $response.Time
}
}
}

$startTime = $startTime.AddSeconds($Interval)
Start-Sleep -Seconds $Interval
}

return [PSCustomObject]@{
BaseUrl = $BaseUrl
Duration = $Duration
Interval = $Interval
Results = $results
}
}
catch {
Write-Host "OpenAPI 监控失败:$_"
}
}

这些技巧将帮助您更有效地集成 OpenAPI。记住,在处理 OpenAPI 时,始终要注意版本兼容性和安全性。同时,建议使用适当的错误处理和日志记录机制来跟踪所有操作。

PowerShell 技能连载 - OpenAPI 集成技巧

http://blog.vichamp.com/2024/11/27/powershell-openapi-integration/

作者

吴波

发布于

2024-11-27

更新于

2025-03-25

许可协议

评论