Skip to main content

Host

Trait Host 

Source
pub trait Host: Send + Sync {
    // Required methods
    fn read_file<'life0, 'life1, 'async_trait>(
        &'life0 self,
        path: &'life1 Path,
    ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn http_get<'life0, 'life1, 'async_trait>(
        &'life0 self,
        url: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn http_post_json<'life0, 'life1, 'life2, 'life3, 'async_trait>(
        &'life0 self,
        url: &'life1 str,
        headers: &'life2 BTreeMap<String, String>,
        body: &'life3 str,
    ) -> Pin<Box<dyn Future<Output = Result<HttpResponse>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait;
    fn env(&self, name: &str) -> Option<String>;
}
Expand description

What an extension can ask the host to do.

Required Methods§

Source

fn read_file<'life0, 'life1, 'async_trait>( &'life0 self, path: &'life1 Path, ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Read a file, already resolved and checked against the working directory.

§Errors

yk_core::Error::Read when the file cannot be read.

Source

fn http_get<'life0, 'life1, 'async_trait>( &'life0 self, url: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Fetch a URL as text.

§Errors

yk_core::Error::Provider when the request fails.

Source

fn http_post_json<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, url: &'life1 str, headers: &'life2 BTreeMap<String, String>, body: &'life3 str, ) -> Pin<Box<dyn Future<Output = Result<HttpResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

POST a JSON document and read the reply.

The first write reach an extension has, and the reason it is here: http_get can read a URL and nothing else, which is enough for a document and not enough for any hosted API, because they authenticate with a request header and answer on POST.

headers are the caller’s, so the caller chooses the credentials and the body chooses the rest. The host sets Content-Type: application/json first and lets headers override it, because the name promises JSON and a caller talking to something else should be able to say so.

§Errors

yk_core::Error::Provider when the request fails or a header is one the HTTP client refuses. A response with a non-2xx status is not an error here.

Source

fn env(&self, name: &str) -> Option<String>

Read one environment variable by name, or None when it is unset.

os.getenv does not exist in this VM, so without this an extension cannot read a key at all, which puts every hosted API out of reach for a script that has no filesystem and no process to inherit one from.

One name at a time rather than the whole table. A script that can dump the environment in one call is a much better exfiltration story than one that has to ask for each value it wants.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§