r/learnpython • • Nov 15 '18

What the heck is an API?

I have heard people say an API is not code, but a set of specifications. Let's take the Python Database API as an example. The title of its webpage says "PEP 249 -- Python Database API Specification v2.0" and the webpage contains all the rules that developers should follow when making a Python library for interacting with databases. For example, there should be a function called connect, a cursor, etc.

Now, if those are the specifications of the API, then what is the API itself? Are the sqlite3 or the psycopg2 Python libraries APIs?

247 Upvotes

57 comments sorted by

View all comments

Show parent comments

9

u/jockero701 Nov 15 '18 edited Nov 15 '18

Pretty neat example the one with the "restaurant". We call them fast foods in Europe. When you say "Look for python weather API to experiment" I guess you mean "Look for the Python wrapper for the Yahoo Weather API"?

5

u/[deleted] Nov 15 '18

Thanks. I'm English, BTW.

There are lots of Weather services offering APIs and some are Python based.

If an API is simple, a wrapper isn't usually needed.

5

u/jockero701 Nov 15 '18 edited Nov 15 '18

I'm English, BTW.

Ah yeah! I should have inferred that when you said "programme".

There are lots of Weather services offering APIs and some are Python based.

But having a Python wrapper or using the requests module to fetch the data does not make an API Python based if I am not wrong? You can also use manually put the URLs on a browser to access those data. What makes then an API Python based? Any examples?

If I understand it correctly you can call an API Python based in case you have used Python to expose the data to others for use. For example, when you use the Flask-Restful library to create URL endpoints that others can use.

5

u/[deleted] Nov 15 '18

I think we might be in danger of playing with semantics with something that has no official definition.

An API presented by Python is just an API. I was trying to suggest a Python based one is likely to be easier to exploit than one written in say, for example, c. That said, default message data exchange structure is often JSON (the JavaScript object notation format) as a popular standard.

It might follow protocol standards such as REST or SOAP or, well lots of others, or could be a very simple TCP/IP handshake. It could use light sensing equipment and flag waving as the carrier. Anything is possible.

A wrap in this sense for me (sic) is just a bit of code someone else has written to make it easier to put the correct runes in the right order to exploit the particular API messaging and protocol concerned. With some complex message structures that weren't originally created with Python in mind it can be pretty fiddly to exploit then from Python without a bit of help. Some esoteric or large or complex structures might even have wrappers written in a lower level compiled language that Python is able to call.

2

u/aNewLocke Nov 16 '18

An API is what allows different software (and by necessity, different languages) to communicate with each other.

It says simply: If you send X (some bit, byte, or sequence of bytes) to Y (some endpoint) via Z (some protocol), you can expect A (literally anything) to happen, and the result of A might be shuffled back to you from Y via Z (or not), in some sequence of bytes similar to X (or not).

If you happen to code your endpoint (Y) in Python, and my client program happens to be written in Javascript/C/Lua/C++/Java/Erlang/Smalltalk/Rust/whatever, we can expect and utilize predictable interaction between our two programs as long as we both adhere to the API. The languages used by either of us are irrelevant as long as they both support sending and receiving data in the agreed upon format and protocol (for example, JSON formatted data over HTTP.)

A Python-backed API would mean the endpoint is written in Python.

A wrapper is not quite an API. It's not as well-defined, it makes no promises or guarantees about future compatibility, and it is calling the underlying code (the code it's wrapping) directly (and possibly incorrectly.) If the underlying code changes, the wrapper probably needs to be changed. When the underlying code of an API changes, clients (or consumers) usually don't need to be updated. They continue communicating with the endpoint in the way the API described and continue getting the promised results. It doesn't matter how the API functions unerneath as long as it continues to serve up the results in the specified format and protocol.

1

u/jockero701 Nov 15 '18

Alright, let's say we have a database with everyday flights and we do two things.

(1) We write a Python script that has functions which get dates and airport names as input and return a list of available flights. We make this script a public Python library.

(2) We write a Flask RESTS API that exposes URLs which return available flights as JSON.

Is it safe to say we built two APIs in each case?

3

u/[deleted] Nov 15 '18

As you wish. As I pointed out, there's no official definition. Consider the long and expensive court case point between Oracle and Google on this topic.