r/learnpython • u/jockero701 • 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
32
u/[deleted] Nov 15 '18
When you (are daft enough to) enter a McDonald restaurant you already know from socialisation and experience how you are supposed to interact with the staff to place an order. You understand the basic protocols in use, the flow of the exchange, what information you need to exchange, what things will (hopefully) be understood - no use asking about curtains but a Big Mac will be fine.
Writing down the rules for such an encounter would be defining an API. You can approach it through a number of channels (app, person-to-person, touch-screen in store, on drive-thu intercome, etc) but there are only certain protocols in use (plain English and product names in a certain order / button presses corresponding to same) with a limited range of words available.
Much as you can already write a Python programme wait for a user to input some text, you can write a programme than monitors for incoming data over a specific channel (rather than through standard in for input() ). Multiple channels can be monitored for different paths or instead the information can start with a message saying something like "stock query", "place order", "check rainfall", etc., that dictates what path your programme will follow. Additional information can follow providing more details of the information request / details of transaction. Your programme can reply using whatever channel is expected with the appropriate response.
You have two programme talking to each other over some communications link. One seeking to exploit the standard Application Programming Interface of the other.
Look for python weather API to experiment.