API Reference

class plugapi.http.AutoName(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Class for creating an enum with the name of the enum as the value

Inherits from:

enum.Enum – The base enum class

Methods:

_generate_next_value_ – Generates the next value

class plugapi.http.FileResponse(body: str | list | dict | ~io.TextIOBase | ~_io.BytesIO, status: int = 200, headers: dict[str, str] = <factory>, attachment: bool = False, includeFilename: bool = True)

Class for sending a file to a client

Inherits from:

Response – The base response class

Attributes:

body {io.TextIOBase | io.BytesIO | str} – The body of the response attachment {bool} – Whether to display the file in the browser (inline) or as an attachment (default: {False (inline)}) includeFilename {bool} – Whether to include the filename in the Content-Disposition header (default: {True})

class plugapi.http.HTMLResponse(body: str | list | dict | ~io.TextIOBase | ~_io.BytesIO, status: int = 200, headers: dict[str, str] = <factory>)

Class for creating a HTML response to send to a client

Inherits from:

Response – The base response class

Attributes:

body {str} – The body of the response

class plugapi.http.JSONPResponse(body: str | list | dict | ~io.TextIOBase | ~_io.BytesIO, status: int = 200, headers: dict[str, str] = <factory>, callback: str = 'callback')

Class for creating a JSONP response to send to a client

Inherits from:

Response – The base response class

Attributes:

body {str} – The body of the response callback {str} – The name of the callback function (default: {“callback”})

class plugapi.http.JSONResponse(body: str | list | dict | ~io.TextIOBase | ~_io.BytesIO, status: int = 200, headers: dict[str, str] = <factory>)

Class for creating a JSON response to send to a client

Inherits from:

Response – The base response class

Attributes:

body {str | list | dict} – The body of the response

class plugapi.http.RedirectResponse(body: str = '', status: int = 200, headers: dict[str, str] = <factory>, to: str = '')

Class for creating a redirect response to send to a client

Inherits from:

Response – The base response class

Attributes:

to {str} – The URL to redirect to (default: {“”} (due to dataclass limitations))

class plugapi.http.Request(path: str, method: RequestMethod, headers: dict[str, str], body: str | list | dict, query: dict[str, list[str]], params: dict[str, str], cookie: str | dict[str, str])

Class containing the request data

Attributes:

path {str} – The path of the request method {RequestMethod} – The method of the request headers {dict[str, str]} – The request headers body {str | list | dict} – The body of the request (if any) query {dict[str, list[str]]} – The query string at the end of the path params {dict[str, str]} – The parameters in the path cookie {str | dict[str, str]} – The cookie of the request (if any) (dict if parsed by cookie_middleware)

class plugapi.http.RequestMethod(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

The enum containing HTTP request methods

Inherits from:

AutoName – The base enum class

class plugapi.http.Response(body: str | list | dict | ~io.TextIOBase | ~_io.BytesIO, status: int = 200, headers: dict[str, str] = <factory>)

Class for creating a response to send to a client

Attributes:

body {str | list | dict | io.TextIOBase | io.BytesIO} – The body of the response status {int} – The status code of the response (default: {200}) headers {dict[str, str]} – The headers of the response (default: {})

send(client: socket)

Sends the response to a client

Arguments:

client {socket.socket} – The client to send the response to

to_bytes() bytes

Converts the response to bytes

Returns:

bytes: The response in bytes

class plugapi.http.Server(port: int, timeout: int = 5, host: str = 'localhost', https: bool = False, certfile: str | None = None, keyfile: str | None = None, shouldLogErrors: bool = True)

Class for creating a HTTP(S) server

Attributes:

host {str} – The host of the server port {int} – The port of the server timeout {int} – The timeout before the server closes the connection socket {socket.socket} – The socket of the server threads {list[Thread]} – The connection threads lock {Lock} – The lock for the threads list handlers {dict[RequestMethod, dict[str, callable]]} – The handlers for the requests https {bool} – Whether the server is using HTTPS certfile {str | None} – The certificate file for HTTPS keyfile {str | None} – The key file for HTTPS middlewares {list[callable]} – The middlewares for the requests context {ssl.SSLContext | None} – The SSL context for HTTPS shouldLogErrors {bool} – Whether the server should log errors to the console

add_middlewares(*middlewares)

Adds middlewares to the server

Arguments:

*middlewares {callable} – The middlewares to add

handler(path: str = None, method: RequestMethod | list[plugapi.http.RequestMethod] = RequestMethod.GET) callable

The decorator for the handlers

Arguments:

path {str} – The path of the handler method {RequestMethod | list[RequestMethod]} – The HTTP method the handler can receive (default: {RequestMethod.GET})

Returns:

callable – The decorator

run()

Runs the server

plugapi.http.cookie_middleware(socket: socket, method: str, headers: dict[str, str], body: str | list | dict) tuple[dict[str, str], str | list | dict]

Middleware for parsing cookies

Arguments:

socket {socket.socket} – The socket of the client method {str} – The method of the request headers {dict[str, str]} – The headers of the request body {str | list | dict} – The body of the request

Returns:

tuple[dict[str, str], str | list | dict] – The headers and body of the request

plugapi.http.cors_middleware(socket: socket, method: str, headers: dict[str, str], body: str | list | dict) tuple[dict[str, str], str | list | dict]

Middleware for adding CORS headers

Arguments:

socket {socket.socket} – The socket of the client method {str} – The method of the request headers {dict[str, str]} – The headers of the request body {str | list | dict} – The body of the request

Returns:

tuple[dict[str, str], str | list | dict] – The headers and body of the request

plugapi.http.json_middleware(socket: socket, method: str, headers: dict[str, str], body: str | list | dict) tuple[dict[str, str], str | list | dict]

Middleware for parsing JSON

Arguments:

socket {socket.socket} – The socket of the client method {str} – The method of the request headers {dict[str, str]} – The headers of the request body {str | list | dict} – The body of the request

Returns:

tuple[dict[str, str], str | list | dict] – The headers and body of the request

plugapi.http.multipart_middleware(socket: socket, method: str, headers: dict[str, str], body: str | list | dict) tuple[dict[str, str], str | list | dict]

Middleware for parsing multipart data

Arguments:

socket {socket.socket} – The socket of the client method {str} – The method of the request headers {dict[str, str]} – The headers of the request body {str | list | dict} – The body of the request

Returns:

tuple[dict[str, str], str | list | dict] – The headers and body of the request

plugapi.http.parse_multipart(body: bytes, boundary: bytes) dict[str, str]

Parses a multipart form

Arguments:

body {str} – The body of the request boundary {str} – The boundary of the multipart form

Returns:

dict[str, str] – The parsed multipart form

plugapi.http.url_encoded_middleware(socket: socket, method: str, headers: dict[str, str], body: str | list | dict) tuple[dict[str, str], str | list | dict]

Middleware for parsing URL encoded data

Arguments:

socket {socket.socket} – The socket of the client method {str} – The method of the request headers {dict[str, str]} – The headers of the request body {str | list | dict} – The body of the request

Returns:

tuple[dict[str, str], str | list | dict] – The headers and body of the request