Senior
Homework #2: Design a REST API for a social network VKontakte with the following operations: • add and remove friends; • view user's friends; • view user's profile; • publish a post in the feed; • upload media files for posts; • view the feed (home and user feeds); • view dialogues and chats; • send and read messages in dialogues and chats. addFriend(post) POST /users/{id}/friends deleteFriend(delete) DELETE /users/{id}/friends/{friends_id} getUserFriends(get) GET /users/{id}/friends getUserForm(get) GET /users/{id}/profile createPost(post) POST /posts uploadMediaFile(post) POST /media getFeed(home/user) GET /feed GET /users/{id}/feed getDialogues(get) GET /dialogs sendMessage(post) POST /dialogs/{id}/messages getMessage(get) GET /dialogs/{id}/messages
sobes.tech AI
Answer from AI
Designing a REST API for a social network like VKontakte with the specified operations can be represented as follows:
-
Add a friend:
- Method: POST
- Path:
/users/{id}/friends - Request body: ID of the user to be added as a friend
-
Remove a friend:
- Method: DELETE
- Path:
/users/{id}/friends/{friend_id}
-
View user's friends:
- Method: GET
- Path:
/users/{id}/friends
-
View user's profile:
- Method: GET
- Path:
/users/{id}/profile
-
Publish a post:
- Method: POST
- Path:
/posts - Request body: post content, author ID
-
Upload media files:
- Method: POST
- Path:
/media - Request body: file (multipart/form-data)
-
View post feed:
- Home feed:
- Method: GET
- Path:
/feed
- Specific user's feed:
- Method: GET
- Path:
/users/{id}/feed
- Home feed:
-
View dialogs and chats:
- Method: GET
- Path:
/dialogs
-
Send messages:
- Method: POST
- Path:
/dialogs/{id}/messages - Request body: message text, attachments
-
Read messages:
- Method: GET
- Path:
/dialogs/{id}/messages
This design aligns with REST principles, where resources are represented as URLs, and HTTP methods define actions.
Example of a request to add a friend (curl):
curl -X POST https://api.vk.com/users/123/friends -d '{"friend_id":456}' -H "Content-Type: application/json"
This allows the client to add the user with ID 456 as a friend of the user with ID 123.