Intro.
An API, or Application Programming Interface, is like a menu in a restaurant. The menu provides a list of dishes you can order, along with a description of each dish. When you specify what menu items you want, the restaurant's kitchen does the work and provides you with some finished dishes. You don't know exactly how the restaurant prepares that food, and you don't really need to. Similarly, an API provides a way for different software applications to communicate with each other. It provides a list of functions that one software application can use, along with a description of what each function does. When a software application wants to use a function, it sends a request to the API, and the API carries out the function on behalf of the application.
What is a REST API
A REST API, or Representational State Transfer API, is a set of rules and protocols for building and interacting with web services. It's a way for different software applications to communicate with each other over the internet. REST APIs use HTTP methods (GET, POST, PUT, DELETE) to create a uniform interface for accessing data.
Here's a simple breakdown of how a REST API works:
1. **Resources**: In a REST API, data is considered as 'resources'. These resources are accessed using URLs. For example, if you have a website that sells books, you might have a resource for each book, and you could access each book using a URL like `http://mywebsite.com/books/1` [Source 7](https://www.guru99.com/restful-web-services.html).
2. **HTTP Methods**: The HTTP methods (GET, POST, PUT, DELETE) are used to perform operations on these resources. GET is used to retrieve data, POST is used to send data, PUT is used to update data, and DELETE is used to remove data [Source 2](https://www.ramotion.com/blog/rest-api/), [Source 7](https://www.guru99.com/restful-web-services.html).
3. **Stateless**: Each request from the client to the server must contain all the information needed to understand and process the request. The server should not store anything about the latest HTTP request the client made. Each request is independent of the others [Source 2](https://www.ramotion.com/blog/rest-api/), [Source 7](https://www.guru99.com/restful-web-services.html).
4. **Cacheable**: The responses from the server can be cached by the client. This means that the client can store the response from the server and use it for future requests, reducing the need for the client to make a new request each time [Source 2](https://www.ramotion.com/blog/rest-api/), [Source 7](https://www.guru99.com/restful-web-services.html).
5. **Uniform Interface**: The API should have a consistent and predictable interface. This makes it easier for developers to understand and use the API [Source 2](https://www.ramotion.com/blog/rest-api/), [Source 7](https://www.guru99.com/restful-web-services.html).
So a REST API is a way for different software applications to communicate with each other over the internet.
It uses HTTP methods to perform operations on resources, and it's designed to be stateless, cacheable, and have a uniform interface.
What are Microservices and monoliths ?
Microservices and monoliths are two architectural approaches for developing and organizing software applications. Let me explain both concepts:
Microservices:
Microservices is an architectural style where a complex application is broken down into smaller, independent services that communicate with each other through APIs (often over HTTP). Each service is responsible for a specific business capability or function.
Characteristics:
Decomposition: The application is divided into small, loosely coupled services that are easier to manage and develop independently.
Independence: Each microservice can use its own technology stack, programming language, and database, as long as it exposes well-defined APIs.
Complexity: Microservices can introduce complexity in terms of service discovery, communication, and data consistency.
Monolith:
A monolithic application is a single, self-contained unit where all components and functionality are tightly integrated. In a monolith, the entire application is typically built using a single technology stack and often shares a common database.
Characteristics:
Simplicity: Monoliths are typically simpler to develop, as they don't involve the complexities of distributed systems.
Consistency: Data consistency is easier to achieve, as all data is stored in a single database.
Deployment: Deploying a monolith is straightforward, as it involves deploying a single application.
Drawbacks: Monoliths can become large and unwieldy over time, making it challenging to scale and maintain as the application grows. They can also have a single point of failure.
Microservices are often chosen for large, complex applications that require scalability and flexibility. Monoliths are sometimes preferred for smaller applications with simpler requirements to reduce development complexity. There's also a hybrid approach where some parts of an application are microservices while others remain part of a monolith, which is known as a "monolithic microservices" architecture.
API in practice:
Python libraries used for making HTTP requests:
`urllib` and `requests
` are Python libraries used for making HTTP requests. They allow you to interact with websites and web services by sending and receiving data over the HTTP protocol. Here's an explanation of both:
1. **`urllib`**:
- **Description**: `urllib` is a Python module that provides several modules for working with URLs. It is part of Python's standard library, which means you don't need to install it separately.
- **Modules**:
- `urllib.request`
: This module is used for opening and reading URLs. You can use it to make HTTP requests and handle responses.
- `urllib.parse`
: This module provides functions to manipulate URLs, parse query strings, and more.
- `urllib.error`
: It defines exceptions that can be raised when working with URLs.
- **Basic Usage**:
```python
import urllib.request
url = 'https://www.example.com'
response = urllib.request.urlopen(url)
content = response.read()
print(content)
```
**Pros**:
- Part of the standard library, so no external installation is needed.
- Suitable for basic HTTP requests.
2. **`requests`**:
- **Description**: `requests` is a third-party Python library for making HTTP requests. It is widely used in Python for its simplicity and versatility.
- User-friendly API: It offers a straightforward and easy-to-use API for making HTTP requests.
- Sessions and Cookies: `requests` can easily manage sessions and cookies for more complex web interactions.
- File Uploads: It can handle file uploads in HTTP requests.
- Authentication: Supports various authentication methods, including Basic and OAuth.
- Custom Headers: You can set custom headers for requests.
- **Basic Usage**:
```python
import requests
url = 'https://www.example.com'
response = requests.get(url)
content = response.text
print(content)
```
- **Pros**:
- Elegant and intuitive API.
- Excellent support for handling various aspects of HTTP requests.
- Extensive documentation and a large user community.
In most cases, developers prefer to use the `requests` library over `urllib` due to its ease of use and comprehensive feature set. However, if you're working in a minimal Python environment where third-party libraries are not allowed, `urllib` can be a viable choice for basic HTTP requests.
PYTHON REQUESTS GET - PARAMETERS
In Python, you can use the requests
module to send a GET request with parameters. The parameters can be passed as a dictionary to the params
argument of the requests.get()
function techinima.com, pythonrequests.com.
Here's an example where we want to retrieve books of a certain reader from a hypothetical API:
In Python, you can use the requests
module to send a GET request with parameters. The parameters can be passed as a dictionary to the params
argument of the requests.get()
function techinima.com, pythonrequests.com.
Here's an example where we want to retrieve books of a certain reader from a hypothetical API:
import requests
url = 'https://api.example.com/books'
params = {'reader': 'John Doe'}
response = requests.get(url, params=params)
# Print the status code and the response text
print(response.status_code)
print(response.text)
In this example, we're sending a GET request to the 'https://api.example.com/books' URL. The params
argument in the requests.get()
function is used to pass the parameters as a dictionary. The server will receive this request and return a response containing the books of the reader 'John Doe'.
If the response is in JSON format, you can use the json()
method to convert the response to a Python dictionary:
import requests
url = 'https://api.example.com/books'
params = {'reader': 'John Doe'}
response = requests.get(url, params=params)
# Convert the response to a Python dictionary
data = response.json()
# Print the data
print(data)
In this example, response.json()
converts the JSON response to a Python dictionary diveintopython.org.
Remember to replace 'https://api.example.com/books' with the actual API endpoint you want to use, and replace 'John Doe' with the actual reader's name. Also, the key 'reader' in the params
dictionary should match the parameter name expected by the API.
Important Notes:
The
requests.get()
method returns aResponse
object, not a dictionary. So you can't use theget()
method directly on response. You need to usejson()
method first to convert the response to a Python dictionary.The
requests.get()
method needs the URL as its first argument. if we need to append a param like an id to the URL and then pass it torequests.get()
. This can be achieved using string formatting.In this example, we're using an f-string to append the employee_id
to the URL. The formatted URL is then passed torequests.get()
. The server will return the details of the user with the given ID, and these details are printed to the console datagy.io, geeksforgeeks.org.
How to export response data to csv file ?
In Python, you can use the built-in `csv` module or the `pandas` library to export data to a CSV file.
Here is an example of how you can export response data to a CSV file using the `csv` module:
```python
import csv
import requests
response = requests.get('https://jsonplaceholder.typicode.com/posts')
data = response.json()
# Open the file in write mode
with open('output.csv', 'w') as f:
# Create a CSV writer
writer = csv.writer(f)
# Write the header
writer.writerow(data[0].keys())
# Write the data
for row in data:
writer.writerow(row.values())
```
In this example, `requests.get('https://jsonplaceholder.typicode.com/posts')` fetches a list of posts from the JSONPlaceholder API. The `json()`
method is used to convert the response to a Python list of dictionaries. Each dictionary represents a post and its keys represent the column names see
The csv.QUOTE_ALL
parameter
The `csv.QUOTE_ALL` parameter in Python's `csv` module is used to specify when quotes should be generated by the `csv.writer` object. When you set `quoting=csv.QUOTE_ALL`, it means that quotes will be added around all fields, regardless of their content.
Here's an example:
```python
import csv
data = [['Name', 'Age', 'Occupation'],
['John Doe', 30, 'Engineer'],
['Jane Doe', 25, 'Doctor']]
with open('output.csv', 'w', newline='') as file:
writer = csv.writer(file, quoting=csv.QUOTE_ALL)
writer.writerows(data)
```
In this example, `csv.writer(file, quoting=csv.QUOTE_ALL)` creates a `csv.writer` object that will add quotes around all fields when writing to the CSV file. The resulting CSV file will look like this:
```
"Name","Age","Occupation"
"John Doe","30","Engineer"
"Jane Doe","25","Doctor"
```
As you can see, all fields are enclosed in quotes. This can be useful when your data contains special characters that might interfere with the CSV format, or when you want to ensure that the data is interpreted correctly when the CSV file is read: see
How to export data in the JSON format:
In Python, you can use the built-in json
module to export data in JSON format. The json.dump()
function can be used to write JSON data to a file.
Here's an example of how you can export response data to a JSON file:

requests
library to make an API request to https://api.example.com/data
. We then check if the request was successful (status code 200). If it was, we parse the response JSON data using response.json()
.Notes on List comprehension for revision:
List comprehensions can be a powerful tool for creating lists in a concise and readable way. If you're having trouble with list comprehensions, here are some tips to help you understand and use them effectively:
1. Basic Syntax: The basic syntax of a list comprehension is `[expression for item in iterable]`. The expression is evaluated for each item in the iterable, and the results are collected into a new list.
2. Filtering with Conditionals: You can add an optional conditional statement to filter the items in the iterable. The conditional statement is added after the expression and before the `for` keyword. For example:
`[expression for item in iterable if condition]`.
3. Nested List Comprehensions: You can also nest list comprehensions to create more complex lists. This allows you to iterate over multiple iterables or apply multiple conditions. For example: `
[expression for item1 in iterable1 for item2 in iterable2 if condition]`.
Here's an example to illustrate the use of list comprehensions:
```python
# Example: Squaring numbers from 1 to 10 using a list comprehension
squared_numbers = [num**2 for num in range(1, 11)]
print(squared_numbers)
```
In this example, the list comprehension `[num**2 for num in range(1, 11)]` generates a list of squared numbers from 1 to 10. The expression `num**2` is evaluated for each number in the range, and the results are collected into the `squared_numbers` list.
Resources":