Working with APIs using Python
I love automation and believe automating repetitive tasks makes the world a better place as it would free up humans to work on more interesting challenges! APIs are one of the most useful tools in setting up automation so I thought I'd take some time to write about them and share my experience using them.
Let's start by defining the word 'Program'. 'Program' can be used as a noun or a verb, in the noun form, it means a set of instructions provided to a computer to perform a certain task while in the verb form it means to provide a human/computer a set of instructions to perform a task automatically.
Figure 1: API/Program Illustration |
API stands for Application Programming Interface and it is a way for humans and programs to interact with programs. Here's a simple illustration of how an API works:
Figure 2: API Analogy |
- Open APIs -> These are also known as public APIs or external APIs. These are APIs that are available for everyone to use, they might be restricted by security measures.
- Partner APIs -> These are APIs that are only available based on business agreements with stronger security measures restricting access.
- Internal APIs -> These are APIs that are used within a business for development/business-related functions and are not open to public users.
- Composite APIs -> These combine multiply APIs to bundle calls or requests and receive one unified response from different servers.
- SOAP APIs -> Simple Object Access Protocol. Client and server exchange messages using XML.
- RPC APIs -> Remote Procedure Calls. Can use XML or JSON.
- Websocket APIs -> Modern API development that uses JSON objects to pass data.
- REST APIs -> Representational State Transfer. Most popular & flexible APIs. Use JSON as well.
- Client-Server architecture: The Client and the Server have to be independent allowing improvements to be made more granularly to either the client or the server-side.
- Statelessness: There must be no session information stored on the server side and each client request must be independent of the other.
- Cacheability: There needs to be clear demarcation from the server-side whether the data being transmitted can be cached and if so for how long. Useful for internet-scale applications.
- Layered system: This allows the client to access the server in the same way irrespective of whether there is a load balancer or any other system between the client and the server serving the end-point.
- Code-on-demand: The ability to send executable code from the server to the client when requested, this allows the server to add functionality to the client that it normally doesn't have for some specified period of time.
- Uniform interface: This has the following 4 parts.
- Resource identification in requests.
- Resource manipulation through representations.
- Self-descriptive messages.
- Hypermedia as the engine of application state.
- GET
- POST
- PUT
- DELETE
Figure 4: HTTP status codes |
'$ python -m pip install requests' or just 'pip install requests'
>>> import requests>>> requests.get("https://randomuser.me/api/")<Response [200]>
>>> r = requests.get ("https://api.github.com/events")>>> r.text
>>> payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
>>> r = requests.get('https://httpbin.org/get', params=payload)
>>> print(r.url)
https://httpbin.org/get?key1=value1&key2=value2&key2=value3
[5] Requests module docs -> https://requests.readthedocs.io/en/latest/
Comments
Post a Comment