0

What is the best way to parallel process Invoke-RestMethod?

I'm trying to retrieve a list of coin prices, currently I'm using a foreach loop to retrieve the price. Doing this with 30 odd coins takes a while:

$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    
    }
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328

1 Answers1

1

The base url returns all the prices... so instead of filtering it in a loop, you can grab them all and parse them as a 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"
gvee
  • 16,732
  • 35
  • 50