Page 1 of 1
How to call web services/API?
Posted: Wed Oct 05, 2022 1:01 pm
by togFox
I'd like to call an API that is "out there" so I can display live data on my Love2D screen. Think of a stock ticker or live hockey score updates. Here is an example:
https://rapidapi.com/asepscareer/api/yahoo-finance97
How would I invoke something like that in Love/LUA? Is POST within the realms of possibility? Would I need FFI? How do I get started on something like this? Thanks.
Re: How to call web services/API?
Posted: Wed Oct 05, 2022 1:34 pm
by MrFariator
Love2d provides
luasocket out of the box, and you can use it to implement some simple POST requests. From the
reference, the
request page has relevant details, and there are fair few luasocket tutorials out there. The main catch is that it may be wise to run the networking requests on a separate thread (in case of a blocking operation), so not to block the main thread execution.
Re: How to call web services/API?
Posted: Wed Oct 05, 2022 7:46 pm
by pgimeno
I think the main catch is actually that https is not supported.
Re: How to call web services/API?
Posted: Wed Oct 05, 2022 10:40 pm
by MrFariator
Right, I forgot about https specifically. Of course, that depends on if you can just roll with http or not.
Re: How to call web services/API?
Posted: Wed Oct 05, 2022 10:42 pm
by zorg
pgimeno wrote: ↑Wed Oct 05, 2022 7:46 pm
I think the main catch is actually that https is not supported.
yet*
Re: How to call web services/API?
Posted: Fri Oct 07, 2022 12:55 pm
by togFox
So it seems all currency exchange API's use https. Is that not gunna work with Love2D? Are there https libraries I should google for? Should I mess about with this:
https://github.com/love2d/lua-https
Or this:
https://github.com/daurnimator/lua-http (supports https 1.2)
I'll keep googling for a http but all the sites I've seen have an identical look and feel (same company - same API - same https) but just researching this alone is educational.
Edit: this site offers http:
http://api.nbp.pl/en.html
and by typing into a browser:
http://api.nbp.pl/api/exchangerates/tables/a/
I get the following xml:
Code: Select all
<ArrayOfExchangeRatesTable>
<ExchangeRatesTable>
<Table>A</Table>
<No>195/A/NBP/2022</No>
<EffectiveDate>2022-10-07</EffectiveDate>
<Rates>
<Rate>
<Currency>bat (Tajlandia)</Currency>
<Code>THB</Code>
<Mid>0.1326</Mid>
</Rate>
<Rate>
<Currency>dolar amerykański</Currency>
<Code>USD</Code>
<Mid>4.9588</Mid>
</Rate>
...
...
I think this is enough for me to play and learn.
Re: How to call web services/API?
Posted: Sun Oct 09, 2022 4:00 pm
by dusoft
togFox wrote: ↑Fri Oct 07, 2022 12:55 pm
So it seems all currency exchange API's use https. Is that not gunna work with Love2D? Are there https libraries I should google for? Should I mess about with this:
https://github.com/love2d/lua-https
Or this:
https://github.com/daurnimator/lua-http (supports https 1.2)
I'll keep googling for a http but all the sites I've seen have an identical look and feel (same company - same API - same https) but just researching this alone is educational.
[snip...]
You could use CURL binding
https://github.com/Lua-cURL/Lua-cURLv3 instead