并行处理Invoke-RestMethod

时间:2018-01-22 15:37:32

标签: rest powershell

并行处理Invoke-RestMethod的最佳方式是什么?

我正在尝试检索硬币价格列表,目前我正在使用foreach循环来检索价格。用30多个硬币做这件事需要一段时间:

$coins = @("XRP", "TRX", "VEN", "CND", "ICX", "XLM", "BNB")
$base = "ETH"

while ($true) {
    foreach ($coin in $coins) {
        $pair = $coin + $base
        $uri = "https://api.binance.com/api/v3/ticker/price?symbol=$pair"
        $price = Invoke-RestMethod -Method Get -URI $uri |
                 select Price -ExpandProperty Price
        Write-Host $price    
    }
}

1 个答案:

答案 0 :(得分:1)

基本网址返回所有价格...所以不是在循环中过滤它,而是可以全部抓取它们并将它们解析为PSObject

$uri    = "https://api.binance.com/api/v3/ticker/price"
$request = Invoke-WebRequest -Method Get -URI $uri 
$prices = $request.Content | ConvertFrom-Json
$prices | Where-Object symbol -IN "XRPETH", "TRXETH", "VENETH", "CNDETH", "ICXETH", "XLMETH", "BNBETH"