Skip to content

Commit

Permalink
Merge pull request #38 from Nuhvi/dev
Browse files Browse the repository at this point in the history
Api inconsistency in resolve/relay_get
  • Loading branch information
Nuhvi authored Dec 5, 2023
2 parents e3e7aea + 5436295 commit dab59f0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
13 changes: 8 additions & 5 deletions pkarr/examples/async/relay-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async fn main() -> Result<()> {

let client = PkarrClient::new();
client
.relay_put(&Url::parse(relay1).unwrap(), signed_packet)
.relay_put(&Url::parse(relay1).unwrap(), &signed_packet)
.await?;

println!("Published {}", keypair.to_uri_string());
Expand All @@ -74,11 +74,14 @@ async fn main() -> Result<()> {

let instant = Instant::now();

let signed_packet = reader
if let Some(signed_packet) = reader
.relay_get(&Url::parse(relay2).unwrap(), keypair.public_key())
.await?;

println!("Resolved in {:?} \n{}", instant.elapsed(), signed_packet);
.await?
{
println!("Resolved in {:?} \n{}", instant.elapsed(), signed_packet);
} else {
println!("No record found for {}", keypair.to_uri_string());
}
}

Ok(())
Expand Down
12 changes: 8 additions & 4 deletions pkarr/examples/relay-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn main() -> Result<()> {
let signed_packet = SignedPacket::from_packet(&keypair, &packet)?;

let client = PkarrClient::new();
client.relay_put(&Url::parse(relay1).unwrap(), signed_packet)?;
client.relay_put(&Url::parse(relay1).unwrap(), &signed_packet)?;

println!("Published {}", keypair.to_uri_string());
}
Expand All @@ -71,9 +71,13 @@ fn main() -> Result<()> {

let instant = Instant::now();

let signed_packet = reader.relay_get(&Url::parse(relay2).unwrap(), keypair.public_key())?;

println!("Resolved in {:?} \n{}", instant.elapsed(), signed_packet);
if let Some(signed_packet) =
reader.relay_get(&Url::parse(relay2).unwrap(), keypair.public_key())?
{
println!("Resolved in {:?} \n{}", instant.elapsed(), signed_packet);
} else {
println!("No record found for {}", keypair.to_uri_string());
}
}

Ok(())
Expand Down
34 changes: 22 additions & 12 deletions pkarr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,43 +66,53 @@ impl PkarrClient {

#[cfg(all(feature = "relay", not(feature = "async")))]
/// Resolves a [SignedPacket](crate::SignedPacket) from a [relay](https://github.com/Nuhvi/pkarr/blob/main/design/relays.md).
pub fn relay_get(&self, url: &Url, public_key: PublicKey) -> Result<SignedPacket> {
pub fn relay_get(&self, url: &Url, public_key: PublicKey) -> Result<Option<SignedPacket>> {
let url = format_relay_url(url, &public_key);

let response = self.http_client.get(url).send()?;
if !response.status().is_success() {

if response.status().is_success() {
let bytes = response.bytes()?;
return Ok(Some(SignedPacket::from_relay_response(public_key, bytes)?));
} else if response.status() == reqwest::StatusCode::NOT_FOUND {
return Ok(None);
} else {
return Err(Error::RelayResponse(
response.url().clone(),
response.status(),
response.text()?,
));
}
let bytes = response.bytes()?;

SignedPacket::from_relay_response(public_key, bytes)
}

#[cfg(all(feature = "relay", feature = "async"))]
/// Resolves a [SignedPacket] from a [relay](https://github.com/Nuhvi/pkarr/blob/main/design/relays.md).
pub async fn relay_get(&self, url: &Url, public_key: PublicKey) -> Result<SignedPacket> {
pub async fn relay_get(
&self,
url: &Url,
public_key: PublicKey,
) -> Result<Option<SignedPacket>> {
let url = format_relay_url(url, &public_key);

let response = self.http_client.get(url).send().await?;
if !response.status().is_success() {

if response.status().is_success() {
let bytes = response.bytes().await?;
return Ok(Some(SignedPacket::from_relay_response(public_key, bytes)?));
} else if response.status() == reqwest::StatusCode::NOT_FOUND {
return Ok(None);
} else {
return Err(Error::RelayResponse(
response.url().clone(),
response.status(),
response.text().await?,
));
}
let bytes = response.bytes().await?;

SignedPacket::from_relay_response(public_key, bytes)
}

#[cfg(all(feature = "relay", not(feature = "async")))]
/// Publishes a [SignedPacket] through a [relay](https://github.com/Nuhvi/pkarr/blob/main/design/relays.md).
pub fn relay_put(&self, url: &Url, signed_packet: SignedPacket) -> Result<()> {
pub fn relay_put(&self, url: &Url, signed_packet: &SignedPacket) -> Result<()> {
let url = format_relay_url(url, signed_packet.public_key());

let response = self
Expand All @@ -124,7 +134,7 @@ impl PkarrClient {

#[cfg(all(feature = "relay", feature = "async"))]
/// Publishes a [SignedPacket] through a [relay](https://github.com/Nuhvi/pkarr/blob/main/design/relays.md).
pub async fn relay_put(&self, url: &Url, signed_packet: SignedPacket) -> Result<()> {
pub async fn relay_put(&self, url: &Url, signed_packet: &SignedPacket) -> Result<()> {
let url = format_relay_url(url, signed_packet.public_key());

let response = self
Expand Down

0 comments on commit dab59f0

Please sign in to comment.