State Machine¶
Overview¶
RestMachine uses a webmachine-inspired state machine to process HTTP requests. The state machine handles content negotiation, authentication, authorization, and conditional requests automatically.
State Machine Callbacks¶
Customize request processing by providing state machine callbacks:
@app.get('/protected')
def protected_resource(request):
return {"data": "secret"}
# Add authentication callback
@app.state_machine_callback('is_authorized')
def check_auth(request):
"""Check if request is authorized."""
auth_header = request.headers.get('authorization')
if not auth_header or not auth_header.startswith('Bearer '):
return False
# Validate token...
return True
Available Callbacks¶
The state machine supports various callback points:
is_authorized
- Check authorizationforbidden
- Handle forbidden requestsmalformed_request
- Validate request formatallowed_methods
- Specify allowed HTTP methodscontent_types_provided
- Available response formatscontent_types_accepted
- Accepted request formats
See Also¶
- State Machine Guide - Complete guide with diagrams
- Application API - Register callbacks
- Authentication Guide - Auth patterns