added get peer example + updated readme with it

This commit is contained in:
Mahesh Bansod 2021-10-23 23:39:26 +05:30
parent 19ef4c4d03
commit 59d9532766
2 changed files with 65 additions and 17 deletions

View File

@ -14,29 +14,42 @@ Incase any method isn't available in this crate, you can make raw requests using
A simple example that logs you in and shows peer information.
```rust
use socialvoid::Client;
// 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
// {
// "username":"yourusername",
// "password":"yourpassword"
// }
let mut client = new_with_defaults();
#[tokio::main]
async fn main() {
let creds: serde_json::Value =
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");
client
.authenticate_user(
creds["username"].as_str().unwrap().to_string(),
creds["password"].as_str().unwrap().to_string(),
None,
)
.await?;
let mut client = socialvoid::new_with_defaults().await.unwrap();
client
.authenticate_user(
creds["username"].as_str().unwrap().to_string(),
creds["password"].as_str().unwrap().to_string(),
None,
)
.await
.unwrap();
let peer = client.get_me().await?;
client.logout().await?;
let peer = client.get_me().await.unwrap();
println!("{:?}", peer);
assert_eq!(
peer.username,
creds["username"].as_str().unwrap().to_string()
);
println!("{:?}", peer);
client.logout().await.unwrap();
assert_eq!(
peer.username,
creds["username"].as_str().unwrap().to_string()
);
}
```
More examples can be found in client/examples (TODO+add a link)
More examples can be found in [client/examples](https://github.com/intellivoid/socialvoid-rs/tree/master/client/examples)
## Installation of the CLI client
1. Clone this repository

View File

@ -0,0 +1,35 @@
// You need to make a file called `test_creds.test` in the root of the project for
// this example to run. Though, it makes more sense to work using a session instead of saving
// the password in plaintext in a file in real applications
// The file is a JSON file with the following format
// {
// "username":"yourusername",
// "password":"yourpassword"
// }
#[tokio::main]
async fn main() {
let creds: serde_json::Value =
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");
let mut client = socialvoid::new_with_defaults().await.unwrap();
client
.authenticate_user(
creds["username"].as_str().unwrap().to_string(),
creds["password"].as_str().unwrap().to_string(),
None,
)
.await
.unwrap();
let peer = client.get_me().await.unwrap();
println!("{:?}", peer);
client.logout().await.unwrap();
assert_eq!(
peer.username,
creds["username"].as_str().unwrap().to_string()
);
}