0
0
Rest APIprogramming~5 mins

Resource-based design thinking in Rest API

Choose your learning style9 modes available
Introduction

Resource-based design thinking helps you organize your API around things (resources) like users or products. It makes APIs easy to understand and use.

When building a web API to manage data like users, orders, or products.
When you want your API to be simple and follow common web standards.
When you want to clearly separate different parts of your system by their data.
When you want to use HTTP methods (GET, POST, PUT, DELETE) in a meaningful way.
When you want your API to be easy for other developers to learn and use.
Syntax
Rest API
HTTP_METHOD /resource_name/{resource_id}

Example:
GET /users/123
POST /products
PUT /orders/456
DELETE /comments/789

Use nouns (resource names) in URLs, not verbs.

Use HTTP methods to describe actions: GET to read, POST to create, PUT to update, DELETE to remove.

Examples
This gets all books from the server.
Rest API
GET /books

Returns a list of books.
This adds a new book to the collection.
Rest API
POST /books

Creates a new book with data sent in the request body.
This changes details of the book number 10.
Rest API
PUT /books/10

Updates the book with ID 10.
This removes the book number 10 from the collection.
Rest API
DELETE /books/10

Deletes the book with ID 10.
Sample Program

This simple API manages a list of books as a resource. You can get all books, add a new book, update a book by ID, or delete a book by ID. It uses resource-based design thinking by treating books as resources with URLs and HTTP methods.

Rest API
from flask import Flask, jsonify, request

app = Flask(__name__)

books = [
    {"id": 1, "title": "Learn Python"},
    {"id": 2, "title": "REST API Basics"}
]

@app.route('/books', methods=['GET'])
def get_books():
    return jsonify(books)

@app.route('/books', methods=['POST'])
def add_book():
    new_book = request.get_json()
    books.append(new_book)
    return jsonify(new_book), 201

@app.route('/books/<int:book_id>', methods=['PUT'])
def update_book(book_id):
    updated_data = request.get_json()
    for book in books:
        if book['id'] == book_id:
            book.update(updated_data)
            return jsonify(book)
    return jsonify({'error': 'Book not found'}), 404

@app.route('/books/<int:book_id>', methods=['DELETE'])
def delete_book(book_id):
    for book in books:
        if book['id'] == book_id:
            books.remove(book)
            return jsonify({'message': 'Book deleted'})
    return jsonify({'error': 'Book not found'}), 404

if __name__ == '__main__':
    app.run(debug=True)
OutputSuccess
Important Notes

Always use clear and consistent resource names in your URLs.

Use HTTP status codes to tell if requests succeeded or failed.

Keep URLs simple and avoid actions in the path; use HTTP methods instead.

Summary

Resource-based design thinking organizes APIs around things (resources) like users or products.

Use HTTP methods (GET, POST, PUT, DELETE) to act on resources.

This approach makes APIs easier to understand and use.