🚀 This blog is just a cheetsheet for myself. You can't visit links because the corresponding repositories are private.
Simple API
API is the shortname of Application Program Interface. An API can be viewed as a program which takes some data, processes it and gives back some data.
We use JSON format to store and transmit data over the Internet. The server side cannot return the Python or Java data directly since JavaScript doesn't understand it. 😓 Imagining how JS deals with Python's dictionary or Java's HashTable? Instead, since JSON data is text, a long string, so JS can read it and deals with it.
- tip: Alway double quote in JSON instead of single quotes.
❤️ LINK: demo of a simple api using flask
❤️ LINK: What is an API endpoint?
RESTful API
REST Principles
"REST" is a way of thinking about how a web server responds to your requests. Resource is the core component when designing RESTful API. A resouces can be anything, like a document, an image, a service, or a collection of other resources. So it is the resources that transfer between cilent and server. JSON, XML or JPG image are just the format of resources.
(When designing RESTful API, theoretically we only use noun and don't use verb)
Another key feature of REST is to be stateless. This means one request cannot depend on other requests. The server doesn't remember any state.
Let's see an example in the real world.
1 | - A user logs in a web application such as Twitter. |
Virtual Environment
We develop our api using Python virtual environment.
flask-restful
flask-restful
is an extension of Flask that provides additional support for building RESTful apps.
flask_jwt
- Flask-JWT: (JWT: JSON Web Token) an extension of Flask that implementing token-based authentication
Using JWT authentication is good for scaling. Since the server doesn't need to store any information of the user, so it is stateless.
When we pass the authentication, the server will generate a JWT token and return to the client. Each time, the cilent sends request containing the JSON token, and the server will use this JSON token to identity the user.
1 | # security.py |
1 | # app.py (snippet) |
Advanced JWT configurations:
1 | """Advanced JWT Configuration""" |
Advanced Postman Config
❤️ Link: Advanced Postman —— environment & tests
Resources vs. Models
For any code related to getting data from API users and responding with data, that will go in a resource. The resource will then call the model for any interactions with our system (e.g. databases, saving things, creating objects,...)
Resources and models are used to encapsulate the logic of user-facing and system-facing respectively, which makes it simpler for you — the developer — in the long term.
Persistent Storage: Database
SQLite DB
❤️ Link: interaction with sqlite demo
PostgreSQL with SQLAlchemy
We can use SQLAlchemy to easily replace database from SQLite to PostgreSQL or other databases. SQLAlchemy is a library that provides a nice “Pythonic” way of interacting with databases. So rather than dealing with the differences between specific dialects of traditional SQL such as MySQL or PostgreSQL or Oracle, you can leverage the Pythonic framework of SQLAlchemy to streamline your workflow and more efficiently query your data.
Most of the times, this library is used as an Object Relational Mapper (ORM) tool that translates Python classes to tables on relational databases and automatically converts function calls to SQL statements. Also, it can map objects to rows in a database.
psycopg2
SQLAlchemy is a ORM. psycopg2 is a PostgreSQL database adapter for the Python programming language. These are completely different things: SQLAlchemy generates SQL statements and psycopg2 sends SQL statements to the database. SQLAlchemy depends on psycopg2 or other database drivers to communicate with the database!
Security
Security in REST APIs is extremely important, because often applications that use our REST APIs will be sending us all sorts of data about users: passwords, e-mail addresses, names, postal addresses, security questions, bank details, and more.
In order to prevent people from intercepting the data on the internet and being able to read it, we must use Secure Sockets Layer. This sits on top of HTTP and encrypts all communication between a server and a client.
❤️ Link: Security in REST APIs