Skip to content

nserver.server 🔗

Blueprint 🔗

Blueprint(name: str)

Bases: Scaffold

Class that can replicate many of the functions of a NameServer.

They can be used to construct or extend applications.

New in 2.0.

Parameters:

Name Type Description Default
name str

The name of the server. This is used for internal logging.

required

after_query 🔗

after_query()

Decorator for registering after_query hook.

These functions are after the rule function is run and may modify the response.

before_first_query 🔗

before_first_query()

Decorator for registering before_first_query hook.

These functions are called when the server receives it's first query, but before any further processesing.

before_query 🔗

before_query()

Decorator for registering before_query hook.

These functions are called before processing each query.

entrypoint 🔗

entrypoint(query: Query) -> Response

Entrypoint into this Blueprint.

This method should be passed to rules as the function to run.

exception_handler 🔗

exception_handler(exception_class: Type[Exception])

Decorator for registering a function as an exception handler

New in 2.0.

Parameters:

Name Type Description Default
exception_class Type[Exception]

The Exception class to register this handler for

required

register_after_query 🔗

register_after_query(
    func: middleware.AfterQueryHook,
) -> None

Register a function to be run on the result of a query.

Parameters:

Name Type Description Default
func AfterQueryHook

the function to register

required

register_before_first_query 🔗

register_before_first_query(
    func: middleware.BeforeFirstQueryHook,
) -> None

Register a function to be run before the first query.

Parameters:

Name Type Description Default
func BeforeFirstQueryHook

the function to register

required

register_before_query 🔗

register_before_query(
    func: middleware.BeforeQueryHook,
) -> None

Register a function to be run before every query.

Parameters:

Name Type Description Default
func BeforeQueryHook

the function to register If func returns anything other than None will stop processing the incoming Query and continue to result processing with the return value.

required

register_blueprint 🔗

register_blueprint(
    blueprint: Blueprint,
    rule_: Union[Type[RuleBase], str, Pattern],
    *args,
    **kwargs
) -> None

Register a blueprint using smart_make_rule.

New in 2.0.

Parameters:

Name Type Description Default
blueprint Blueprint

the Blueprint to attach

required
rule_ Union[Type[RuleBase], str, Pattern]

rule as per nserver.rules.smart_make_rule

required
args

extra arguments to provide smart_make_rule

()
kwargs

extra keyword arguments to provide smart_make_rule

{}

Raises:

Type Description
ValueError

if func is provided in kwargs.

register_exception_handler 🔗

register_exception_handler(
    exception_class: Type[Exception],
    handler: middleware.ExceptionHandler,
) -> None

Register an exception handler for the QueryMiddleware

Only one handler can exist for a given exception type.

New in 2.0.

Parameters:

Name Type Description Default
exception_class Type[Exception]

the type of exception to handle

required
handler ExceptionHandler

the function to call when handling an exception

required

register_middleware 🔗

register_middleware(
    query_middleware: middleware.QueryMiddleware,
) -> None

Add a QueryMiddleware to this server.

New in 2.0.

Parameters:

Name Type Description Default
query_middleware QueryMiddleware

the middleware to add

required

register_rule 🔗

register_rule(rule: RuleBase) -> None

Register the given rule

Parameters:

Name Type Description Default
rule RuleBase

the rule to register

required

rule 🔗

rule(
    rule_: Union[Type[RuleBase], str, Pattern],
    *args,
    **kwargs
)

Decorator for registering a function using smart_make_rule.

Changed in 2.0: This method now uses smart_make_rule.

Parameters:

Name Type Description Default
rule_ Union[Type[RuleBase], str, Pattern]

rule as per nserver.rules.smart_make_rule

required
args

extra arguments to provide smart_make_rule

()
kwargs

extra keyword arguments to provide smart_make_rule

{}

Raises:

Type Description
ValueError

if func is provided in kwargs.

NameServer 🔗

NameServer(name: str, settings: Optional[Settings] = None)

Bases: Scaffold

NameServer for responding to requests.

Parameters:

Name Type Description Default
name str

The name of the server. This is used for internal logging.

required
settings Optional[Settings]

settings to use with this NameServer instance

