Skip to content

zhengkyl/review-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

review-api

an attempt to learn how to use rust for a web server

Check src/main.rs for all API endpoints.

See review-ssh for a terminal user interface.

Endpoints

https://review-api.fly.dev

Public endpoints
All GET endpoints except for GET /auth
POST /users to create a new user

All other endpoints require authentication. This means the id cookie received from POST /auth needs to be sent with each request. This happens automatically if using a browser.

Example with curl

curl --location --request GET 'https://review-api.fly.dev/auth' \
--header 'Cookie: id=YOUR_SESSION_ID'

/auth

GET /auth

Check current user. This shows the email field.

Response body

{
  "id": 1,
  "name": "Kyle",
  "email": "[email protected]",
  "created_at": "2022-11-30T17:05:36.313355Z",
  "updated_at": "2022-11-30T17:05:36.313355Z"
}

DELETE /auth

This logs out the user.

204 OK

POST /auth

{
  "email": "[email protected]",
  "password": "password"
}

This logs in the user.

Response header

Key Value
set-cookie id=YOUR_SESION_ID_COOKIE; Path=/; Secure; HttpOnly;

Response body

{
  "id": 1,
  "name": "Kyle Zheng",
  "email": "[email protected]",
  "created_at": "2022-11-30T20:03:35.554592Z",
  "updated_at": "2022-11-30T20:03:35.554592Z"
}

/users

GET /users

Query params

Param Type Default
page 0 < integer 1
per_page 0 < integer < 51 10
sort_by FIELD.ORDER
FIELD is one of id, name, created_at, updated_at
ORDER is one of asc, desc
id.asc

Request body

{
  "results": [
    {
      "id": 1,
      "name": "Kyle",
      "created_at": "2022-11-30T17:05:36.313355Z",
      "updated_at": "2022-11-30T17:05:36.313355Z"
    },
    {
      "id": 3,
      "name": "Loid",
      "created_at": "2022-11-30T17:13:11.250255Z",
      "updated_at": "2022-11-30T17:27:53.894057Z"
    }
  ],
  "page": 1,
  "total_pages": 1,
  "total_results": 2
}

GET /users/{id}

Request body

{
  "id": 1,
  "name": "Kyle",
  "created_at": "2022-11-30T17:05:36.313355Z",
  "updated_at": "2022-11-30T17:05:36.313355Z"
}

POST /users

This creates a new user.

Request body

{
  "name": "Twilight",
  "email": "[email protected]",
  "password": "password"
}

Response body

{
  "id": 3,
  "name": "Twilight",
  "created_at": "2022-11-30T17:13:11.250255Z",
  "updated_at": "2022-11-30T17:13:11.250255Z"
}

PATCH /users/{id}

Request body

All fields are optional.

{
  "name": "Loid",
  "email": "[email protected]"
}

Response body

This shows the email field, because the route is authenticated.

{
  "id": 3,
  "name": "Loid",
  "email": "[email protected]",
  "created_at": "2022-11-30T17:13:11.250255Z",
  "updated_at": "2022-11-30T17:27:53.894057Z"
}

DELETE /users/{id}

Response body

{
  "deleted": 1
}

/search

GET /search/{category}?query=

Category is Film | Show

This mostly just a wrapper around the The Movie Database (TMDB) API.

https://developers.themoviedb.org/3/search/search-movies

https://developers.themoviedb.org/3/search/search-tv-shows

Query params

Param Type Default
page 0 < integer 1
lang ISO 639-1 value en-US
year integer n/a

Response body

{
  "page": 1,
  "results": [
    {
      "id": 505642,
      "title": "Black Panther: Wakanda Forever",
      "original_title": "Black Panther: Wakanda Forever",
      "original_language": "en",
      "release_date": "2022-11-09",
      "overview": "Queen Ramonda, Shuri, M’Baku, Okoye and the Dora Milaje fight to protect their nation from intervening world powers in the wake of King T’Challa’s death. As the Wakandans strive to embrace their next chapter, the heroes must band together with the help of War Dog Nakia and Everett Ross and forge a new path for the kingdom of Wakanda.",
      "poster_path": "/sv1xJUazXeYqALzczSZ3O6nkH75.jpg"
    }
  ],
  "total_results": 1,
  "total_pages": 1
}

/reviews

GET /reviews

Query params

Param Type Default
page 0 < integer 1
per_page 0 < integer < 51 10
sort_by FIELD.ORDER
FIELD is one of tmdb_id, created_at, updated_at
ORDER is one of asc, desc
updated_at.desc
user_id user id n/a
category Film | Show n/a
tmdb_id tmdb_id integer n/a
season season integer n/a
status Completed | Watching | Dropped | PlanToWatch n/a
fun_before bool n/a
fun_during bool n/a
fun_after bool n/a

Response body

{
  "results": [
    {
      "user_id": 1,
      "tmdb_id": 505642,
      "category": "Film",
      "status": "Completed",
      "text": "🙅🏿‍♂️",
      "fun_before": true,
      "fun_during": true,
      "fun_after": true,
      "created_at": "2022-11-30T18:09:58.829342Z",
      "updated_at": "2022-11-30T18:18:00.720356Z"
    }
  ],
  "page": 1,
  "total_pages": 1,
  "total_results": 1
}

POST /reviews

Request body

{
  "tmdb_id": 505642,
  "category": "Film",
  "status": "Completed"
}
or
{
  "tmdb_id": 505642,
  "category": "Show",
  "season": 1,
  "status": "Completed"
}

Response body

{
  "user_id": 1,
  "tmdb_id": 505642,
  "category": "Film",
  "status": "Completed",
  "text": "",
  "fun_before": false,
  "fun_during": false,
  "fun_after": false,
  "created_at": "2022-11-30T18:09:58.829342Z",
  "updated_at": "2022-11-30T18:09:58.829342Z"
}

PATCH /reviews/{category}/{tmdb_id}

PATCH /reviews/{category}/{tmdb_id}/{season}

Request body

All fields are optional.

{
  "status": "Completed",
  "text": "🙅🏿‍♂️",
  "fun_before": true,
  "fun_during": true,
  "fun_after": true
}

Response body

{
  "user_id": 1,
  "tmdb_id": 505642,
  "category": "Film",
  "status": "Completed",
  "text": "🙅🏿‍♂️",
  "fun_before": true,
  "fun_during": true,
  "fun_after": true,
  "created_at": "2022-11-30T18:09:58.829342Z",
  "updated_at": "2022-11-30T18:18:00.720356Z"
}

DELETE /reviews/{category}/{tmdb_id}

DELETE /reviews/{category}/{tmdb_id}/{season}

Response body

{
  "deleted": 1
}

Checklist

  • user auth
  • search movies and shows using TMDB api
  • add and edit reviews for movies and shows
  • allow better programmatic access to api
    • regeneratable api keys for users?

Based on following examples (and many more)

Run locally

Create .env file

Look at .env.sample for template.

Start local databases

sudo service postgresql start

sudo service redis-server start

Check data

sudo -u postgres psql

redis-cli

Local Docker

docker build -t review-api:latest .

# .env.dev should have localhost replaced with host.docker.internal
docker run --rm -p 8080:8080 --env-file .env.dev review-api

Connecting to deployed fly app

https://fly.io/docs/reference/private-networking/#private-network-vpn

Diesel reads DATABASE_URL in .env

check out db

psql -h <hostname> -p <port> -U <username> -d <database>

About

web service to search and review films and shows

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors