Skip to content

Commit e777354

Browse files
committed
feat: put client behind a feature gate
fix: some client API arguments fix: `all` test
1 parent b3c69da commit e777354

File tree

8 files changed

+51
-52
lines changed

8 files changed

+51
-52
lines changed

Cargo.toml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@ license = "MIT"
99
prost = "0.6"
1010
prost-types = "0.6"
1111
tonic = "0.3"
12-
log = "0.4"
13-
http = "0.2"
14-
assign = "1.1"
15-
paste = "1.0"
12+
log = { version = "0.4", optional = true }
13+
http = { version = "0.2", optional = true }
14+
assign = { version = "1.1", optional = true }
15+
paste = { version = "1.0", optional = true }
1616
parking_lot = { version = "0.11", optional = true }
17-
futures = "0.3"
17+
futures = { version = "0.3", optional = true }
1818

1919
[build-dependencies]
2020
tonic-build = "0.3"
2121

2222
[features]
23-
deafult = [ ]
23+
default = [ "client" ]
24+
client = [ "futures", "paste", "assign", "http", "log" ]
25+
2426
use_parking_lot = [ "parking_lot" ]
2527

2628
[dev-dependencies]
@@ -29,3 +31,4 @@ env_logger = "0.8"
2931

3032
[[example]]
3133
name = "echo_bot"
34+
required-features = [ "client" ]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ Rust implementation of [the Harmony chat protocol](https://github.com/harmony-de
2121

2222
## Crate features
2323
- Enable the `use_parking_lot` feature if you want to use [parking_lot](https://github.com/Amanieu/parking_lot) `sync` types instead of `std::sync`.
24+
- Enable the `client` feature for a lightweight client implementation and the client API. Enabled by default.

examples/echo_bot.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,9 @@ async fn main() -> ClientResult<()> {
2323
log::info!("Successfully created client.");
2424

2525
// We try to login, if it fails we register (which also authenticates)
26-
if let Err(_) = client.login(EMAIL.to_string(), PASSWORD.to_string()).await {
26+
if let Err(_) = client.login(EMAIL, PASSWORD).await {
2727
log::info!("Login failed, let's try registering.");
28-
client
29-
.register(
30-
EMAIL.to_string(),
31-
USERNAME.to_string(),
32-
PASSWORD.to_string(),
33-
)
34-
.await?;
28+
client.register(EMAIL, USERNAME, PASSWORD).await?;
3529
log::info!("Successfully registered.");
3630
} else {
3731
log::info!("Successfully logon.");

src/client/api/core.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ client_api! {
221221
args {
222222
guild_id: u64 => guild_id: (|g| g);
223223
channel_id: u64 => channel_id: (|g| g);
224-
previous_channel_id: u64 => previous_id: (|p| p);
225-
next_channel_id: u64 => next_id: (|n| n);
224+
previous_channel_id: Option<u64> => previous_id: Option::unwrap_or_default;
225+
next_channel_id: Option<u64> => next_id: Option::unwrap_or_default;
226226
}
227227
}
228228

@@ -454,7 +454,7 @@ client_api! {
454454
guild_id: u64 => guild_id: (|g| g);
455455
channel_id: u64 => channel_id: (|g| g);
456456
role_id: u64 => role_id: (|g| g);
457-
permissions: Option<PermissionList> => perms: (|p| p);
457+
permissions: PermissionList => perms: Some;
458458
}
459459
}
460460

@@ -518,8 +518,8 @@ client_api_action! {
518518
args {
519519
guild_id: u64 => guild_id: (|g| g);
520520
role_id: u64 => role_id: (|g| g);
521-
before_role_id: u64 => before_id: (|g| g);
522-
after_role_id: u64 => after_id: (|g| g);
521+
before_role_id: Option<u64> => before_id: Option::unwrap_or_default;
522+
after_role_id: Option<u64> => after_id: Option::unwrap_or_default;
523523
}
524524
}
525525

src/client/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub use tonic::{transport::Error as TransportError, Status};
1+
pub use tonic::{transport::Error as TransportError, Code, Status};
22

33
/// Result type used by many `Client` methods.
44
pub type ClientResult<T> = Result<T, ClientError>;

src/client/mod.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
//!
1414
//! // Login:
1515
//! let session = client
16-
//! .login("[email protected]".to_string(), "password".to_string())
16+
//! .login("[email protected]", "password")
1717
//! .await?;
1818
//!
1919
//! // Or register:
2020
//! let session = client
21-
//! .register("[email protected]".to_string(), "example".to_string(), "password".to_string())
21+
//! .register("[email protected]", "example", "password")
2222
//! .await?;
2323
//!
2424
//! // You're now logged in / registered!
@@ -51,12 +51,16 @@
5151
//! You can also use the API methods in the [`api`] module
5252
//! (note that you *won't* be able to store a [`Session`] that you got from these APIs inside a [`Client`]):
5353
//! ```no_run
54-
//! use harmony_rust_sdk::client::{Client, Session, api::foundation::login};
54+
//! use harmony_rust_sdk::client::{Client, Session, api::core::create_guild};
5555
//!
5656
//! let work = async {
5757
//! let homeserver_url = "https://example.org".parse().unwrap();
5858
//! let client = Client::new(homeserver_url, None).await?;
59-
//! let session = login(&client, "[email protected]".to_string(), "password".to_string()).await?;
59+
//!
60+
//! // Auth here
61+
//!
62+
//! // Create a guild and get the guild_id from the response
63+
//! let created_guild_id = create_guild(&client, String::from("Example Guild"), None).await?.guild_id;
6064
//!
6165
//! // make more API calls
6266
//! # harmony_rust_sdk::client::ClientResult::Ok(())
@@ -195,8 +199,8 @@ impl Client {
195199
}
196200

197201
/// Send a [`api::foundation::login`] request to the server and store the returned session.
198-
pub async fn login(&self, email: String, password: String) -> ClientResult<()> {
199-
let session = api::foundation::login(self, email, password).await?;
202+
pub async fn login(&self, email: impl ToString, password: impl ToString) -> ClientResult<()> {
203+
let session = api::foundation::login(self, email.to_string(), password.to_string()).await?;
200204
*self.session_lock() = Some(session);
201205

202206
Ok(())
@@ -205,11 +209,17 @@ impl Client {
205209
/// Send a [`api::foundation::register`] request to the server and store the returned session.
206210
pub async fn register(
207211
&self,
208-
email: String,
209-
username: String,
210-
password: String,
212+
email: impl ToString,
213+
username: impl ToString,
214+
password: impl ToString,
211215
) -> ClientResult<()> {
212-
let session = api::foundation::register(self, email, username, password).await?;
216+
let session = api::foundation::register(
217+
self,
218+
email.to_string(),
219+
username.to_string(),
220+
password.to_string(),
221+
)
222+
.await?;
213223
*self.session_lock() = Some(session);
214224

215225
Ok(())

src/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,5 @@ pub mod api {
1313
}
1414
}
1515

16-
/// Exports of some deps used in this crate's public API.
17-
pub mod exports {
18-
pub use futures;
19-
pub use http;
20-
}
21-
16+
#[cfg(feature = "client")]
2217
pub mod client;

tests/all.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
use harmony_rust_sdk::{client::Client, exports::http::Uri};
1+
use harmony_rust_sdk::client::{Client, ClientResult};
2+
3+
const EMAIL: &str = "[email protected]";
4+
const USERNAME: &str = "example";
5+
const PASSWORD: &str = "123456789";
26

37
// Make sure legato is running on `http://127.0.0.1:2289` before running this test.
48
#[tokio::test]
5-
async fn all() {
9+
async fn all() -> ClientResult<()> {
610
env_logger::builder().is_test(true).init();
711

8-
let email = String::from("[email protected]");
9-
let username = String::from("example");
10-
let password = String::from("123456789");
11-
12-
let client = Client::new("http://127.0.0.1".parse::<Uri>().unwrap(), None)
13-
.await
14-
.unwrap();
15-
16-
let response = client.login(email.clone(), password.clone()).await;
12+
let client = Client::new("http://127.0.0.1".parse().unwrap(), None).await?;
1713

18-
log::info!("{:?}", response);
14+
if client.login(EMAIL, PASSWORD).await.is_err() {
15+
client.register(EMAIL, USERNAME, PASSWORD).await?;
16+
}
1917

20-
let response = client
21-
.register(email.clone(), username.clone(), password.clone())
22-
.await;
18+
// TODO: do more API calls
2319

24-
log::info!("{:?}", response);
20+
Ok(())
2521
}

0 commit comments

Comments
 (0)