API for Assignments 3.1 and 3.2

Written by Michael

This page describes the endpoints for the API you will use as a client in assignment 3.1 and implement in assignment 3.2.

General Notes


GET /

An endpoint to confirm the API is running. You won't have to use it, but it may be useful for testing. For assignment 3.2, it is already implemented.

Returns: An object with basic info about the database.

Example:

> GET /
Status: 200

{
  "db": "cs193x_mchang91",
  "numUsers": 1,
  "numPosts": 1
}

GET /users

Get a list of existing users.

Returns: An object with a single key, users, mapping to an array of user IDs.

Example:

> GET /users
Status: 200

{
  "users": [
    "mchang",
    "kashif"
  ]
}

GET /users/:id

Get the profile for the user id.

Returns: An object with the keys id, name, avatarURL (which are all strings), and following (which is an array of user IDs the given user is following).

Errors:

Examples:

> GET /users/mchang
Status: 200

{
  "id": "mchang",
  "name": "Michael",
  "avatarURL": "images/stanford.png",
  "following": [
    "kashif"
  ]
}
> GET /users/nobody
Status: 404

{
  "error": "No user with ID nobody"
}

POST /users

Create a new user. The new user's name is the same as their ID, their avatar URL is the default avatar (images/default.png), and they are not following anyone.

Request body: An object with a single key id, whose value is the new user's ID.

Returns: Same as GET /users/:id after user is created.

Errors:

Example:

> POST /users
> {"id":"binky"}
Status: 200

{
  "id": "binky",
  "name": "binky",
  "avatarURL": "images/default.png",
  "following": []
}

PATCH /users/:id

Update a user's profile (name or avatar URL). User IDs cannot be changed, but including the id in the request body is allowed.

If the specified display name is the empty string, the user's display name is reset to their user ID. If the specified avatar URL is the empty string, the user's avatar is reset to the default avatar (images/default.png).

Request body: An object with any number of the keys id (ignored), name, and avatarURL.

Note: Explicitly sending an empty string for name/avatarURL will set them to their defaults; not including them at all will leave them unchanged.

Returns: Same as GET /users/:id after user is updated.

Errors:

Note: For assignment 3.1, our API checks if extra keys are included in the request body and returns an error if so. For assignment 3.2, you do not have to check for this case.

Example:

> PATCH /users/mchang
> {"name":"Michael Chang"}
Status: 200

{
  "id": "mchang",
  "name": "Michael Chang",
  "avatarURL": "images/stanford.png",
  "following": [
    "kashif"
  ]
}

GET /users/:id/feed

Get a user's feed. This includes posts from all users they are following, as well as their own. Posts are sorted in reverse chronological order (newest first).

Returns: An object with a single key posts, mapping to an array. Each element has the following structure:

{
  "user": {
    "id": <the poster's user ID>,
    "name": <the poster's display name>,
    "avatarURL": <the poster's avatar URL>
  },
  "time": <the date and time the post was made>,
  "text": <the post text>
}

Errors:

Example:

> GET /users/mchang/feed
Status: 200

{
  "posts": [
    {
      "user": {
        "id": "kashif",
        "name": "kashif",
        "avatarURL": "images/default.png"
      },
      "time": "2022-02-08T02:51:12.431Z",
      "text": "My new post"
    },
    {
      "user": {
        "id": "mchang",
        "name": "Michael Chang",
        "avatarURL": "images/stanford.png"
      },
      "time": "2022-02-07T19:32:52.063Z",
      "text": "Welcome to the Generic Social Media App!"
    }
  ]
}

POST /users/:id/posts

Create a new post by the given user. The post time is set to the current date/time.

Request body: An object with a single key text, containing the post text.

Returns: The exact object { "success": true }.

Errors:

Example:

> POST /users/kashif/posts
> {"text":"My new post"}
Status: 200

{
  "success": true
}

POST /users/:id/follow

Have the user id follow the target user.

Query string: target: the ID of the user to follow

Returns: The exact object { "success": true }.

Errors:

Examples:

> POST /users/mchang/follow?target=binky
Status: 200

{
  "success": true
}
> POST /users/mchang/follow?target=kashif
Status: 400

{
  "error": "mchang is already following kashif"
}

DELETE /users/:id/follow

Have the user id stop following the target user.

Query string: target: the ID of the user to stop following

Returns: The exact object { "success": true }.

Errors:

Example:

> DELETE /users/mchang/follow?target=kashif
Status: 200

{
  "success": true
}