Correct use of forEach with newArrayList

Hi, I am using , is this the correct use of forEach with a String List

import java.util.List
val List<String> stocks = newArrayList("BTC", "DGC", "ETC", "ETH", "GRC", "LTC")  // crypto

rule "Get share prices"
    System started or
    Time cron "0 1/15 * ? * * *"
    logInfo("org.openhab","Shares: Obtaining the latest prices.")
    stocks.forEach[ stock  , i |
            val csv = sendHttpGetRequest(""+stock+"&market=GBP&datatype=csv&apikey=xxxxx").split("\n")
            val firstLine = csv.get(1).split(",")
            // 4th item (close price)
            val price = firstLine.get(4).trim
            sendCommand(stock +"_Current_Price", price)
            logInfo("share.rules",stock +"_Current_Price "+price)
	logInfo("org.openhab","Shares: Updated completed.")

the code is fetching the first value correct and populating the state, but not looping around and passes the error

2021-03-21 19:11:07.751 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Error during the execution of startup rule 'Get share prices': 4

this is with OH 2.5.12 ,

comments appreciated

Put some logInfo() in and see what’s going on.
For example, confident that service lets you hit it rapidly with multiple http requests? i.e. what are csv contents?

the 1st one comes back fine , ahhh i see regarding the 2nd fetch speed, i would guess thats at OH poll speed

Found it, thankyou, I was not watching (closely) the undecoded responses from the server, it appears some of the listed currencies are not “used” there fore returns ‘Invalid API call’

the code appears good,as is the fetch speed I guess

1 Like

If this service offers an API like this, almost certainly they will offer an API that lets you query for and retrieve all the prices at once in one call which you can then parse out.

Most services like this will rate limit you to prevent abusing the API.