Curl is a widely available and used command-line tool to receive or send data using URL syntax. Chances are, you probably just used it when you set up your Fluence development environment. For Fluence services to be able to interact with the world, cUrl is one option to facilitate https calls. Since Fluence modules are Wasm IT modules, cUrl cannot not be a service intrinsic. Instead, the curl command-line tool needs to be made available and accessible at the node level. And for Fluence services to be able to interact with Curl, we need to code a cUrl adapter taking care of the mounted (cUrl) binary.
Adapter Construction
The work for the cUrl adapter has been fundamentally done and is exposed by the Fluence Rust SDK. As a developer, the task remaining is to instantiate the adapter in the context of the module and services scope. The following code snippet illustrates the implementation requirement. It is a part of a larger service, url-downloader.
use marine_rs_sdk::marine;
use marine_rs_sdk::WasmLoggerBuilder;
use marine_rs_sdk::MountedBinaryResult;
pub fn main() {
WasmLoggerBuilder::new().build().unwrap();
}
#[marine]
pub fn download(url: String) -> String {
log::info!("download called with url {}", url);
let result = unsafe { curl(vec![url]) };
String::from_utf8(result.stdout).unwrap()
}
#[marine]
#[link(wasm_import_module = "host")]
extern "C" {
fn curl(cmd: Vec<String>) -> MountedBinaryResult;
}
with the following dependencies necessary in the Cargo.toml:
marine-rs-sdk = { version = "=0.6.11", features = ["logger"] }
log = "0.4.8"
We are basically linking the external cUrl binary and are exposing access to it as a marine interface called download.
We are specifying the location of the Wasm file, the import name of the Wasm file, some logging housekeeping, and the mounted binary reference with the command-line call information.
Remote Service Creation
cargo new curl_adapter
cd curl_adapter
# copy the above rust code into src/main
# copy the specified dependencies into Cargo.toml
# copy the above service configuration into Config.toml
marine build --release
You should have the Fluence module cur-service.wasm in target/wasm32-wasi/release . We can test our service with mrepl.
Service Testing
Running the REPL, we use the interface command to list all available interfaces and the call command to run a method. For our purposes, we furnish the https://duckduckgo.com/?q=Fluence+Labs url to give the the curl adapter a workout.