Calling and handling APIS

Calling and handling APIS
0

#1

Hi all,

I have familiarized myself with calling and using APIs (the past day or so) I have yet to come across an in depth tutorial, or a set of concrete problem solving methods when facing challenges. It seems that every API has their own unique methods of calling and using it. But I was wondering if someone can give a nice overview of some general rules when it comes to using them. Why sometimes it works and sometimes you get different errors (cross-origin, etc…) I think that would be very helpful to myself and the community.

Cheers.


#2

I second this request API’s really confusing.


#3

I am digging my way through APIs understanding and I am progressing, but surely not as fast as if there would be straightforward instructions somewhere.
It would be really useful is someone helped out here. :slight_smile:


#4

There aren’t really any general rules - there’s no established format or structure for API’s - each API is going to have its own quirks and interface rules - each API will take time to learn on its own. Some are easier than others - some require authentication - some don’t - some limit the number of times you can query them some don’t - most mature API’s have good documentation and if they have a large user base there’s usually a large ‘blog’ base of people writing about using them. Sometimes you just have to slog through it until it does what you wnat it to.


#5

I totally understand your confusion. There are a lot of different concepts that get thrown together under this topic, and I haven’t seen it explained well. Ever. It isn’t so bad once you get your head around it, but that’s like saying “It’s easy to play the piano once you learn how to press some keys”. Let’s try to clear up some confusion:

By “API”, I generally assume you mean the API that we use to access other programs over HTTP. The actual definition is much broader, and API (Application Program[ming] Interface) refers to the code you use to run any other pre-written code. So, the browser has an API that we use constantly with functions like window.onload, setTimeout, and document.getElementById. jQuery has an API, and even JavaScript has an API for things like array manipulation (forEach(), map()) and date objects (new Date().getSeconds). My point here is that you have been using APIs since day 1, and web APIs aren’t much different. Most of the time, we’re just getting data!

Here are some videos that will (hopefully) help clear up some concepts you need to be successful.

What is AJAX?
REST API concepts and examples
CORS: Cross-domain requests with JavaScript
jQuery Ajax Tutorial #1 - Using AJAX & API’s
HTML5 Security Part 1/3 - Same Origin Policy Basics - This will explain a lot, but it’s dense and the presenter speaks very quickly.


Poorly explained AngularJS and APIs in FCC. Can you recommend a tutorial?
#6

It pays to follow FCC medium:


#7

Thanks all…

can anyone tell me how to get over the cross origin error when calling APIs?
preferably a browser independent solution??

That seems to be the biggest problem


#8

@Mozar10 pretty sure you can get around that by either using jsonp or somehow getting the content to the same domain as the code causing the error.

As far as APIs go… I’ll make it super simple:

