feat(pubky): PubkyClient::builder().build() return result

This commit is contained in:
nazeh
2024-11-14 15:17:36 +03:00
parent 1b7d83a315
commit af390e8087
7 changed files with 56 additions and 59 deletions

61
Cargo.lock generated
View File

@@ -802,21 +802,6 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "futures"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.31"
@@ -824,7 +809,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
@@ -834,22 +818,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
[[package]]
name = "futures-executor"
version = "0.3.31"
name = "futures-lite"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f"
checksum = "cef40d21ae2c515b51041df9ed313ed21e572df340ea58a922a0aefe7e8891a1"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
"pin-project-lite",
]
[[package]]
name = "futures-io"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
[[package]]
name = "futures-macro"
version = "0.3.31"
@@ -879,18 +856,30 @@ version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
"pin-project-lite",
"pin-utils",
"slab",
]
[[package]]
name = "genawaiter"
version = "0.99.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c86bd0361bcbde39b13475e6e36cb24c329964aa2611be285289d1e4b751c1a0"
dependencies = [
"futures-core",
"genawaiter-macro",
]
[[package]]
name = "genawaiter-macro"
version = "0.99.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b32dfe1fdfc0bbde1f22a5da25355514b5e450c33a6af6770884c8750aedfbc"
[[package]]
name = "generic-array"
version = "0.14.7"
@@ -1715,17 +1704,21 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkarr"
version = "3.0.0"
source = "git+https://github.com/Pubky/pkarr?branch=v3#ab2fb43c49185c3054f988fd5c548d774ce4f37e"
source = "git+https://github.com/Pubky/pkarr?branch=v3#6a6c40bf7a6b0957e3222bd193ca4ed858c72516"
dependencies = [
"base32",
"byteorder",
"bytes",
"document-features",
"dyn-clone",
"ed25519-dalek",
"flume",
"futures",
"futures-lite",
"genawaiter",
"getrandom",
"heed",
"js-sys",
"libc",
"lru",
"mainline",
"once_cell",
@@ -1737,7 +1730,9 @@ dependencies = [
"sha1_smol",
"simple-dns",
"thiserror 1.0.69",
"tokio",
"tracing",
"wasm-bindgen-futures",
]
[[package]]

View File

@@ -24,7 +24,7 @@ async fn main() -> Result<()> {
let homeserver = cli.homeserver;
let client = PubkyClient::builder().build();
let client = PubkyClient::new()?;
println!("Enter your recovery_file's passphrase to signup:");
let passphrase = rpassword::read_password()?;

View File

@@ -66,7 +66,7 @@ async fn main() -> Result<()> {
println!("PublicKey: {}", keypair.public_key());
let client = if cli.testnet.unwrap_or_default() {
let client = PubkyClient::testnet();
let client = PubkyClient::testnet()?;
// For the purposes of this demo, we need to make sure
// the user has an account on the local homeserver.
@@ -78,7 +78,7 @@ async fn main() -> Result<()> {
client
} else {
PubkyClient::builder().build()
PubkyClient::new()?
};
println!("Sending AuthToken to the 3rd party app...");

View File

@@ -18,14 +18,14 @@ struct Cli {
async fn main() -> Result<()> {
let cli = Cli::parse();
let client = PubkyClient::builder().build();
let client = PubkyClient::new()?;
match cli.url.scheme() {
"https" => {
unimplemented!();
}
"pubky" => {
let response = client.get(cli.url).await.unwrap();
let response = client.get(cli.url).await?;
println!("Got a response: \n {:?}", response);
}

View File

@@ -19,7 +19,7 @@ async fn main () {
let client = PubkyClient::test(&testnet);
// Uncomment the following line instead if you are not just testing.
// let client PubkyClient::builder().build();
// let client PubkyClient::new().unwrap();
// Generate a keypair
let keypair = Keypair::random();

View File

@@ -43,12 +43,12 @@ impl Settings {
}
/// Build [PubkyClient]
pub fn build(self) -> PubkyClient {
pub fn build(self) -> Result<PubkyClient, std::io::Error> {
// TODO: convert to Result<PubkyClient>
let pkarr = pkarr::Client::new(self.pkarr_settings).unwrap();
let pkarr = pkarr::Client::new(self.pkarr_settings)?;
PubkyClient {
Ok(PubkyClient {
http: reqwest::Client::builder()
.cookie_store(true)
// .dns_resolver(Arc::new(dns_resolver))
@@ -56,17 +56,16 @@ impl Settings {
.build()
.unwrap(),
pkarr,
}
}
}
impl Default for PubkyClient {
fn default() -> Self {
PubkyClient::builder().build()
})
}
}
impl PubkyClient {
/// Create a new [PubkyClient] with default [Settings]
pub fn new() -> Result<Self, std::io::Error> {
Self::builder().build()
}
/// Returns a builder to edit settings before creating [PubkyClient].
pub fn builder() -> Settings {
Settings::default()
@@ -74,15 +73,18 @@ impl PubkyClient {
/// Create a client connected to the local network
/// with the bootstrapping node: `localhost:6881`
pub fn testnet() -> Self {
Self::test(&Testnet {
bootstrap: vec!["localhost:6881".to_string()],
nodes: vec![],
})
pub fn testnet() -> Result<Self, std::io::Error> {
Self::builder()
.testnet(&Testnet {
bootstrap: vec!["localhost:6881".to_string()],
nodes: vec![],
})
.build()
}
/// Alias to `PubkyClient::builder().testnet(testnet).build()`
pub fn test(testnet: &Testnet) -> PubkyClient {
PubkyClient::builder().testnet(testnet).build()
#[cfg(test)]
/// Alias to `PubkyClient::builder().testnet(testnet).build().unwrap()`
pub(crate) fn test(testnet: &Testnet) -> PubkyClient {
PubkyClient::builder().testnet(testnet).build().unwrap()
}
}

View File

@@ -33,7 +33,7 @@ mod tests {
let homeserver = Homeserver::start_test(&testnet).await.unwrap();
let client = PubkyClient::builder().testnet(&testnet).build();
let client = PubkyClient::test(&testnet);
let url = format!("http://{}/", homeserver.public_key());
@@ -50,7 +50,7 @@ mod tests {
async fn http_get_icann() {
let testnet = Testnet::new(10).unwrap();
let client = PubkyClient::builder().testnet(&testnet).build();
let client = PubkyClient::test(&testnet);
let url = format!("http://example.com/");