This solution implements an HTTP client that accepts user commands and communicates with a REST architecture server. The client sends HTTP requests to the server and handles responses through success or error messages. Data exchange is performed in JSON format.
The implementation builds upon the 09. HTTP Protocol Communication Protocols
laboratory solution, which already contained some HTTP request implementations
and server interaction mechanisms. My implemented commands are located in
commands.cpp, with helper functions added to helpers.cpp.
I used the json.hpp and json_fwd.hpp files from the recommended C++ library,
nlohmann, to work with data in JSON format. I also used the json::parse and
json::dump functions to transform strings into JSON and vice versa, as well as
the [] operator to access values within JSON objects.
-
login_admin/login_user- Obtain credentials from keyboard, check for spaces, check if user isn't already authenticated by validating cookies and send authentication request. On200response, save cookies and set admin flag (for admin login). On403, credentials are incorrect. -
logout_admin/logout_user- Verify authentication status and send logout request. On200response, clear cookies/JWT and reset admin flag (for admin logout).
All user management commands require admin authentication.
-
add_user- Obtain and validate credentials and send user creation request. On201response, save cookies. -
get_users- Send request to retrieve user list. On200response, display username and password for each user. -
delete_user- Obtain and validate username and send deletion request. On200response, save cookies. On404, user was not found.
get_access- Verify admin status and send access request. On200response, save cookies and JWT token.
All movie operations require library access.
-
add_movie- Obtain and validate movie information and send data as JSON. On201response, save cookies. -
get_movies- Retrieve and display movie list with ID and title for each entry. -
get_movie- Obtain movie ID, complete path with ID and send request. On200response, display movie details (title, year, description, rating). On404, movie not found. -
update_movie- Obtain movie ID and information, send JSON with updated path. On404if movie not found. -
delete_movie- Obtain movie ID, complete path with ID for deletion. Returns404if movie not found.
All collection operations require library access.
-
add_collection- Obtain collection information (title, number of movies, movie IDs) and send as JSON. On201response, save cookies and add each movie usingadd_movie_to_collection. Display success only if all movies were added successfully, otherwise display error. -
add_movie_to_collection- This function can be called both from withinadd_collectionand fromclient.cppas a standalone command, indicated by theis_standaloneflag.- When called from
add_collection, it receives movie and collection IDs as parameters, does not read them from keyboard, and does not check library access since this was already done inadd_collection. It returns success or error codes instead of displaying error messages. - When called from
client.cpp, it reads movie and collection IDs from keyboard, checks library access, and if access is granted, sends the request to the server with movie information in JSON format. If the response is201, it saves cookies and displays a success message. If the response is404, it indicates that the collection was not found by the ID entered by the user, or the user does not have access to the collection. If the response is400, it indicates that the movie already exists in the collection. For other response codes, it displays the corresponding message.
- When called from
-
delete_movie_from_collection- Obtain movie and collection IDs, complete path with both IDs for removal. -
get_collections- Retrieve and display collection list with ID and title for each entry. -
get_collection- Obtain and validate collection ID and display collection information including owner, title and movie details (ID and title for each film). -
delete_collection- Obtain collection ID and send deletion request with ID in path.