Hi, my name’s Todd. I’m the back-end developer at Google Maps. Your name is @Mozar10, you want to find out the best route between two addresses. Your JavaScript/jQuery/AJAX/Whatever sends a request to api.ToddsGoogleMapsRules.com/directions (This is called the API endpoint and the URL can be different for every site) with the two addresses. This is where my back-end system (written in PHP, Python, Ruby, JavaScript, C#, or even Java) takes over, runs the code to actually figure out the best route, then shoots that route back to your JavaScript/jQuery/AJAX/Whatever code, usually in the JSON format.

When your request hits my endpoint, what is really going on is whatever you send to me is hitting one of my server’s functions and then that function, just like your JavaScript code, is acting upon the parameters that you give it. This is why in URLs these days, you barely ever see .html. This is because most websites you visit, your browser’s requests are hitting server-side functions/methods that are dynamically shooting your browser a different page depending on some server-side processing. I.E. if you’re not logged in, my server aint gonna let you see the pages for people who are and etc…

The concept of APIs isn’t really that complicated. You hit up a server with a request, and it replies to your app, giving you the data/information that your app needs to function properly. You can even make an API yourself.

For example, I recently made a simple API which allows me to add bars and beers to my website’s database. What’s cool about this, is now I can do so from any device. I can easily make an Android or iOS app that hits up the API rather than only being able to use a web browser and web form.

The other thing that’s badass, is that I could program my entire website using APIs so that way, instead of having to send you a bunch of HTML markup, my server can simply send you raw json data and the client can handle the markup portion which greatly saves bandwidth and makes the user feel like the website is a lot faster.

This is the most practical way to describe an API as it is used and referred to in the curriculum of this website. As mentioned above though, in a general sense, they can technically be any code library or anyone else’s code that you re-use. For example, I can write a C# program that will use either Microsoft’s Windows API or Apple’s Mac OS API and do things on your computer such as check the system processor usage, available memory, delete files (can you say… viruses…) etc…


#10

@TheOnlyRealTodd, how do you use C#.Net on mac, I’ve always heard it worked only on windows?


#11

.htaccess rules from the server control cors access by the browser. As for non-browser methods, that is a back-end question. You would need to write code on your server in php for so-example, or in nodejs for github example, then write your own API for connecting a browser with the newly acquired data which is now stored on your server.

Another possibility would be to write a bash script on your server that is run by a cron job that acquires the remote data on a schedule and stores it on your server for future use by the API that you would write to access the data on your server.

Another possibility would be to negotiate with the server administrator that controls the data you want to share and ask them to allow cors from your website :wink:

It should go without saying, clearly you do not want to attempt illegal :imp: access to private servers. Further it is your responsibility to scan whatever you download and not run un-trusted code on your server.


#12

@IsaacAbrahamson It works on Mac! That’s what’s driving me nuts. MS has gone through tons of effort specifically to make C# a cross-platform tool. They’ve open-sourced .NET, made .NET Core available to Linux and Mac, and they even created Visual Studio Code, yet for some reason, these rumors go around still. They also bought and developed Xamarin; this means that you can also write Android AND iOS AND Mac applications all with C#! There is even a C# library/compilter that can write JavaScript. Here are some resources to help you out:

Your first ASP.NET CORE Application on a Mac

Write C# on a Mac like a Champ!

ASP.NET vNext Applications on a Mac - Note that vNext is/was the old name for what is now ASP.NET Core .

How to use C# On your Mac

For Cross-Origin, @NorthDecoder had some great ideas. It’s a real PITA and obviously, you have to either get the content onto your server, get special access, or you could also set up a proxy. You definitely need access to a server back-end to solve this problem though if you aren’t going to use jsonp. Check out these resources for further help. Try the SitePoint one first, as it seems more clear:


#13

So C# language works on Mac, but you still can’t run WinForms on a mac, I don’t think or am I wrong?


#14

Well you could but of course for that you’d need to be running paralells or Bootcamp because WinForms is literally the Windows Operating system… So if you could run that without a virtual machine, you’d be using a Windows machine!

I’ve actually heard of a lot of people who do Windows development on Mac just fine though - As long as you have decent RAM, I’ve seen a number of folks actually write full-blown Windows Apps using Visual Studio in a VM. If you’re into that kind of thing; I chose to save a few bucks and just get a Windows computer because I figured I’d be mostly doing .NET dev.


#15

Well, I have a windows 10 laptop with VS 2016 Community. The first language I learned was C# WinForms, and I love GUI applications. I just wish there was a way for other OS to build a GUI in c#


#16

@IsaacAbrahamson I could be wrong, go check this out: http://www.mono-project.com/docs/about-mono/supported-platforms/osx/

I’ll admit I don’t have time to read about it now but you may be able to do that using that software I just linked to.


#17

I dont have much experience with APIs but just want to throw in my two cents on an issue I ran into. This might be obvious, but make sure the URL that is used to call the API includes the http:// at the beginning. I dont know if this matters for all APIs, but it was needed for the Open Weather API that FCC suggests to use, and I wasted a lot of time figuring that out.


#19

Here’s a blog post on how to get around CORS problems. https://jvaneyck.wordpress.com/2014/01/07/cross-domain-requests-in-javascript/