How to call web services/API?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

How to call web services/API?

Post 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.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
MrFariator
Party member
Posts: 548
Joined: Wed Oct 05, 2016 11:53 am

Re: How to call web services/API?

Post 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.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: How to call web services/API?

Post by pgimeno »

I think the main catch is actually that https is not supported.
MrFariator
Party member
Posts: 548
Joined: Wed Oct 05, 2016 11:53 am

Re: How to call web services/API?

Post by MrFariator »

Right, I forgot about https specifically. Of course, that depends on if you can just roll with http or not.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to call web services/API?

Post by zorg »

pgimeno wrote: Wed Oct 05, 2022 7:46 pm I think the main catch is actually that https is not supported.
yet* :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: How to call web services/API?

Post 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. :)
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
User avatar
dusoft
Party member
Posts: 634
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: How to call web services/API?

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests