FastAPI Social Network is a simple social networking CRUD application built using FastAPI framework. It provides basic functionality to register and authenticate users, who can create, edit, delete, and view posts. Users can also like or dislike posts made by other users, but not their own posts.
Registration/Authorization: Users can sign up and login, using JWT tokens. There are also registration credentials requirements implemented, which disallow duplicate usernames and emails and restrict creation of too simple passwords and logins.
NOTE! As additional features there are also email validation and user data enrichment in place. These features use external APIs (hunter.io and clearbit.com) and require API keys to be set up as project environment variables. Without them, the project will not work as expected.
Post Management: Users can create, edit, delete, and view posts. Posts are stored in the PostgreSQL database, utilizing Docker volumes, ensuring data persistence.
Post Likes/Dislikes: Users can like or dislike posts made by other users, but not their own posts. Likes are stored in a Redis cache for faster access.
Before running the application, make sure you have the following dependencies installed on your system:
- Docker: Install Docker
- Docker Compose: Install Docker Compose
- Clone the repository to your local machine and make it your current working directory:
git clone https://github.com/RogerRabbit32/Python_FastAPI---TestTask_SocialNetwork.git
cd SocialNetwork
-
Inside the directory, create a '.env' file (this particular name is referenced in the docker-compose environment variable), where you have to specify two variables, storing your API keys:
HUNTER_API_KEY=#your secret key, no spaces around equal sign, no quotes, here
These keys will grant the application access to hunter.io and clearbit.com services. Without them, user registration will not be available and the project will not work properly.CLEARBIT_API_KEY=#your secret key, no spaces around equal sign, no quotes, here
-
Build the Docker image. Run the command:
docker-compose build
- Start the containers. Run the command:
docker-compose up
If the installation is successful, your app should be available at http://localhost:8000
All project API routes are available for tryouts via Swagger documentation http://localhost:8000/docs
Register user
Create a new user in the database with the specified login and password.
Route: POST /accounts/signup
NOTE! A valid email has to be provided at this stage, since all emails are verified through emailhunter.co If additional data is available on the user through clearbit.com email enrichment, the user's full name, if not provided by the user himself, will be automatically added to registration credentials.
Login user
Create a new JWT access token for the user. (Authentication also available via form data in Swagger docs)
Route: POST /accounts/login
Create Post
Create a new post with the specified title and text.
Route: POST /posts
Get All Posts
Fetch all posts with optional pagination support (limit and offset).
Route: GET /posts
Get User's Own Posts
Fetch all posts created by the currently authenticated user.
Route: GET /posts/user
Get Single Post
Fetch a single post by its ID.
Route: GET /posts/{post_id}
Update User's Own Post
Update a post created by the currently authenticated user.
Route: PUT /posts/{post_id}
Delete a Post
Delete a post created by the currently authenticated user.
Route: DELETE /posts/{post_id}
Like/Dislike a Post
Like or dislike a post made by other users.
Route: POST /posts/{post_id}/like
Unlike/Remove Like from a Post
Remove a previously liked or disliked post.
Route: DELETE /posts/{post_id}/like
Get Post Likes
Fetch all likes (and dislikes) for a specific post.
Route: GET /accounts/{post_id}/likes
When the containers are up, you can run the tests for the application by typing the following command in a new terminal:
docker exec <container name or id> pytest
This will execute the pytest autotests suite, stored in /tests directory
- FastAPI: Python Web framework for building APIs
- pytest: Python framework for tests management
- PostgreSQL: Database management system for storing application data
- Redis: In-memory data store for handling likes/dislikes functionality
- Docker + Docker Compose: Containerization for easy deployment