/FastAPI-social-API

Fast API Social Media Application API

Primary LanguagePythonMIT LicenseMIT

FastAPI Social API

This is restful CRUD social API built with FastAPI framework

Prerequisites

Python

Installation

Clone the repository

git clone git@github.com:DanNduati/FastAPI-social-API.git

Create a python virtual environment activate it

python3 -m venv env
source venv/bin/activate

Create a .env file similar to .env.example.

Install dependencies

pip install -r requirements.txt

Run the server with:

uvicorn app.main:app --reload

API Endpoints Overview

1. Auth Endpoint

OAuth2 authentication implementation, with password hashing and JWT tokens.

POST /login

Sample response

{
  "access_token": "string",
  "token_type": "string"
}

2. Users Endpoints

create/register a user

POST /users/

Sample response

{
  "id": 1,
  "email": "user@example.com",
  "created_at": "2021-12-24T10:53:04.455Z"
}

Get user by id

GET /users/{user_id}

Sample response

{
  "id": 1,
  "email": "user@example.com",
  "created_at": "2021-12-24T10:53:04.455Z"
}

3. Posts Endpoint

Get all posts

GET /posts/

Sample response

[
  {
    "Post": {
      "title": "string",
      "content": "string",
      "published": true,
      "id": 0,
      "created_at": "2021-12-24T10:56:01.321Z",
      "user_id": 0,
      "owner": {
        "id": 0,
        "email": "user@example.com"
      }
    },
    "votes": 0
  }
]

Create a post

POST /login

Sample request body

{
  "title": "string",
  "content": "string",
  "published": true
}

Sample response

{
  "title": "string",
  "content": "string",
  "published": true,
  "id": 0,
  "created_at": "2021-12-24T10:56:51.205Z",
  "user_id": 0,
  "owner": {
    "id": 0,
    "email": "user@example.com"
  }
}

Get latest post

GET /posts/latest

Sample response

{
  "Post": {
    "title": "string",
    "content": "string",
    "published": true,
    "id": 0,
    "created_at": "2021-12-24T11:02:11.832Z",
    "user_id": 0,
    "owner": {
      "id": 0,
      "email": "user@example.com"
    }
  },
  "votes": 0
}

Get a post by its id

GET /posts/{post_id}

Sample response

{
  "Post": {
    "title": "string",
    "content": "string",
    "published": true,
    "id": 0,
    "created_at": "2021-12-24T11:03:10.442Z",
    "user_id": 0,
    "owner": {
      "id": 0,
      "email": "user@example.com"
    }
  },
  "votes": 0
}

Update a post

PUT /posts/{post_id}

Sample request body

{
  "title": "string",
  "content": "string",
  "published": true
}

Sample response

{
  "title": "string",
  "content": "string",
  "published": true,
  "id": 0,
  "created_at": "2021-12-24T11:03:52.891Z",
  "user_id": 0,
  "owner": {
    "id": 0,
    "email": "user@example.com"
  }
}

Delete a post

DELETE /posts/{post_id}

4. Vote Endpoint

UpVote/Downvote a post

POST /vote/

Sample request body

{
  "post_id": 0,
  "direction": 1
}

Getting Started with Fast API and Docker

My Article

Acknowledgment

Special thanks to @Sanjeev-Thiyagarajan for his excellent and very thorough fastapi-youtube-tutorial-series