A Flask application with the ability to log in and register a user, create and receive articles written by one or another user.
To get started with the application you need to complete following steps:
- Install dependencies:
$ pip install -r requirements.txt- Create
.envfile in root directory of the project and add thereSECRET_KEYandDB_URIvariables:
SECRET_KEY=your-secret-key
DB_URI=your-database-uri
- Create tables in your database:
CREATE TABLE USERS(
id SERIAL PRIMARY KEY,
login VARCHAR,
first_name VARCHAR,
last_name VARCHAR,
password VARCHAR
);
CREATE TABLE ARTICLES(
id SERIAL PRIMARY KEY,
author_id INT REFERENCES USERS(id),
created_at BIGINT,
text VARCHAR
)- Run application:
$ python3 src/app.pyThe application consists of two modules:
- Auth - authorization module, which responsible for user registration and token generation.
- Article - article module, which responsible for creating new articles for user and retreiving them.
So in total we have following routes:
-
[POST] /auth/registerBody
{ login: String, first_name: String, last_name: String, password: String }
Response body
{ id: String, login: String, first_name: String, last_name: String }
-
[POST] /auth/loginRequest body
{ login: String, password: String }
Response body
{ access: String }
-
[GET] /articles/Query parameters
[OPTIONAL] author_id: String- parameters used to retrieve articles written by specific author.Response body
[ { id: Integer, author_id: Integer, created_at: Integer, text: String } ]
-
[POST] /articles/createQuery parameters
[REQUIRED] token: String- token that is used to create article for specific user.Response body
{ id: Integer, author_id: Integer, created_at: Integer, text: String }
Here is the example of usage:
$ curl -XGET 'http://localhost:5000/articles/'
[
{
"author_id": 1,
"created_at": 1634918180,
"id": 1,
"text": "Lorem ipsum"
},
{
"author_id": 1,
"created_at": 1634918194,
"id": 2,
"text": "Lorem ipsum dolor sit amet"
},
{
"author_id": 1,
"created_at": 1634918209,
"id": 3,
"text": "Lorem ipsum dolor sit amet"
},
{
"author_id": 1,
"created_at": 1634918517,
"id": 4,
"text": "Lorem ipsum dolor sit amet"
}
]