-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathbasic.rs
More file actions
55 lines (47 loc) · 1.71 KB
/
Copy pathbasic.rs
File metadata and controls
55 lines (47 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//! Basic example: create an agent, a drive, a resource, and edit it.
//!
//! Requires AtomicServer running at localhost:9883.
//!
//! Run with: cargo run --example basic -p atomic_lib
use atomic_lib::client::connected::Client;
use atomic_lib::errors::AtomicResult;
#[tokio::main(flavor = "current_thread")]
async fn main() -> AtomicResult<()> {
let client = Client::new("http://localhost:9883").await?;
let agent = client.new_agent("Alice").await?;
println!("Agent: {}", agent.subject);
let drive = client.new_drive(&agent, "Alice's Drive").await?;
println!("Drive: {}", drive);
let mut resource = client.new_resource(&drive)?;
resource.set_name("My first resource")?;
resource.set_unsafe(
"https://atomicdata.dev/properties/description".into(),
atomic_lib::Value::String("Created with atomic_lib".into()),
)?;
resource.set_unsafe(
atomic_lib::urls::IS_A.into(),
atomic_lib::Value::ResourceArray(vec![atomic_lib::urls::CLASS.into()]),
)?;
resource.set_unsafe(
atomic_lib::urls::SHORTNAME.into(),
atomic_lib::Value::Slug("my-resource".into()),
)?;
let subject = resource.save_remote(client.store()).await?;
println!("Created: {}", subject);
println!(" name: {}", resource.get_name().unwrap_or_default());
// Edit
resource.set_name("Updated name")?;
resource.save_remote(client.store()).await?;
println!(
" name (after edit): {}",
resource.get_name().unwrap_or_default()
);
// Fetch from server
let fetched = client.get_resource(&subject).await?;
println!(
" name (fetched): {}",
fetched.get_name().unwrap_or_default()
);
println!("Done!");
Ok(())
}