Made get-peer example more readable and updated readme

This commit is contained in:
Mahesh Bansod 2021-11-03 12:54:59 +05:30
parent acf8eadf47
commit 59847a5032
2 changed files with 18 additions and 23 deletions

View File

@ -8,13 +8,18 @@ This repository contains
//TODO: link to this crate's documentation once it's up. //TODO: link to this crate's documentation once it's up.
Most of the methods are `async`
Methods can be accessed as `<namespace>.<methodname>`
For example, to invoke the `authenticate_user` method in `session` namespace, you can use `sv.session.authenticate_user(...)`.
Incase any method isn't available in this crate, you can make raw requests using the `socialvoid-rawclient` crate, the latest full API Documentation of Socialvoid is [here](https://github.com/intellivoid/Socialvoid-Standard-Documentation) Incase any method isn't available in this crate, you can make raw requests using the `socialvoid-rawclient` crate, the latest full API Documentation of Socialvoid is [here](https://github.com/intellivoid/Socialvoid-Standard-Documentation)
## Example for the library ## Example for the library
A simple example that logs you in and shows peer information. A simple example that logs you in and shows peer information.
```rust ```rust
// You need to make a file called `test_creds.test` in the root of the project for this example to run // You need to make a file called `test_creds.test` in the root of the project for
// this example to run.
// The file is a JSON file with the following format // The file is a JSON file with the following format
// { // {
// "username":"yourusername", // "username":"yourusername",
@ -27,25 +32,20 @@ async fn main() {
serde_json::from_str(&std::fs::read_to_string("test_creds.test").unwrap()) serde_json::from_str(&std::fs::read_to_string("test_creds.test").unwrap())
.expect("Couldn't read the credentials. Check the JSON format or something"); .expect("Couldn't read the credentials. Check the JSON format or something");
let mut client = socialvoid::new_with_defaults().await.unwrap(); let sv = socialvoid::new_with_defaults().await.unwrap();
client let username = creds["username"].as_str().unwrap().to_string();
.authenticate_user( let password = creds["password"].as_str().unwrap().to_string();
creds["username"].as_str().unwrap().to_string(), sv.session
creds["password"].as_str().unwrap().to_string(), .authenticate_user(username, password, None)
None,
)
.await .await
.unwrap(); .unwrap();
let peer = client.get_me().await.unwrap(); let peer = sv.network.get_me().await.unwrap();
println!("{:?}", peer); println!("{:?}", peer);
client.logout().await.unwrap(); sv.session.logout().await.unwrap();
assert_eq!( assert_eq!(peer.username, username);
peer.username,
creds["username"].as_str().unwrap().to_string()
);
} }
``` ```

View File

@ -14,12 +14,10 @@ async fn main() {
.expect("Couldn't read the credentials. Check the JSON format or something"); .expect("Couldn't read the credentials. Check the JSON format or something");
let sv = socialvoid::new_with_defaults().await.unwrap(); let sv = socialvoid::new_with_defaults().await.unwrap();
let username = creds["username"].as_str().unwrap().to_string();
let password = creds["password"].as_str().unwrap().to_string();
sv.session sv.session
.authenticate_user( .authenticate_user(username, password, None)
creds["username"].as_str().unwrap().to_string(),
creds["password"].as_str().unwrap().to_string(),
None,
)
.await .await
.unwrap(); .unwrap();
@ -28,8 +26,5 @@ async fn main() {
println!("{:?}", peer); println!("{:?}", peer);
sv.session.logout().await.unwrap(); sv.session.logout().await.unwrap();
assert_eq!( assert_eq!(peer.username, username);
peer.username,
creds["username"].as_str().unwrap().to_string()
);
} }