Skip to content

Instantly share code, notes, and snippets.

@briu
Created November 13, 2024 14:53
Show Gist options
  • Select an option

  • Save briu/d1723f842ce8349aace9425429a4d2ef to your computer and use it in GitHub Desktop.

Select an option

Save briu/d1723f842ce8349aace9425429a4d2ef to your computer and use it in GitHub Desktop.
forum api

forum app

Authentication through http-header Authentication

Endpoints

GET /api/v1/users - endpoint for receiving users

Required headers:

Authorization - access token.

Content-Type: application/json - content type.

Allowed parameters:

page, per_page - in case of pagination by pages

cursor, per_page - in case of infinite pagination

Example of successfull response:

{
  "users": [
    {
      "id": 1,
      "name": "Paul",
      "comments_count": 10,
      "updated_at":1627651413,
      "avatar_url": null,
      "latest_comment": {
        "id": 3,
        "text": "Hello, bro!"
      }
    },
    {
      "id": 2,
      "name": "Joe",
      "comments_count": 1,
      "updated_at":1627651414,
      "avatar_url": "/rails/active_storage/representations/eyJfcmFpbHMiOnsib",
      "latest_comment": {
        "id": 5,
        "text": "Awesome forum!"
      }
    }
  ],
  "page": 2
}

avatar_url could be null it means that user hasn't avatar Other fields should have values(for real usage here could be table with each field description)

Response code: 200

When user unauthorized for this request response code: 401

GET /api/v1/users/:id/comments - endpoint for receiving user comments

Required headers:

Authorization - access token.

Content-Type: application/json - content type.

Example of successfull response:

{
  "comments": [
    {
      "id": 5,
      "text": "Hello, bro!",
      "created_at":1627651413,
      "author": {
        "id": 1,
        "name": "Paul",
        "comments_count": 10,
        "created_at":1627651413,
      }
    },
    {
      "id": 2,
      "text": "Awesome forum!",
      "created_at":1627651414,
      "author": {
        "id": 1,
        "name": "Joe",
        "comments_count": 1,
        "created_at":1627651414,
      }
    }
  ],
  "page": 4
}

Response code: 200

Allowed parameters:

page, per_page - in case of pagination by pages.

cursor, per_page - in case of infinite pagination

In all routes we use only restfull resources, so if we need comment likes we do not create /api/v1/comments/liked action but instead add query parameter to /api/v1/comments action liked=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment