Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New API changes #1

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use crate::process::*;
use crate::project::*;
use crate::template::*;

const DEFAULT_BASE_URL: &str = "https://editor.netsblox.org";
const DEFAULT_BASE_URL: &str = "https://cloud.netsblox.org";
const STEPS_PER_IO_ITER: usize = 64;
const MAX_REQUEST_SIZE_BYTES: usize = 1024 * 1024 * 1024;
const YIELDS_BEFORE_IDLE_SLEEP: usize = 256;
Expand Down
33 changes: 18 additions & 15 deletions src/std_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
struct Context {
base_url: String,
client_id: String,
services_url: String,

project_name: String,
project_id: String,
Expand Down Expand Up @@ -75,7 +76,7 @@
/// A value of [`Ok`] denotes a successful request, whose value will be returned to the system
/// after conversion under [`CustomTypes::from_intermediate`].
/// A value of [`Err`] denotes a failed request, which will be returned as an error to the runtime,
/// subject to the caller's [`ErrorScheme`](crate::runtime::ErrorScheme) setting.

Check failure on line 79 in src/std_system.rs

View workflow job for this annotation

GitHub Actions / doc

redundant explicit link target
fn complete(self, value: Result<C::Intermediate, String>) {
assert!(self.0.lock().unwrap().complete(value).is_ok())
}
Expand All @@ -90,7 +91,7 @@
/// Completes the command.
/// A value of [`Ok`] denotes a successful command.
/// A value of [`Err`] denotes a failed command, which will be returned as an error to the runtime,
/// subject to the caller's [`ErrorScheme`](crate::runtime::ErrorScheme) setting.

Check failure on line 94 in src/std_system.rs

View workflow job for this annotation

GitHub Actions / doc

redundant explicit link target
fn complete(self, value: Result<(), String>) {
assert!(self.0.lock().unwrap().complete(value).is_ok())
}
Expand All @@ -100,8 +101,8 @@

async fn call_rpc_async<C: CustomTypes<StdSystem<C>>>(context: &Context, client: &reqwest::Client, service: &str, rpc: &str, args: &[(&str, &Json)]) -> Result<C::Intermediate, String> {
let time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis();
let url = format!("{base_url}/services/{service}/{rpc}?uuid={client_id}&projectId={project_id}&roleId={role_id}&t={time}",
base_url = context.base_url, client_id = context.client_id, project_id = context.project_id, role_id = context.role_id);
let url = format!("{services_url}/{service}/{rpc}?clientId={client_id}&t={time}",
services_url = context.services_url, client_id = context.client_id);
let args: BTreeMap<&str, &Json> = args.iter().copied().collect();

let res = match client.post(url).json(&args).send().await {
Expand Down Expand Up @@ -156,11 +157,15 @@
pub async fn new_sync(base_url: String, project_name: Option<&str>, config: Config<C, Self>, utc_offset: UtcOffset) -> Self {
Self::new_async(base_url, project_name, config, utc_offset).await
}
/// Initializes a new instance of [`StdSystem`] targeting the given NetsBlox server base url (e.g., `https://editor.netsblox.org`).
/// Initializes a new instance of [`StdSystem`] targeting the given NetsBlox server base url (e.g., `https://cloud.netsblox.org`).
pub async fn new_async(base_url: String, project_name: Option<&str>, config: Config<C, Self>, utc_offset: UtcOffset) -> Self {
let configuration = reqwest::get(format!("{}/configuration", base_url)).await.unwrap().json::<BTreeMap<String, Json>>().await.unwrap();
let services_hosts = configuration["servicesHosts"].as_array().unwrap();

let mut context = Context {
base_url,
client_id: format!("vm-{}", names::Generator::default().next().unwrap()),
services_url: services_hosts[0].as_object().unwrap().get("url").unwrap().as_str().unwrap().to_owned(),
client_id: configuration["clientId"].as_str().unwrap().to_owned(),
project_name: project_name.unwrap_or("untitled").to_owned(),

project_id: String::new(),
Expand All @@ -176,7 +181,7 @@

#[tokio::main(flavor = "multi_thread", worker_threads = 1)]
async fn handler<C: CustomTypes<StdSystem<C>>>(base_url: String, client_id: String, project_name: String, message_replies: Arc<Mutex<MessageReplies>>, out_receiver: Receiver<OutgoingMessage<C, StdSystem<C>>>, in_sender: Sender<IncomingMessage<C, StdSystem<C>>>) {
let ws_url = if let Some(x) = base_url.strip_prefix("http") { format!("ws{}", x) } else { format!("wss://{}", base_url) };
let ws_url = format!("{}/network/{}/connect", if let Some(x) = base_url.strip_prefix("http") { format!("ws{}", x) } else { format!("wss://{}", base_url) }, client_id);
let (ws, _) = tokio_tungstenite::connect_async(ws_url).await.unwrap();
let (mut ws_sender, ws_receiver) = ws.split();
let (ws_sender_sender, ws_sender_receiver) = async_channel::unbounded();
Expand Down Expand Up @@ -272,19 +277,17 @@
};

let client = Arc::new(reqwest::Client::builder().build().unwrap());
let meta = client.post(format!("{}/api/newProject", context.base_url))
.json(&json!({ "clientId": context.client_id, "roleName": "monad" }))
let meta = client.post(format!("{}/projects/", context.base_url))
.json(&json!({ "clientId": context.client_id, "name": context.project_name }))
.send().await.unwrap()
.json::<BTreeMap<String, Json>>().await.unwrap();
context.project_id = meta["projectId"].as_str().unwrap().to_owned();
context.role_id = meta["roleId"].as_str().unwrap().to_owned();
context.role_name = meta["roleName"].as_str().unwrap().to_owned();
context.project_id = meta["id"].as_str().unwrap().to_owned();

let meta = client.post(format!("{}/api/setProjectName", context.base_url))
.json(&json!({ "projectId": context.project_id, "name": context.project_name }))
.send().await.unwrap()
.json::<BTreeMap<String, Json>>().await.unwrap();
context.project_name = meta["name"].as_str().unwrap().to_owned();
let roles = &meta["roles"].as_object().unwrap();
let (first_role_id, first_role_meta) = roles.get_key_value(roles.keys().next().unwrap()).unwrap();
let first_role_meta = first_role_meta.as_object().unwrap();
context.role_id = first_role_id.to_owned();
context.role_name = first_role_meta.get("name").unwrap().as_str().unwrap().to_owned();

let context = Arc::new(context);
let rpc_request_pipe = {
Expand Down
2 changes: 1 addition & 1 deletion src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::gc::*;
mod process;
mod project;

const BASE_URL: &'static str = "https://editor.netsblox.org";
const BASE_URL: &'static str = "https://cloud.netsblox.org";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum NativeType {}
Expand Down
Loading