This project is a REST API web client that allows users to communicate with a remote server using socket connections and HTTP requests. It provides functionalities for user authentication, library access, and book management, using GET
, POST
, and DELETE
requests. The client is built in C++ and interacts with the server through manually constructed HTTP requests, making it lightweight and efficient.
- register_credentials() β Registers a new user by sending user credentials to the server.
- login() β Authenticates the user by verifying login credentials with the server and retrieves a session cookie.
- logout() β Terminates the current session and clears authentication tokens and cookies.
- enter_library() β Grants access to the virtual library by sending an authentication request and retrieves a
JWT
token for further operations.
- get_books() β Retrieves a list of all books available in the library.
- get_book() β Retrieves detailed information about a specific book using its unique ID.
- add_book() β Adds a new book to the library by sending book details to the server.
- del_book() β Deletes a book from the library using its unique ID.
Purpose:
Retrieves data from the server, such as a list of books or details of a specific book.
Parameters:
- host β The server's hostname or IP address.
- url β The API endpoint to which the request is sent.
- query_params β Optional query parameters to append to the URL.
- token β
JWT
token for authentication (optional). - cookies β A vector of session cookies (optional).
- cookies_count β The number of cookies to include in the request.
Process:
- Constructs the
GET
request line, appending the URL and any query parameters if provided. - Adds necessary HTTP headers, including:
- Host: The server's hostname or IP address.
- Authorization: Bearer
token
(if authentication is required). - Cookie:
session_data
(if session-based authentication is needed).
- Terminates the request with an empty line to signal the end of headers.
- Sends the request over the established socket connection and waits for the server's response.
Purpose:
Sends data to the server, such as user authentication credentials or book details.
Parameters:
- host β The server's hostname or IP address.
- url β The API endpoint to which the request is sent.
- token β
JWT
token for authentication (optional). - content_type β The format of the request body (e.g.,
application/json
). - body β A JSON string containing the request data.
- body_fields_nr β The number of fields in the request body.
- cookies β A vector of session cookies (optional).
- cookies_count β The number of cookies to include in the request.
Process:
- Constructs the
POST
request line, specifying the HTTP method and URL. - Adds necessary HTTP headers, including:
- Host: The server's hostname or IP address.
- Authorization: Bearer
token
(if authentication is required). - Content-Type: Specifies the format of the request body (e.g.,
application/json
). - Content-Length: The length of the
JSON body
.
- Includes the request body (
JSON payload
) in the request. - Sends the request over the socket connection and processes the server's response.
Purpose:
Removes a resource from the server, such as deleting a book from the library.
Parameters:
- host β The server's hostname or IP address.
- url β The API endpoint to which the request is sent.
- token β
JWT
token for authentication (optional). - content_type β The format of the request body (usually empty for
DELETE
requests). - body β A JSON string containing the request data (usually empty for
DELETE
requests). - body_fields_nr β The number of fields in the request body.
- cookies β A vector of session cookies (optional).
- cookies_count β The number of cookies to include in the request.
Process:
- Constructs the
DELETE
request line, specifying the HTTP method and URL. - Adds necessary HTTP headers, including:
- Host: The server's hostname or IP address.
- Authorization: Bearer
token
(if authentication is required). - Content-Type: Specifies the format of the request body (if applicable).
- Content-Length: The length of the
JSON body
(if applicable).
- Sends the request over the socket connection and processes the server's response.
Purpose:
Retrieves the server's response from the socket and processes it.
Process:
- Reads the server's response from the socket connection.
- Parses the HTTP response to extract the status code, headers, and body.
- Stores the response for further processing.
Purpose:
Extracts error messages from JSON responses and handles them appropriately.
Process:
- Parses the JSON response to check for error messages.
- If an error is found, formats and prints the error message to the user.
Purpose:
Retrieves the HTTP status code from the server's response.
Process:
- Parses the server's response to extract the HTTP status code (e.g.,
200 OK
,404 Not Found
). - Returns the status code for further processing.
Purpose:
Retrieves the JSON-formatted data from the server's response.
Process:
- Parses the server's response to extract the
JSON body
. - Returns the JSON data for further processing.
Purpose:
Creates a new user account by sending registration details to the server.
Process:
- Prompts the user to enter a username and password.
- Formats the credentials into a JSON object.
- Sends a
POST
request to the server with the registration details. - Processes the server's response to confirm successful registration.
Purpose:
Authenticates the user by verifying login credentials with the server.
Process:
- Prompts the user to enter a username and password.
- Formats the credentials into a JSON object.
- Sends a
POST
request to the server to authenticate the user. - Extracts the session cookie from the server's response for future requests.
Purpose:
Terminates the current session and invalidates authentication credentials.
Process:
- Sends a
GET
request to the server's logout endpoint. - Clears the session cookie and
JWT
token, effectively logging the user out.
Purpose:
Allows the user to enter the virtual library by sending an authentication request.
Process:
- Sends a
GET
request to the server with the authentication token. - Extracts and stores the
JWT
token from the server's response for future library operations.
Purpose:
Retrieves a list of all books available in the library.
Process:
- Sends a
GET
request to the server to retrieve all books. - Parses the server's response to display the list of books.
Purpose:
Retrieves detailed information about a specific book using its unique ID.
Process:
- Prompts the user to enter the book's ID.
- Sends a
GET
request to the server to fetch the book's details. - Parses the server's response to display the book's information.
Purpose:
Adds a new book to the library by sending book details to the server.
Process:
- Prompts the user to enter the book's details (title, author, genre, page count, publisher).
- Formats the book details into a JSON object.
- Sends a
POST
request to the server with the book data. - Processes the server's response to confirm successful addition.
Purpose:
Removes a book from the library using its unique ID.
Process:
- Prompts the user to enter the book's ID.
- Sends a
DELETE
request to the server to remove the book. - Processes the server's response to confirm successful deletion.