None

after_query 🔗

after_query()

Decorator for registering after_query hook.

These functions are after the rule function is run and may modify the response.

before_first_query 🔗

before_first_query()

Decorator for registering before_first_query hook.

These functions are called when the server receives it's first query, but before any further processesing.

before_query 🔗

before_query()

Decorator for registering before_query hook.

These functions are called before processing each query.

exception_handler 🔗

exception_handler(exception_class: Type[Exception])

Decorator for registering a function as an exception handler

New in 2.0.

Parameters:

Name Type Description Default
exception_class Type[Exception]

The Exception class to register this handler for

required

raw_exception_handler 🔗

raw_exception_handler(exception_class: Type[Exception])

Decorator for registering a function as an raw exception handler

New in 2.0.

Parameters:

Name Type Description Default
exception_class Type[Exception]

The Exception class to register this handler for

required

register_after_query 🔗

register_after_query(
    func: middleware.AfterQueryHook,
) -> None

Register a function to be run on the result of a query.

Parameters:

Name Type Description Default
func AfterQueryHook

the function to register

required

register_before_first_query 🔗

register_before_first_query(
    func: middleware.BeforeFirstQueryHook,
) -> None

Register a function to be run before the first query.

Parameters:

Name Type Description Default
func BeforeFirstQueryHook

the function to register

required

register_before_query 🔗

register_before_query(
    func: middleware.BeforeQueryHook,
) -> None

Register a function to be run before every query.

Parameters:

Name Type Description Default
func BeforeQueryHook

the function to register If func returns anything other than None will stop processing the incoming Query and continue to result processing with the return value.

required

register_blueprint 🔗

register_blueprint(
    blueprint: Blueprint,
    rule_: Union[Type[RuleBase], str, Pattern],
    *args,
    **kwargs
) -> None

Register a blueprint using smart_make_rule.

New in 2.0.

Parameters:

Name Type Description Default
blueprint Blueprint

the Blueprint to attach

required
rule_ Union[Type[RuleBase], str, Pattern]

rule as per nserver.rules.smart_make_rule

required
args

extra arguments to provide smart_make_rule

()
kwargs

extra keyword arguments to provide smart_make_rule

{}

Raises:

Type Description
ValueError

if func is provided in kwargs.

register_exception_handler 🔗

register_exception_handler(
    exception_class: Type[Exception],
    handler: middleware.ExceptionHandler,
) -> None

Register an exception handler for the QueryMiddleware

Only one handler can exist for a given exception type.

New in 2.0.

Parameters:

Name Type Description Default
exception_class Type[Exception]

the type of exception to handle

required
handler ExceptionHandler

the function to call when handling an exception

required

register_middleware 🔗

register_middleware(
    query_middleware: middleware.QueryMiddleware,
) -> None

Add a QueryMiddleware to this server.

New in 2.0.

Parameters:

Name Type Description Default
query_middleware QueryMiddleware

the middleware to add

required

register_raw_exception_handler 🔗

register_raw_exception_handler(
    exception_class: Type[Exception],
    handler: middleware.RawRecordExceptionHandler,
) -> None

Register a raw exception handler for the RawRecordMiddleware.

Only one handler can exist for a given exception type.

New in 2.0.

Parameters:

Name Type Description Default
exception_class Type[Exception]

the type of exception to handle

required
handler RawRecordExceptionHandler

the function to call when handling an exception

required

register_raw_middleware 🔗

register_raw_middleware(
    raw_middleware: middleware.RawRecordMiddleware,
) -> None

Add a RawRecordMiddleware to this server.

New in 2.0.

Parameters:

Name Type Description Default
raw_middleware RawRecordMiddleware

the middleware to add

required

register_rule 🔗

register_rule(rule: RuleBase) -> None

Register the given rule

Parameters:

Name Type Description Default
rule RuleBase

the rule to register

required

rule 🔗

rule(
    rule_: Union[Type[RuleBase], str, Pattern],
    *args,
    **kwargs
)

Decorator for registering a function using smart_make_rule.

Changed in 2.0: This method now uses smart_make_rule.

Parameters:

Name Type Description Default
rule_ Union[Type[RuleBase], str, Pattern]

