WSGI (Web Server Gateway Interface)¶
HTTPConnection¶
-
class
baize.wsgi.
HTTPConnection
(environ: MutableMapping[str, Any])¶ A base class for incoming HTTP connections.
It is a valid Mapping type that allows you to directly access the values in any WSGI
environ
dictionary.-
property
client
¶ Client’s IP and Port.
Note that this depends on the
REMOTE_ADDR
andREMOTE_PORT
values given by the WSGI Server, and is not necessarily accurate.
-
property
url
¶ The full URL of this request.
-
property
path_params
¶ The path parameters parsed by the framework.
-
property
query_params
¶ Query parameter. It is a multi-value mapping.
-
property
headers
¶ A read-only case-independent mapping.
Note that in its internal storage, all keys are in lower case.
-
property
accepted_types
¶ Request’s accepted types
-
accepts
(media_type: str) → bool¶ e.g.
request.accepts("application/json")
-
property
content_length
¶ Request’s content-length
-
property
content_type
¶ Request’s content-type
Returns cookies in as a
dict
.NOTE: Modifications to this dictionary will not affect the response value. In fact, this value should not be modified.
-
property
date
¶ The sending time of the request.
NOTE: The datetime object is timezone-aware.
-
get
(k[, d]) → D[k] if k in D, else d. d defaults to None.¶
-
items
() → a set-like object providing a view on D’s items¶
-
keys
() → a set-like object providing a view on D’s keys¶
-
property
referrer
¶ The
Referer
HTTP request header contains an absolute or partial address of the page making the request.
-
values
() → an object providing a view on D’s values¶
-
property
Request¶
-
class
baize.wsgi.
Request
(environ: MutableMapping[str, Any])¶ -
property
method
¶ HTTP method. Uppercase string.
-
stream
(chunk_size: int = 4096) → Iterator[bytes]¶ Streaming read request body. e.g.
for chunk in request.stream(): ...
If you access
.stream()
then the byte chunks are provided without storing the entire body to memory. Any subsequent calls to.body
,.form
, or.json
will raise an error.
-
property
body
¶ Read all the contents of the request body into the memory and return it.
-
property
json
¶ Call
self.body
and usejson.loads
parse it.If
content_type
is not equal toapplication/json
, an HTTPExcption exception will be thrown.
-
property
form
¶ Parse the data in the form format and return it as a multi-value mapping.
If
content_type
is equal tomultipart/form-data
, it will directly perform streaming analysis, and subsequent calls toself.body
orself.json
will raise errors.If
content_type
is not equal tomultipart/form-data
orapplication/x-www-form-urlencoded
, an HTTPExcption exception will be thrown.
-
close
() → None¶ Close all temporary files in the
self.form
.This can always be called, regardless of whether you use form or not.
-
property
Response¶
-
class
baize.wsgi.
Response
(status_code: int = 200, headers: Optional[Mapping[str, str]] = None)¶ The parent class of all responses, whose objects can be used directly as WSGI application.
SmallResponse¶
-
class
baize.wsgi.
SmallResponse
(content: _ContentType, status_code: int = 200, headers: Optional[Mapping[str, str]] = None, media_type: Optional[str] = None, charset: Optional[str] = None)¶ Bases:
baize.wsgi.Response
,abc.ABC
,Generic
[baize.wsgi._ContentType
]Abstract base class for small response objects.
PlainTextResponse¶
-
class
baize.wsgi.
PlainTextResponse
(content: _ContentType, status_code: int = 200, headers: Optional[Mapping[str, str]] = None, media_type: Optional[str] = None, charset: Optional[str] = None)¶ Bases:
baize.wsgi.SmallResponse
[Union
[bytes
,str
]]
HTMLResponse¶
-
class
baize.wsgi.
HTMLResponse
(content: _ContentType, status_code: int = 200, headers: Optional[Mapping[str, str]] = None, media_type: Optional[str] = None, charset: Optional[str] = None)¶ Bases:
baize.wsgi.SmallResponse
[Union
[bytes
,str
]]
JSONResponse¶
-
class
baize.wsgi.
JSONResponse
(content: Any, status_code: int = 200, headers: Optional[Mapping[str, str]] = None, **kwargs: Any)¶ Bases:
baize.wsgi.SmallResponse
[Any
]**kwargs
is used to accept all the parameters thatjson.loads
can accept.
RedirectResponse¶
-
class
baize.wsgi.
RedirectResponse
(url: Union[str, baize.datastructures.URL], status_code: int = 307, headers: Optional[Mapping[str, str]] = None)¶ Bases:
baize.wsgi.Response
StreamResponse¶
-
class
baize.wsgi.
StreamResponse
(iterable: Iterable[bytes], status_code: int = 200, headers: Optional[Mapping[str, str]] = None, content_type: str = 'application/octet-stream')¶ Bases:
baize.wsgi.Response
FileResponse¶
-
class
baize.wsgi.
FileResponse
(filepath: str, headers: Optional[Mapping[str, str]] = None, content_type: Optional[str] = None, download_name: Optional[str] = None, stat_result: Optional[os.stat_result] = None, chunk_size: int = 262144)¶ Bases:
baize.wsgi.Response
,baize.responses.FileResponseMixin
File response.
It will automatically determine whether to send only headers and the range of files that need to be sent.
SendEventResponse¶
-
class
baize.wsgi.
SendEventResponse
(iterable: Iterable[baize.typing.ServerSentEvent], status_code: int = 200, headers: Optional[Mapping[str, str]] = None, *, ping_interval: int = 3, charset: str = 'utf-8')¶ Bases:
baize.wsgi.Response
Server-sent events response.
- Parameters
ping_interval – This determines the time interval (in seconds) between sending ping messages.
Router¶
-
class
baize.wsgi.
Router
(*routes: Union[Tuple[str, Interface], Tuple[str, Interface, str]])¶ A router to assign different paths to different WSGI applications.
- Parameters
routes – A triple composed of path, endpoint, and name. The name is optional. If the name is not given, the corresponding URL cannot be constructed through build_url.
applications = Router( ("/static/{filepath:any}", static_files), ("/api/{_:any}", api_app), ("/about/{name}", about_page, "about"), ("/", homepage, "homepage"), )
-
build_url
(name: str, params: Dict[str, Any]) → str¶ Find the corresponding route by the name of the route, and then construct the URL path.
Use {}
to mark path parameters, the format is {name[:type]}
. If type is not explicitly specified, it defaults to str
.
The built-in types are str
, int
, decimal
, uuid
, date
, any
. Among them, str
can match all strings except /
, and any
can match all strings.
If the built-in types are not enough, then you only need to write a class that inherits baize.routing.Convertor
and register it in baize.routing.CONVERTOR_TYPES
.
Subpaths¶
-
class
baize.wsgi.
Subpaths
(*routes: Tuple[str, Interface])¶ A router allocates different prefix requests to different WSGI applications.
NOTE: This will change the values of
environ["SCRIPT_NAME"]
andenviron["PATH_INFO"]
.applications = Subpaths( ("/static", static_files), ("/api", api_app), ("", default_app), )