[TUT] Basics: (Web) APIs
#1
~~~~ Hey, you! ~~~~

Hello and welcome to my thread. This thread is going to be an addition to my basic-thread series. As you may or may not know I 
have created a series of threads for the purposes of referring back to them in case I mention them elsewhere. Also, 
for the benefit and convenience of less-technical members who maybe want to take a step into development,
general technological knowledge, or whatever else. And yes, for some reason I mention something similar
in every other one of these basic threads. :3 

I intend to cover APIs in this thread, with emphasis on web-based APIs...



Please note: The section titles will each assume that you have read the one prior to it, which is why the context might seem indefinite if you've jumped around.







What is an API?

API stands for Application Programming Interface, and conveniently, that's exactly what it is. :D
The real question is, what does an application programming interface (an API) actually do?
and I want to answer that technically first, and then break it down a little bit.

I like the following definition: 


'A set of functions and procedures that allow the creation of applications

 which access the features or data of an operating system, application, or other service.'



Because it's a broad definition, it covers something for all types of APIs: source code, legacy, web APIs etc.





An Alternative Explanation?

OK. So we know that an API is an Application Programming Interface...

and we know that a GUI is a Graphical User Interface...right?



Now, bear with me because I feel like comparing the two will drive this point home...



Think of a GUI... a Graphical User Interface, like this one:

(forget about what it does, I fetched it off google...it looks like some UI design for developers, not important)





[Image: 110.jpg]

This is a GUI.

It's graphical because we can visually see controls, elements, things we can interact with via our input devices...
and it's a user interface because we (the users) are the ones interacting with it. Right?
We're using our keyboards to type, our mouses (or mice, whichever you prefer) to click, and so on.

Now...

An API (Application Programming Interface) isn't for us users, it's an interface for programming applications.
In other words, it's an interface designed for applications. 

The same way we'd use our input devices to communicate with user interfaces, applications use 
whatever means to interact and communicate with application (programming) interfaces, 
whether that's requesting/establishing server<->server connection or 
simply fetching code and functionality from a local destination,
retrieving information from the OS via provided resources....
the list goes on.

But essentially, a User wouldn't casually use a Application Programming Interface, because that's not
for us users; for us are GUIs, Graphical User Interfaces...
Remember the difference, I feel it may contribute 
to your understanding of APIs,
somewhat






Why do we need APIs?

I find that I firmly understand the necessity of things when I'm in a situation where I don't have access to its remedial solutions...
for example, I get a better understanding of why we humans need food to survive, when I'm starving & food
simply isn't an option... if that makes sense. Silly example but just to show that I'm forced to understand it's benefits, 
because I practically require it. Whereas simply talking about it & not feeling it's requirement is a little weak, for me.

Because I'll keep thinking of alternatives, instead of that exact thing. >_>

...I believe the reason for that is the answer is simply too obvious to think of, and that ironically confuses me;

but I digress.

I mention this because I intend to verbally illustrate some hypothetical conundrums, to which
APIs could introduce remedy, but we won't initially consider APIs. This may help us 
see why they're important, and simply beneficial in the development world.


Scenario #1

Say you wanted to create a program of some sort that has a lot of dependencies, and among them is string manipulation. 
String manipulation could be things like trimming, splitting, flipping, sub-stringing, replacing strings, and so on. 

Without using some sort of library or a collection of these functionalities, you may end up
having to manually cultivate a tedious list of functionalities that enable the mentioned procedures.

However...

If you do include something that makes this easier, then you've used an API. 
Your application code is interacting with whatever resource you provided,
as means of easing an otherwise difficult process for you as the 
developer in this scenario.

The interface that the imported library provides for the code is simply the list of properties and functions that you will use instead of 
whatever else you would have made.

You'll pick and choose whatto use based on what you need. Just like how we'd click and type
into whatever we need in a graphical user interface.


Scenario #2

If you were to make an app that interacts with this platform and retrieves user information...

you'd need some way to retrieve that information, as it's something that's dynamic 

and can change, from time to time...



Typically, there are 2 common solutions to a problem like this. This first being to scrape the website, 

and filter out the required information. This may be convenient in some situations, and in others,

that might simply not be the case.



The second solution is the use of a web API. The way these work is:

you request a URL with certain parameters that define the type of information that you want to retrieve from

that page/server, and sometimes a key that identifies and authenticates your access to that API..


The above is an example of a URL that one might request from, in a scenario like this...
The server usually returns JSON or XML data, which the user can easily format 
and serialize into objects.


Generally, this puts less pressure and hassle on the core application source code, as it's now utilizing external resources. 
Sometimes we don't have a choice, though. For example, facebook's API.

If we don't use an API to retrieve user information, it's not like we can hard code a user's data, when we
have no darn idea what that data is, make sense?

Therefore, an API would be mandatory in this case.


-------------------------------------------------------------------------------------------------------------------


End of Theory






A Practical Demonstration

Imagine I'm working on an app for a forum, localhost. And I want to get some information on all users. 
I could do this by requesting the following URL


 (yes, imagine the API is being 
hosted localhost, too,
 for some reason,
 shurup :p)





http://localhost:3000/api/users:



[Image: ca81deb57f3f43dba1f1762de4d6e434.png]



After this it's all about storing the JSON and interpreting that data and then proceeding with whatever I want to do with it.




Pardon me for any inaccuracies.  Feel free to share your relevant thoughts, concerns and opinions in the comment section below,

 I'll try to answer you if and when I feel it's appropriate and necessary. 

If I don't, perhaps another member will.



 Thank you for reading my thread, I hope you benefited and or enjoyed. 


Have a nice day. Hat Tip
#2
Oooh Baredee, once again I have felt very satisfied after reading and understanding your thread, great effort and overall a fantastic tutorial / explanation,
Cheers!
Reply
#3
(07-27-2016, 09:26 AM)StrandedBanana Wrote: Oooh Baredee, once again I have felt very satisfied after reading and understanding your thread, great effort and overall a fantastic tutorial / explanation,
Cheers!

Glad you like it. :)
Reply
#4
Im familar with this stuff. But for people who doesn't know it is a GREAT explaination.
Nice job again bud. Also I feel like making a follow up how to write them easly, Wink.
Reply
#5
(07-31-2016, 03:04 AM)Coddr Wrote: Im familar with this stuff. But for people who doesn't know it is a GREAT explaination.
Nice job again bud. Also I feel like making a follow up how to write them easly, Wink.

Thanks man!

Is that Node ur talking about? ;D
Reply
#6
(07-31-2016, 03:23 AM)Baredee Wrote:
(07-31-2016, 03:04 AM)Coddr Wrote: Im familar with this stuff. But for people who doesn't know it is a GREAT explaination.
Nice job again bud. Also I feel like making a follow up how to write them easly, Wink.

Thanks man!

Is that Node ur talking about? ;D

I did talk about PHP but Node will be also a options since Express is so easy to make APIs with.
And node is easier to make APIs than PHP since PHP you need to use GET that is really insecure and unprof or you need to use composer.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)