Skip to content

Commit b18eba8

Browse files
committed
blossom: refactor BlossomClient and examples to use Url for server
Updated `BlossomClient` and related examples to replace `String` with `Url` for server URLs. Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent faf2eb7 commit b18eba8

File tree

6 files changed

+21
-27
lines changed

6 files changed

+21
-27
lines changed

crates/nostr-blossom/examples/delete.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use nostr_blossom::prelude::*;
88
struct Args {
99
/// The server URL to connect to
1010
#[arg(long)]
11-
server: String,
11+
server: Url,
1212

1313
/// The SHA256 hash of the blob to delete (in hex)
1414
#[arg(long)]
@@ -23,7 +23,7 @@ struct Args {
2323
async fn main() -> Result<()> {
2424
let args = Args::parse();
2525

26-
let client = BlossomClient::new(&args.server);
26+
let client = BlossomClient::new(args.server);
2727

2828
// Create signer keys using the given private key
2929
let keys = Keys::new(args.private_key);

crates/nostr-blossom/examples/download.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use nostr_blossom::prelude::*;
1010
struct Args {
1111
/// The server URL to connect to
1212
#[arg(long)]
13-
server: String,
13+
server: Url,
1414

1515
/// SHA256 hash of the blob to download
1616
#[arg(long)]
@@ -26,7 +26,7 @@ async fn main() -> Result<()> {
2626
let args = Args::parse();
2727

2828
// Initialize the client.
29-
let client = BlossomClient::new(&args.server);
29+
let client = BlossomClient::new(args.server);
3030

3131
// Parse the private key.
3232
let keypair = Keys::new(args.private_key);

crates/nostr-blossom/examples/integration_test.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ use std::path::PathBuf;
44

55
use clap::Parser;
66
use nostr::hashes::{sha256, Hash};
7-
use nostr::key::SecretKey;
8-
use nostr::Keys;
9-
use nostr_blossom::client::BlossomClient;
7+
use nostr::prelude::*;
8+
use nostr_blossom::prelude::*;
109

1110
/// Integration test for various Blossom operations: upload, check (HEAD), list, download, and delete.
1211
#[derive(Parser, Debug)]
1312
#[command(author, version, about = "Run several operations against a Blossom server for demonstration and testing", long_about = None)]
1413
struct Args {
1514
/// The Blossom server URL
1615
#[arg(long)]
17-
server: String,
16+
server: Url,
1817

1918
/// Optional file path to a blob to upload. If omitted, a small test blob is used.
2019
#[arg(long)]
@@ -31,7 +30,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
3130
let args = Args::parse();
3231

3332
// Create the Blossom client.
34-
let client = BlossomClient::new(&args.server);
33+
let client = BlossomClient::new(args.server);
3534
// Create signer keys from the provided private key.
3635
let keys = Keys::new(args.private_key);
3736

crates/nostr-blossom/examples/list.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use nostr_blossom::prelude::*;
77
struct Args {
88
/// The server URL to connect to
99
#[arg(long)]
10-
server: String,
10+
server: Url,
1111

1212
/// The public key to list blobs for
1313
#[arg(long)]
@@ -21,7 +21,7 @@ struct Args {
2121
#[tokio::main]
2222
async fn main() -> Result<()> {
2323
let args = Args::parse();
24-
let client = BlossomClient::new(&args.server);
24+
let client = BlossomClient::new(args.server);
2525

2626
// Check if a private key was provided and branch accordingly
2727
if let Some(private_key) = args.private_key {

crates/nostr-blossom/examples/upload.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use nostr_blossom::prelude::*;
1010
struct Args {
1111
/// The server URL to connect to
1212
#[arg(long)]
13-
server: String,
13+
server: Url,
1414

1515
/// Path to the file to upload
1616
#[arg(long)]
@@ -29,7 +29,7 @@ struct Args {
2929
async fn main() -> Result<()> {
3030
let args = Args::parse();
3131

32-
let client = BlossomClient::new(&args.server);
32+
let client = BlossomClient::new(args.server);
3333

3434
// Read file data from the specified file path.
3535
let data = fs::read(&args.file)?;

crates/nostr-blossom/src/client.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,15 @@ use crate::error::Error;
2525
/// <https://github.com/hzrd149/blossom>
2626
#[derive(Debug, Clone)]
2727
pub struct BlossomClient {
28-
base_url: String,
28+
base_url: Url,
2929
client: reqwest::Client,
3030
}
3131

3232
impl BlossomClient {
3333
/// Creates a new `BlossomClient` with the given base URL.
34-
pub fn new<T>(base_url: T) -> Self
35-
where
36-
T: Into<String>,
37-
{
34+
pub fn new(base_url: Url) -> Self {
3835
Self {
39-
base_url: base_url.into(),
36+
base_url,
4037
client: Self::build_client().unwrap(),
4138
}
4239
}
@@ -67,7 +64,7 @@ impl BlossomClient {
6764
let hash: Sha256Hash = Sha256Hash::hash(&data);
6865
let file_hashes: Vec<Sha256Hash> = vec![hash];
6966

70-
let mut request = self.client.put(&url).body(data);
67+
let mut request = self.client.put(url).body(data);
7168
let mut headers = HeaderMap::new();
7269

7370
if let Some(ct) = content_type {
@@ -130,16 +127,14 @@ impl BlossomClient {
130127
url.push_str(&format!("?{}", query_params.join("&")));
131128
}
132129

133-
let mut request = self.client.get(&url);
130+
let mut request = self.client.get(url);
134131
let mut headers = HeaderMap::new();
135132

136133
if let Some(signer) = signer {
137-
// TODO: change self.base_url type to Url
138-
let url = Url::parse(&self.base_url).unwrap();
139134
let default_auth = self.default_auth(
140135
BlossomAuthorizationVerb::List,
141136
"Blossom list authorization",
142-
BlossomAuthorizationScope::ServerUrl(url),
137+
BlossomAuthorizationScope::ServerUrl(self.base_url.clone()),
143138
);
144139
let final_auth = authorization_options
145140
.map(|opts| Self::update_authorization_fixture(&default_auth, opts))
@@ -175,7 +170,7 @@ impl BlossomClient {
175170
T: NostrSigner,
176171
{
177172
let url = format!("{}/{}", self.base_url, sha256);
178-
let mut request = self.client.get(&url);
173+
let mut request = self.client.get(url);
179174
let mut headers = HeaderMap::new();
180175

181176
if let Some(range_value) = range {
@@ -231,7 +226,7 @@ impl BlossomClient {
231226
{
232227
let url = format!("{}/{}", self.base_url, sha256);
233228

234-
let mut request = self.client.head(&url);
229+
let mut request = self.client.head(url);
235230

236231
if let Some(signer) = signer {
237232
let default_auth = self.default_auth(
@@ -288,7 +283,7 @@ impl BlossomClient {
288283
let auth_header = Self::build_auth_header(signer, &final_auth).await?;
289284
headers.insert(AUTHORIZATION, auth_header);
290285

291-
let response: Response = self.client.delete(&url).headers(headers).send().await?;
286+
let response: Response = self.client.delete(url).headers(headers).send().await?;
292287

293288
if response.status().is_success() {
294289
Ok(())

0 commit comments

Comments
 (0)