Middle — Senior
How did you design the system using RESTful architecture principles?
sobes.tech AI
Answer from AI
When designing a system using RESTful architecture principles in Python, I follow the main principles:
- Resource-oriented: each system entity is represented as a resource with a unique URI.
- Use of standard HTTP methods: GET for retrieving data, POST for creating, PUT/PATCH for updating, DELETE for deleting.
- HTTP status codes: proper use of response codes to reflect the operation result (e.g., 200, 201, 404, 400, 500).
- Statelessness: the server does not store client state between requests.
- Use of data formats such as JSON for data exchange.
Example using Flask:
from flask import Flask, jsonify, request
app = Flask(__name__)
items = {}
@app.route('/items/<int:item_id>', methods=['GET'])
def get_item(item_id):
item = items.get(item_id)
if item:
return jsonify(item), 200
else:
return jsonify({'error': 'Not found'}), 404
@app.route('/items', methods=['POST'])
def create_item():
data = request.get_json()
item_id = len(items) + 1
items[item_id] = data
return jsonify({'id': item_id}), 201
if __name__ == '__main__':
app.run()
This approach ensures simplicity, scalability, and compatibility with clients.