reorg demo adater code

This commit is contained in:
boneyard93501
2021-10-20 12:29:05 -05:00
parent f668c13074
commit bb665ade12
5 changed files with 242 additions and 0 deletions

View File

@ -0,0 +1,5 @@
debug/
target/
Cargo.lock
**/*.bk
**/*.bak

View File

@ -0,0 +1,22 @@
[package]
name = "ceramic"
version = "0.1.0"
authors = ["boneyard93501 <4523011+boneyard93501@users.noreply.github.com>"]
edition = "2018"
description = "ceramic-adapter"
license = "Apache-2.0"
[[bin]]
name = "ceramic_adapter"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = { version = "0.6.11", features = ["logger"] }
log = "0.4.14"
[dev-dependencies]
marine-rs-sdk-test = "0.1.11"
[dev]
[profile.release]
opt-level = "s"

View File

@ -0,0 +1,108 @@
use marine_rs_sdk::{marine, MountedBinaryResult};
#[marine]
pub struct CeramicResult {
pub ret_code: i32,
pub stderr: String,
pub stdout: String,
}
impl CeramicResult {
fn new(mb: MountedBinaryResult) -> Self {
CeramicResult {
ret_code: mb.ret_code,
stderr: String::from_utf8(mb.stderr).unwrap(),
stdout: String::from_utf8(mb.stdout).unwrap(),
}
}
fn create(ret_code: i32, stdout: String, stderr: String) -> Self {
CeramicResult {
ret_code,
stderr,
stdout,
}
}
}
#[marine]
// general purpose function where all args need to be provided
// e.g., ["ceramic", "state", "stream_id"]
pub fn ceramic_request(args: Vec<String>) -> CeramicResult {
let response: MountedBinaryResult = ceramic(args);
CeramicResult::new(response)
}
#[marine]
// https://developers.ceramic.network/build/cli/quick-start/#2-create-a-stream
pub fn create_stream(payload: String) -> CeramicResult {
let args = vec![
"create".to_string(),
"tile".to_string(),
"--content".to_string(),
payload,
];
let response: MountedBinaryResult = ceramic(args);
if response.stderr.len() > 0 {
return CeramicResult::new(response);
}
let stdout_str: String = String::from_utf8(response.stdout).unwrap();
if stdout_str.contains("StreamID") {
let res: Vec<&str> = stdout_str.split("\n").collect();
let stream_id = res[0].replace("StreamID(", "").replace(")", "");
return CeramicResult::create(response.ret_code, stream_id.to_string(), "".to_string());
} else {
return CeramicResult::create(
response.ret_code,
"Missing StreamId".to_string(),
"".to_string(),
);
}
}
#[marine]
// https://developers.ceramic.network/build/cli/quick-start/#3-query-a-stream
pub fn show(stream_id: String) -> CeramicResult {
let response: MountedBinaryResult = ceramic(vec!["show".to_string(), stream_id]);
CeramicResult::new(response)
}
#[marine]
// https://developers.ceramic.network/build/cli/quick-start/#7-query-the-stream-you-created
pub fn state(stream_id: String) -> CeramicResult {
let response: MountedBinaryResult = ceramic(vec!["state".to_string(), stream_id]);
CeramicResult::new(response)
}
#[marine]
// https://developers.ceramic.network/build/cli/quick-start/#4-update-a-stream
pub fn update(stream_id: String, payload: String) -> CeramicResult {
let response: MountedBinaryResult = ceramic(vec![
"update".to_string(),
stream_id,
"--content".to_string(),
payload,
]);
CeramicResult::new(response)
}
#[marine]
// https://developers.ceramic.network/build/cli/quick-start/#5-create-a-schema
pub fn create_schema(schema: String) -> CeramicResult {
let args = vec![
"create".to_string(),
"tile".to_string(),
"--content".to_string(),
schema,
];
let response: MountedBinaryResult = ceramic(args);
CeramicResult::new(response)
}
// mounted_binaries are available to import like this:
#[marine]
#[link(wasm_import_module = "host")]
extern "C" {
pub fn ceramic(cmd: Vec<String>) -> MountedBinaryResult;
}

View File

@ -0,0 +1,81 @@
use marine_rs_sdk::{marine, MountedBinaryResult};
// source: https://developers.ceramic.network/build/http/api/
static API: &str = "api";
static VERSION: &str = "v0";
static STREAM: &str = "streams";
fn url_maker(host: String, port: u32, arg: String, stream_id: Option<String>) -> String {
let curl_args = format!(
"http://{}:{}/{}/{}/{}",
host,
port,
API,
VERSION,
arg.to_uppercase(),
);
match stream_id {
Some(sid) => format!("{}/{}", curl_args, sid),
None => curl_args,
}
}
#[marine]
pub fn http_streams(url: String, port: u32, stream_id: String) -> String {
// curl http://localhost:7007/api/v0/streams/kjzl6cwe1jw147r7878h32yazawcll6bxe5v92348cxitif6cota91qp68grbhm
let url = url_maker(url, port, STREAM.to_string(), Some(stream_id));
let cmd = vec![url, "GET".to_string()];
let response: MountedBinaryResult = curl_request(cmd);
String::from_utf8(response.stdout).unwrap()
}
#[marine]
pub fn http_chain_id(url: String, port: u32) -> String {
let url = url_maker(url, port, "NODE/CHAINS".to_string(), None);
let cmd = vec![url, "GET".to_string()];
println!("cmd: {:?}", cmd);
let response: MountedBinaryResult = curl_request(cmd);
String::from_utf8(response.stdout).unwrap()
}
#[marine]
pub fn http_health(url: String, port: u32) -> String {
let url = url_maker(url, port, "NODE/HEALTHCHECK".to_string(), None);
let cmd = vec![url, "GET".to_string()];
println!("cmd: {:?}", cmd);
let response: MountedBinaryResult = curl_request(cmd);
String::from_utf8(response.stdout).unwrap()
}
#[marine]
pub fn http_pins(url: String, port: u32) -> String {
let url = url_maker(url, port, "PINS".to_string(), None);
let cmd = vec![url, "GET".to_string()];
let response: MountedBinaryResult = curl_request(cmd);
String::from_utf8(response.stdout).unwrap()
}
#[marine]
pub fn http_pin(url: String, port: u32, stream_id: String) -> String {
let url = url_maker(url, port, "PINS".to_string(), Some(stream_id));
let cmd = vec![url, "GET".to_string()];
println!("cmd: {:?}", cmd);
let response: MountedBinaryResult = curl_request(cmd);
String::from_utf8(response.stdout).unwrap()
}
#[marine]
pub fn http_rm_pin(url: String, port: u32, stream_id: String) -> String {
// not available in gateway mode: https://developers.ceramic.network/build/http/api/#remove-from-pinset
let url = url_maker(url, port, "PINS".to_string(), Some(stream_id));
let cmd = vec![url, "-X DELETE".to_string()];
println!("cmd: {:?}", cmd);
let response: MountedBinaryResult = curl_request(cmd);
String::from_utf8(response.stdout).unwrap()
}
#[marine]
#[link(wasm_import_module = "curl_adapter")]
extern "C" {
pub fn curl_request(cmd: Vec<String>) -> MountedBinaryResult;
}

View File

@ -0,0 +1,26 @@
/*
* Copyright 2021 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use marine_rs_sdk::{module_manifest, WasmLoggerBuilder};
module_manifest!();
pub mod ceramic_cli;
pub mod ceramic_http;
pub fn main() {
WasmLoggerBuilder::new().build().ok();
}