Skip to content

Commit

Permalink
feat(pubky): PubkyClient::builder().build() return result
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Nov 14, 2024
1 parent 1b7d83a commit af390e8
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 58 deletions.
61 changes: 28 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/authn/signup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down
4 changes: 2 additions & 2 deletions examples/authz/authenticator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -78,7 +78,7 @@ async fn main() -> Result<()> {

client
} else {
PubkyClient::builder().build()
PubkyClient::new()?
};

println!("Sending AuthToken to the 3rd party app...");
Expand Down
4 changes: 2 additions & 2 deletions examples/request/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion pubky/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
36 changes: 19 additions & 17 deletions pubky/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,46 +43,48 @@ 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))
.user_agent(DEFAULT_USER_AGENT)
.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()
}
}

impl PubkyClient {
/// Returns a builder to edit settings before creating [PubkyClient].
pub fn builder() -> Settings {
Settings::default()
}

/// 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()
}
}
4 changes: 2 additions & 2 deletions pubky/src/native/api/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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/");

Expand Down

0 comments on commit af390e8

Please sign in to comment.