Package: wsgirouter3
wsgirouter3Small opinionated WSGI request dispatcher. Influenced by Flask.Works using path segments instead of more common regex matching (RFC 3986 path segment parameters are not supported)https://datatracker.ietf.org/doc/html/rfc3986#section-3.3Path variables are by default defined using RFC 6570 level 1https://datatracker.ietf.org/doc/html/rfc6570#section-1.2Start and optional end markers are customizable.
Path variable types are defined using python typing information. Customizable, types supported out-of-box: bool, int, str, uuid.UUID.WSGI environ wrapper can passed to handler if there is parameter with type Request. No global variables/threadlocals.
Request body and query string binding is also supported, using generic types Body and Query.Supports overlapping path segments: zero or more literal segments can overlap with one parameter definition. Parameters of different type and/or name in same position are not supported. Literal segment takes precedence.Route decorators for HTTP methods DELETE, GET, PATCH, POST, PUT.Response compression. By default enabled for application/json. Configurable compression level.@router.get('/abc/literal')defliteral():pass@router.get('/abc/{variable}')defparameterized(variable:str):passMultiple routes can point to same handler:@router.get('/abc',defaults={'variable':None})@router.get('/abc/{variable}')defwith_optional_parameter(variable:Optional[str]):passContent negotiation:@router.get('/get',produces='application/json')defget()->dict:return{'field':'value'}@router.post('/post',consumes='application/json')defpost_with_json(req:Request)->Tuple[int]:data=req.jsonreturn204,Query string and request body binding:@router.get('/get',produces='application/json')defget(query:Query[dict])->dict:returnquery@router.post('/post',consumes='application/json')defpost_with_json(data:Body[dict])->Tuple[int]:# do something with datareturn204,Handler return type handling:TypeDescriptiontupleshortcut for returning status code and optional result + headersNoneallowed for status codes which have no contentdictapplication/jsonstrdefined by optional Content-Type header. When header is missing, taken from config.default_str_content_type, by default text/plain;charset=utf-8bytesdefined by required Content-Type headerdataclassapplication/json, but overridable by custom result handlertyping.GeneratorTypepassed as isCache control for responses:@router.get('/no-store',cache_control=CacheControl.no_store)defno_store()->dict:return{'a':1}@router.get('/store',cache_control=CacheControl.of(max_age=600,private=False))defstore()->dict:return{'a':1}Configuration checklistWsgiAppConfig classTaskActionWant to use another json library or configure value types serialized / deserializedOverride json_serializer / json_deserializerChange maximum length of accepted request bodySet value of max_request_content_lengthChange default content type for str returnedChange value of default_str_content_typeAdd authorizationSet before_request hook handler, use route options to define roles. SeesampleHandle more return typesAdd entry of tuple[matcher, handler] to result_converters or override custom_result_handlerValidate/convert query string and request bodyUse Query and Body generics with value class in handler and override binderCustomize error handlingOverride error_handlerDisable response compressionset compress_level to 0Enable response compression for more casesUpdate compress_content_types and/or override can_compress_resultConfigure compression levelSet value of compress_level (0-9 or -1, see zlib documentation)PathRouter classTaskActionChange parameter markersChange value of path_parameter_start and path_parameter_endAdd new path parameter typeAdd new class inherited from PathParameter into parameter_typesInstallation$pipinstallwsgirouter3