Check if API respons before executing statement
Mostrar comentarios más antiguos
Hello!
I have a code that access an API, and gathers data from it. The API stores values from sales of cryptos currencies, and thus the API is updated frequently. My problem is that sometimes the API is unresponsive, and in that causes the code stops. I need the code to run regardless if the website does not respond. My take on this problem is to create a function that checks (using webread) if the website responds before trying to access the data. This is done by checking if the function returns a value (if isinteger(int8(S.Data.AskPrice)) == 1), and if not, the loop will pause for 150 Seconds. This method should work, but the webread-function fails if no response is received (and thus the script stops). I have considered ('Timeout',15), but since I don't know how long the API is unresponsive, this method does not suffice for me, since I'd like to have some consistency in my data.
My questions is:
- Is there a way to check if an API responds without using webread?
- If not, is it possible to disable the error, and just keep the script running until it gets connection again?
The code:
tic
api = 'https://www.cryptopia.co.nz/api/GetMarket/';
url = [api '5662/'];
options = weboptions('ContentType','json','Timeout',15);
S = webread(url,options);
if isinteger(int8(S.Data.AskPrice)) == 1
for t=1:T
api = 'https://www.cryptopia.co.nz/api/GetMarket/';
url = [api '5662/'];
options = weboptions('ContentType','json','Timeout',15);
S = webread(url,options);
end
else
pause(150)
end
toc
Respuestas (0)
Categorías
Más información sobre MATLAB Mobile en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!