rule as per nserver.rules.smart_make_rule

required
args

extra arguments to provide smart_make_rule

()
kwargs

extra keyword arguments to provide smart_make_rule

{}

Raises:

Type Description
ValueError

if func is provided in kwargs.

run 🔗

run() -> int

Start running the server

Returns:

Type Description
int

exit_code, 0 if exited normally

Scaffold 🔗

Scaffold(name: str)

Base class for shared functionality between NameServer and Blueprint

New in 2.0.

Attributes:

Name Type Description
rules List[RuleBase]

registered rules

hook_middleware

hook middleware

exception_handler_middleware

Query exception handler middleware

Parameters:

Name Type Description Default
name str

The name of the server. This is used for internal logging.

required

after_query 🔗

after_query()

Decorator for registering after_query hook.

These functions are after the rule function is run and may modify the response.

before_first_query 🔗

before_first_query()

Decorator for registering before_first_query hook.

These functions are called when the server receives it's first query, but before any further processesing.

before_query 🔗

before_query()

Decorator for registering before_query hook.

These functions are called before processing each query.

exception_handler 🔗

exception_handler(exception_class: Type[Exception])

Decorator for registering a function as an exception handler

New in 2.0.

Parameters:

Name Type Description Default
exception_class Type[Exception]

The Exception class to register this handler for

required

register_after_query 🔗

register_after_query(
    func: middleware.AfterQueryHook,
) -> None

Register a function to be run on the result of a query.

Parameters:

Name Type Description Default
func AfterQueryHook

the function to register

required

register_before_first_query 🔗

register_before_first_query(
    func: middleware.BeforeFirstQueryHook,
) -> None

Register a function to be run before the first query.

Parameters:

Name Type Description Default
func BeforeFirstQueryHook

the function to register

required

register_before_query 🔗

register_before_query(
    func: middleware.BeforeQueryHook,
) -> None

Register a function to be run before every query.

Parameters:

Name Type Description Default
func BeforeQueryHook

the function to register If func returns anything other than None will stop processing the incoming Query and continue to result processing with the return value.

required

register_blueprint 🔗

register_blueprint(
    blueprint: Blueprint,
    rule_: Union[Type[RuleBase], str, Pattern],
    *args,
    **kwargs
) -> None

Register a blueprint using smart_make_rule.

New in 2.0.

Parameters:

Name Type Description Default
blueprint Blueprint

the Blueprint to attach

required
rule_ Union[Type[RuleBase], str, Pattern]

rule as per nserver.rules.smart_make_rule

required
args

extra arguments to provide smart_make_rule

()
kwargs

extra keyword arguments to provide smart_make_rule

{}

Raises:

Type Description
ValueError

if func is provided in kwargs.

register_exception_handler 🔗

register_exception_handler(
    exception_class: Type[Exception],
    handler: middleware.ExceptionHandler,
) -> None

Register an exception handler for the QueryMiddleware

Only one handler can exist for a given exception type.

New in 2.0.

Parameters:

Name Type Description Default
exception_class Type[Exception]

the type of exception to handle

required
handler ExceptionHandler

the function to call when handling an exception

required

register_middleware 🔗

register_middleware(
    query_middleware: middleware.QueryMiddleware,
) -> None

Add a QueryMiddleware to this server.

New in 2.0.

Parameters:

Name Type Description Default
query_middleware QueryMiddleware

the middleware to add

required

register_rule 🔗

register_rule(rule: RuleBase) -> None

Register the given rule

Parameters:

Name Type Description Default
rule RuleBase

the rule to register

required

rule 🔗

rule(
    rule_: Union[Type[RuleBase], str, Pattern],
    *args,
    **kwargs
)

Decorator for registering a function using smart_make_rule.

Changed in 2.0: This method now uses smart_make_rule.

Parameters:

Name Type Description Default
rule_ Union[Type[RuleBase], str, Pattern]

rule as per nserver.rules.smart_make_rule

required
args

extra arguments to provide smart_make_rule

()
kwargs

extra keyword arguments to provide smart_make_rule

{}

Raises:

Type Description
ValueError

if func is provided in kwargs.