【问题标题】:PowerShell URI template parsingPowerShell URI 模板解析
【发布时间】:2019-11-18 21:53:41
【问题描述】:

我希望使用Octopus REST API,我想充分利用 Octopus REST API 中提供的HATEOAS links,其中一些使用URI Templates

我在 Octopus 论坛 here 上找到了一篇相当老的帖子,但我想知道 4 年多过去了,是否有更好的解决方案用于在 PowerShell 中解析 Uri 模板。

如果没有,我可以使用论坛帖子中列出的 .NET Uri 模板解析包。

【问题讨论】:

    标签: .net powershell rest octopus-deploy hateoas


    【解决方案1】:

    所以我找不到任何原生解决方案,但我将分享一个实现 Resta.UriTemplates 的示例。


    # Example adapted from: https://github.com/a7b0/uri-templates#examples
    
    Install-Package Resta.UriTemplates -Version 1.3.0
    
    # Import the Resta.UriTemplates assembly
    $Path = '.\Resta.UriTemplates.1.3.0\lib\netstandard2.0\Resta.UriTemplates.dll'
    Add-Type -Path $Path -PassThru | Select-Object -ExpandProperty Assembly | Select-Object -ExpandProperty FullName -Unique
    # Returns: 
    # Resta.UriTemplates, Version=1.3.0.0, Culture=neutral, PublicKeyToken=null
    
    $template = [Resta.UriTemplates.UriTemplate]'http://example.org/{area}/news{?type,count}'
    
    Write-Output $template
    # Returns:
    # Template                                    Variables
    # --------                                    ---------
    # http://example.org/{area}/news{?type,count} {area, type, count}
    
    $dict = New-Object 'system.collections.generic.dictionary[string,object]'
    $dict["area"] = "world"
    $dict["type"] = "actual"
    $dict["count"] = "10"
    
    $template.Resolve($dict)
    # Returns: 
    # http://example.org/world/news?type=actual&count=10
    

    【讨论】:

      猜你喜欢
      • 2011-08-10
      • 2012-12-30
      • 1970-01-01
      • 2016-10-27
      • 2017-05-18
      • 2023-01-26
      • 2018-01-27
      • 1970-01-01
      • 2020-12-06
      相关资源
      最近更新 更多