You must Bring Your Own Device and have access to an IBM i.
Goals
Understand APIs & Documentation
Host an API locally with Python
Consume an API with Python
Learn the basics of Authentication
RESTful API Basics
REST (Representational State Transfer) is a way to design networked applications. It uses HTTP requests to perform operations like Create, Read, Update, and Delete (CRUD) on resources identified by URLs.
Key principles of REST:
Stateless: Each request from client to server must contain all the information needed to understand and process the request.
Client-Server: The client and server are independent and can be developed separately.
Cacheable: Responses must define themselves as cacheable or not to prevent clients from reusing stale or inappropriate data.
Uniform Interface: Resources are identified in the request, and the operations are defined by HTTP methods (GET, POST, PUT, DELETE, etc.).
In summary, RESTful APIs provide a standardized way for systems to communicate over HTTP using a set of well-defined operations and principles.
Methods
GET
The GET method retrieves data from the server.
PUT
The PUT method replaces all current representations of the target resource with the request payload.
PATCH
The PATCH method is used to apply partial modifications to a resource.
POST
The POST method is used update an existing resource or create a new resource.
DELETE
The DELETE method deletes the specified resource.
HEAD/OPTIONS/TRACE/CONNECT
Less commonly used methods for various purposes like checking the server status, options available, tracing the request, and establishing a tunnel.
Status Codes
200 OK
The request has succeeded.
201 Created
The request has been fulfilled and has resulted in one or more new resources being created.
400 Bad Request
The server cannot or will not process the request due to an apparent client close.
401 Unauthorized
Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided.
404 Not Found
The requested resource could not be found but may be available in the future.
500 Internal Server Error
A generic error message, given when an unexpected condition was encountered and no more specific message is suitable.
APIs Visualized
Swagger UI is a popular tool for visualizing APIs. It provides a user-friendly interface to interact with APIs and understand their endpoints and data structures.
If an API has an OpenAPI formatted yml or json file, you can use Swagger UI to visualize it.
Create a new folder for your project. This can be a folder anywhere on your computer.
Step #2
Open Visual Studio Code and select FILE and “Open Folder” to open your project folder.
Step #3
Open the terminal (Ctrl + ` in VS Code).
Step #4
Create a new virtual environment:
Step #5
Activate your virtual environment:
On Windows (powershell):
On macOS/Linux or Windows (bash):
On PASE (IBM i):
Setting up a Basic Flask Application
Step 1: Use the python package manager pip to install Flask
Step 2: Create a new file hello.py
We start by importing the Flask module and creating a new instance of the Flask class. We then define a route for the root URL / that returns the string “Hello, World!“.
Add the following route to host.py to update the manager for a specific department by its ID using a PATCH request:
Test this endpoint using Thunder Client!
Select PATCH and enter http://127.0.0.1:5000/departments/A00 with the following JSON data in the Request Body:
Then switch to GET to verify the changes.
Consuming an API
To consume an API, you can use the popular requests library in Python. Install it using:
GET /employees
First, create a new file consume.py and add the following code to retrieve the list of employees:
This script sends a GET request to the /employees endpoint and prints the list of employees if the request is successful.
Testing consume.py
PATCH /departments
Next, add code to consume.py to patch departments with data from the /departments endpoint:
This script sends a PATCH request to the /departments endpoint with the new location data and prints the ID of the updated departments if the request is successful.
Basics of Authentication
Common Types
Basic Authentication: This method involves sending the username and password encoded in Base64 in the Authorization header.
Bearer Token: This method uses a token that is generated by the server and sent in the Authorization header. The token is usually a JWT (JSON Web Token).
API Key: This method involves sending a unique key, usually in the query parameters or headers, to authenticate requests.
Testing Auth Tokens
To test Auth Tokens add the Authorization header to your requests.
If your client supports it, there might be a dedicated tab for Auth. Refer to the screenshot for using a bearer token in Thunder Client.
Sending a Bearer Token in your GET request to /employees
To send a bearer token in your GET request to /employees, you need to include the Authorization header with the token when calling requests.
Replace ‘your_token_here’ with your actual bearer token. This script sends a GET request to the /employees endpoint with the bearer token included in the Authorization header.
Generating a Auth Token with API Playground
Send a HTTP request to /employees or /departments with the Auth Token and watch it populate on the Receive Tab
Using the Receive Tab click the Token button to view the validity of the token
Congratulations! 🎉
You’ve successfully set up a basic Flask application, created API endpoints, consumed those APIs, and learned about authentication.