Activity 28: Research Rest API

Funda, Mark Harvey D. BSIT 4TH Year

What is REST API?

REST (Representational State Transfer) is an architectural style used for designing networked applications. It uses HTTP protocols to enable communication between clients (e.g., browsers or apps) and servers, allowing them to create, read, update, and delete (CRUD) resources over a network.

Key Principles of REST API

  1. Statelessness: Each request from a client to a server must contain all necessary information to be understood and processed independently. The server does not store client context between requests.

  2. Client-Server Separation: The client and server operate independently, communicating only through the API. This allows each to evolve without dependency.

  3. Uniform Interface: REST APIs have a standardized method for accessing and modifying resources, typically using HTTP methods like GET, POST, PUT, and DELETE.

  4. Cacheability: Responses should indicate whether they are cacheable to improve performance and reduce server load.

  5. Layered System: A REST API can use intermediate servers (e.g., for security or caching) without clients needing to know about them.

  6. Code on Demand (optional): Servers can provide executable code to the client to extend functionality (e.g., JavaScript for dynamic behavior)​.

How REST APIs Work

  1. Resource Identification: Resources (e.g., users, orders) are identified via URLs.

  2. Resource Representation: Data is represented in formats like JSON or XML.

  3. HTTP Methods:

    • GET: Retrieve a resource.

    • POST: Create a new resource.

    • PUT: Update an existing resource.

    • DELETE: Remove a resource.

  4. Self-Descriptive Messages: Requests and responses contain all necessary details, including resource metadata and hyperlinks to related actions.

For example, to fetch a user's data:
GET /users/{id} returns JSON like:

jsonCopy code{
  "id": 1,
  "name": "John Wick",
  "email": "johnwick@gmail.com"
}

Common Use Cases

  1. Web Applications: Backend APIs support web frontends by managing data (e.g., e-commerce platforms).

  2. Mobile Applications: REST APIs enable mobile apps to interact with servers for data synchronization (e.g., social media apps).

  3. Integration with Third-Party Services: APIs allow communication between different services, such as payment gateways or map services.

  4. IoT Devices: REST APIs facilitate communication between devices like smart thermostats and their controllers​.

Best Practices for Implementing REST APIs in Angular

  1. Use Angular Services: Centralize API calls in Angular services to separate data access logic from components.

  2. HttpClient Module: Leverage Angular's HttpClient for making HTTP requests.

  3. Error Handling: Implement robust error handling using interceptors to manage responses globally.

  4. Observables: Use RxJS observables for handling asynchronous API calls efficiently.

  5. Environment Variables: Store API base URLs in Angular's environment configuration for maintainability.

  6. Versioning: Include versioning in API endpoints (e.g., /api/v1/) to maintain backward compatibility.

  7. Security: Implement authentication (e.g., JWT) and ensure API calls use HTTPS to secure data in transit​.

Examples in Development

  • Web App: An Angular e-commerce app fetches products using GET /products, displays details, and enables adding to cart via POST /cart.

  • Mobile App: A weather app retrieves data using GET /weather?location=city.