Struct nx_request_handler::RequestEngine
source · pub struct RequestEngine { /* private fields */ }
Expand description
An engine for streamlining the handling of backend requests by skyline-web
applications.
Implementations§
source§impl RequestEngine
impl RequestEngine
sourcepub fn new(session: WebSession) -> Self
pub fn new(session: WebSession) -> Self
Creates a new RequestEngine, taking ownership of the session in the process.
sourcepub fn register<S: ToString>(
&mut self,
request_name: S,
arg_count: Option<usize>,
handler: impl Fn(&mut MessageContext<'_>) -> Result<String, String> + 'static
) -> &mut Self
pub fn register<S: ToString>(
&mut self,
request_name: S,
arg_count: Option<usize>,
handler: impl Fn(&mut MessageContext<'_>) -> Result<String, String> + 'static
) -> &mut Self
Registers a handler for requests with the given name.
Arguments
request_name
- the name of the request to listen forarg_count
- an optional number of arguments to expect. IfNone
is supplied, this argument does nothing. IfSome
is supplied, then the engine will validate that the inbound request has the required arguments present before calling the registered handler. If the argument count is incorrect, the handler will not be called and an error will be returned to the frontend instead.handler
- this is a closure or function, which takes aMessageContext
and must returnResult<String, String>
. The returned value (Ok
orErr
) is then sent to the frontend as anaccept()
orreject()
on the originalPromise
. Note that the returned string can be populated with JSON data. Such JSON can then be used in the frontend viaJSON.parse()
to retreive complex structures.
Example:
engine.register("my_call_name", Some(3), |context| {
let args = context.arguments.unwrap();
return Ok(format!("args: {}, {}, {}", args[0], args[1], args[2]));
})
sourcepub fn register_defaults(&mut self) -> &mut Self
pub fn register_defaults(&mut self) -> &mut Self
Registers the “default” handlers for some common functionality.
This aligns with the nx-request-api
NPM package’s DefaultMessenger.
Default calls:
ping
- returns ok if the backend responded to the request
read_file
- returns the file’s contents as a string
download_file
- downloads the given file to the given location
delete_file
- deletes the given file
write_file
- writes the given string to the given file location
get_md5
- returns the md5 checksum of the given file
unzip
- unzips the given file as to the given location
file_exists
- returns whether the given path exists and is a file
dir_exists
- returns whether the given path exists and is a directory
list_all_files
- returns a tree structure of the given directory, recursively
list_dir
- returns a list of the files and directories in the given path (non recursive)
get_request
- performs a GET request (using
smashnet
) and returns the body as a string
- performs a GET request (using
exit_session
- signals the engine to shutdown and the session to close, unblocking
start()
- signals the engine to shutdown and the session to close, unblocking
exit_application
- closes the application entirely (you will return to the home menu)