Skip to content

Ioana-Maraloi/HTTP-Client

Repository files navigation

Movie Library Management System

A C-based HTTP client application that interacts with a RESTful API to manage a movie library system with user authentication, admin privileges, and collection management.

Overview

This project implements a command-line client for a movie library management system. It supports multiple user roles (regular users and admins), JWT-based authentication, and comprehensive CRUD operations for movies and collections.

Features

Authentication & User Management

  • User Login/Logout: Standard user authentication with session management
  • Admin Login/Logout: Administrative access with elevated privileges
  • User Management: Admin capabilities to add, view, and delete users

Movie Operations

  • Get Access: Obtain JWT token for authenticated API access
  • View Movies: List all available movies or get detailed information about specific movies
  • Add Movie: Create new movie entries with title, year, description, and rating
  • Update Movie: Modify existing movie information
  • Delete Movie: Remove movies from the library

Collection Management

  • View Collections: List all collections or get detailed information about specific collections
  • Create Collection: Build custom movie collections
  • Add Movies to Collection: Populate collections with selected movies
  • Remove Movies from Collection: Remove specific movies from collections
  • Delete Collection: Remove entire collections

Technical Implementation

Architecture

The application is built using standard C libraries and implements HTTP client functionality from scratch:

  • Socket-based communication using sys/socket.h
  • JSON parsing and serialization with Parson library
  • Custom HTTP request builders (GET, POST, PUT, DELETE)
  • Session management with cookies and JWT tokens

Key Components

Helper Functions

  • cookie_find(): Locates cookie data in server responses
  • cookie_extract(): Extracts and stores session cookies
  • error_message(): Parses error messages from JSON responses
  • response_find(): Extracts HTTP response codes

Core Functionality

All operations follow a consistent pattern:

  1. Read user input and validate data
  2. Construct JSON payload (when needed)
  3. Build HTTP request with appropriate headers
  4. Send request to server
  5. Parse response and extract status code
  6. Display success or error message
  7. Clean up allocated memory

API Communication

  • Server: 63.32.125.183:8081
  • Base Path: /api/v1/tema/
  • Content Type: application/json
  • Authentication: Cookie-based sessions and JWT tokens

Implementation Approach

Development Process

  1. Integrated Parson library for JSON handling
  2. Implemented commands incrementally following the specification
  3. Created custom HTTP request functions (PUT, DELETE) based on existing GET/POST models
  4. Iteratively tested and refined output formatting

Design Decisions

Unified Login Function

Created a single login() function handling both regular and admin authentication:

  • Parameter login_admin = 0: Regular user login (includes admin_username field)
  • Parameter login_admin = 1: Admin login (username and password only)

Return Values for Composition

Functions like add_movie_to_collection() return status codes:

  • Return 1: Operation successful
  • Return 0: Operation failed

This enables complex operations like add_collection() to validate each step and rollback if needed.

Input Validation

  • Username validation: No whitespace allowed
  • ID validation: Numeric values only
  • Rating validation: Maximum value of 9.9

Error Handling

The application implements comprehensive error handling:

  • Input validation before API calls
  • HTTP status code checking
  • JSON error message extraction
  • Memory cleanup on both success and failure paths

Memory Management

Proper memory management throughout:

  • Dynamic allocation for all user inputs
  • Cleanup after each operation
  • Cookie and JWT token management
  • Response buffer handling

Challenges & Solutions

Output Formatting

Initial implementation displayed full server responses. Refined to match exact specification format, including proper use of # prefixes and structured output.

Collection Management

The add_collection() function required careful design:

  • Create empty collection first
  • Add movies one by one
  • On any failure, rollback by deleting the collection
  • Only show success if all operations complete

Solution: Implemented return codes in add_movie_to_collection() to enable proper error propagation and rollback logic.

Code Reusability

Instead of duplicating code, added optional parameters:

  • add_movie_to_collection(): Parameter indicates whether to read input or use provided IDs
  • delete_collection(): Parameter for collection ID to enable programmatic calls

Dependencies

  • Standard C libraries (stdio, stdlib, string, etc.)
  • POSIX socket libraries
  • Parson library for JSON parsing
  • Custom helpers, requests, and buffer modules

Usage

Compile and run the program, then enter commands interactively:

login
login_admin
add_user
get_users
delete_user
logout
logout_admin
get_access
get_movie
get_movies
add_movie
update_movie
delete_movie
get_collections
get_collection
add_collection
delete_collection
add_movie_to_collection
delete_movie_from_collection
exit

Project Structure

├── client.c              # Main implementation
├── requests.c/h          # HTTP request builders
├── helpers.c/h           # Utility functions
├── buffer.c/h            # Buffer management
└── parson.c/h           # JSON parser library

About

Command-line HTTP client written in C that interfaces with a RESTful API to manage a movie library system. Features include user authentication, admin privileges, JWT-based access control, and full CRUD operations for movies and collections using socket programming and JSON parsing.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors