Learning to use APIs

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
egolombek
Posts: 74
Joined: Thu Jan 30, 2020 2:11 pm

Learning to use APIs

Post by egolombek » Wed Jun 10, 2020 11:25 am

(I really hope I am not asking too many questions -- tell me if I am overasking! Don't know the netiquette)

I am looking to write an app that pulls in current stock prices using an API like Morningstar's API. But, I have no idea how to use an API with LiveCode. Does anyone know where I can learn?

Thanks! Eric

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Learning to use APIs

Post by bogs » Wed Jun 10, 2020 11:46 am

Cyril had a pretty good lesson on it here. In fact, his whole site is full of lessons, tidbits, code, and all kinds of other useful things, I'd HIGHLY recommend starting on the home page after you find out what you want to know.
Image

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Learning to use APIs

Post by dunbarx » Wed Jun 10, 2020 1:41 pm

Exactly the opposite. You are not asking enough questions. Several people on this forum live for this.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Learning to use APIs

Post by FourthWorld » Wed Jun 10, 2020 3:32 pm

Like Craig says, you cannot ask too many questions. Feel free to fire away any that come to mind.

Most APIs use HTTP. How well do you understand HTTP? Once you grasp the basics of that protocol, using it to obtain data from a server gets much easier.

More challenging than using the API itself can be authenticating, usually a first step before the request for data can be sent to a server. The two most common authentication methods are HTTP Basic Authentication (which embeds the user name and password in the request URL), and OAuth2.

OAuth2 is well supported by a library in LiveCode on all platforms but Linux. It's easy to use, but it's helpful to understand how AOuth2 works too use it well. There are many resources on the web for learning how OAuth2 works.

Which authentication method is prescribed in the Morningstar API docs?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

egolombek
Posts: 74
Joined: Thu Jan 30, 2020 2:11 pm

Re: Learning to use APIs

Post by egolombek » Wed Jun 10, 2020 3:45 pm

I really, really appreciate that. I feel like I shouldn't be wasting people's time, but I am so at the beginning of my programming journey. This forum has really helped me.

I went to a place called "Rapid API" where I signed up for the Morning Star API. They offer snippets of code in like 20 languages, but not LiveCode (of course :( ) So, I have copied JAVA (unirest) of which I know absolutely none. Can anyone figure out from this how to translate to LiveCode?
HttpResponse<String> response = Unirest.get("https://morning-star.p.rapidapi.com/sto ... 0P0000OQN8")
.header("x-rapidapi-host", "morning-star.p.rapidapi.com")
.header("x-rapidapi-key", "XXXXX")
.asString();
Here it is in Python:
headers = {
'x-rapidapi-host': "morning-star.p.rapidapi.com",
'x-rapidapi-key': "XXXXX"
}

conn.request("GET", "/stock/v2/get-mini-chart-realtime-data?performanceId=0P0000OQN8", headers=headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))
But to answer your question, I do not know HTTP or oAuth2. (I don't even really know what those refer to...) In this case, I am hoping to learn how I can access data, without necessarily learning all the mechanics. If possible...
Last edited by egolombek on Wed Jun 10, 2020 9:29 pm, edited 1 time in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Learning to use APIs

Post by FourthWorld » Wed Jun 10, 2020 5:48 pm

egolombek wrote:
Wed Jun 10, 2020 3:45 pm
I am hoping to learn how I can access data, without necessarily learning all the mechanics. If possible...
Possible, but not optimal. And likely not desirable, given the potential risks with network applications.

You're entering programming at a uniquely challenging moment in history. Everything is connected by an open Internet, which enables app designers to deliver empowering solutions, but it also enables the bad guys to do great damage.

In the olden days users were content to read and write files only on their own machine. Programming was simple then. Without connectivity, the only way someone could muck with your data was if they burgled your home and physically stole the computer. If that happened you may have bigger concerns than just the data an app uses.

These days data and the apps that use it are often in different locations, with potentially several billion other people between you and your data. And some of those are aren't nice. And a few of those are also smart.

The good news is that we're not alone. Every industry uses the internet, so there are few wheels we need to reinvent. But we do need to learn them, at least at ab basic level, so we can use them well.

HTTP is a beautiful protocol. You can make your own protocol, but most of the time what you need is just a way to send a request and get a reply, and HTTP does that wonderfully. So well, in fact, you'll probably never need to write a custom protocol through your entire career. Most of what you'll do will be done through HTTP.

HTTP is simple, yet extensible. So learning only a few things let's you use it in infinite ways.

Skim the Wikipedia page on it, and come back with questions. Some day I'll get around to writing a book on networking in LiveCode, but for now there's enough out there to do well.

When learning something new, details can be overwhelming. Don't worry. With HTTP a very small number of principles make everything else fall into place.

Once you have a basic understanding of using HTTP, some services like APIs will require a means of authenticating the request, making sure the requester is who they say they are.

There are a great many authentication methods, and some people even roll their own (not recommended nor usually even necessary). You can ignore most of them.

Most services you'll access will only use either Basic Auth or OAuth2. Basic is super-simple, and while OAuth2 is complex under the hood you don't need to worry about most of those details when using LiveCode because the included OAuth2 library takes care of the heavy lifting.

The most fun way to learn things is with a concrete example for something you want to accomplish. And you have one, so we don't need to explore everything all at once, just the parts you need to get this task done. Anything else can come later as your needs grow.

So let's start at the beginning, with the most important thing to learn: dealing with vendor documentation. :)

The code examples you provided will be helpful later. For now we need to know how to gain access to the system at all. And that will depend on which authentication method they use.

So your first task is to review their docs to find out what's needed to authenticate.

Good docs will make that easy to find, usually in the first section or two because everything else will depend on it.

So dive in and let us know what you find. If their writing isn't clear (many great engineers are poor writers, so that happens often) let us know how far you got and include the URL to the page where you found what you're asking about, and we'll pick it up from there.

The art of programming is really the art of learning. Those who do well are those who enjoy learning new things, a necessity in a field defined by perpetual change.

But you don't need to learn everything at once (and that's not even how learning works anyway). We'll help guide you step by step, as others helped us before, and over time you'll find yourself helping others as well.

Looking forward to what you learn about their auth methods...
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Learning to use APIs

Post by SparkOut » Wed Jun 10, 2020 7:11 pm

FourthWorld wrote:
Wed Jun 10, 2020 5:48 pm
Some day I'll get around to writing a book on networking in LiveCode
I would like to pre-order that. And apologise for distracting from the pertinent and informative tracts above.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Learning to use APIs

Post by FourthWorld » Wed Jun 10, 2020 8:12 pm

SparkOut wrote:
Wed Jun 10, 2020 7:11 pm
FourthWorld wrote:
Wed Jun 10, 2020 5:48 pm
Some day I'll get around to writing a book on networking in LiveCode
I would like to pre-order that. And apologise for distracting from the pertinent and informative tracts above.
No worries. Thanks for the encouragement on the book. Very motivating.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

egolombek
Posts: 74
Joined: Thu Jan 30, 2020 2:11 pm

Re: Learning to use APIs

Post by egolombek » Wed Jun 10, 2020 8:30 pm

I appreciate your thoughtful response with encouragement to learn http. I took your advice and learned a bit about http (and then tcp/ip) -- videos were more helpful than Wikipedia, but it led me to realize I need to build a custom http tsNetCustomSync request. To my shock and surprise, it actually worked!

Here's what I did. I have XXXXXed the key, for privacy:
on mouseUp
put "https://morning-star.p.rapidapi.com/sto ... 0P0000OQN8" into pURL
put "x-rapidapi-host: morning-star.p.rapidapi.com"&return&"x-rapidapi-key:XXXXX"&return&"useQueryString: true" into tHeaders
put tsNetCustomSync(pURL, "GET", tHeaders, rOutHeaders, rResult, rBytes) into cd fld tOut
answer rResult
end mouseUp
Still much to learn, but nice to get it to connect! :D

Post Reply