ClickHouse / ClickHouse/clickhouse-rs

Support configuring ClickHouse client puerly from URL

Open
#467 2 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Rust
Stars
559
Forks
172
Avg merge
1d 19h
Merged PRs (30d)
3

Description

### Use case

Create `Client` from a single all-in-one URL, most importantly containing user/password

### Describe the solution you'd like

Associated function on Client, something like `from_url` / `from_dsn`

Looks like a similar interface is available in JS client

Those options have no default place to read from URL, can be parsed from query params (but not sure we should)

- `with_access_token()` (also can be extracted from user/password pard of URL)
- `with_compression()`
- `with_header()`
- `with_product_info()`

### Describe the alternatives you've considered

Adding all parameters as separate CLI args to my program (user/password are what I 100% do need)

### Additional context

`url` crate that can help with implementation is already a dependency

Here is the code I'm using in my project. If the idea is OK, I'll prepare a PR with proper integration

```rust
pub fn client_from_url(url: String) -> eyre::Result {
let url = url::Url::parse(&url)?;

let (scheme, default_port) = match url.scheme() {
"http" => ("http", 8123),
"https" => ("https", 8443),
other => eyre::bail!("unsupported URL scheme `{other}://`, expected http or https"),
};
let host = if let Some(host) = url.host() {
format!("{host}")
} else {
"localhost".to_string()
};
let port = url.port().unwrap_or(default_port);

let mut client = Client::default().with_url(format!("{scheme}://{host}:{port}"));

let user = url.username();
if !user.is_empty() {
client = client.with_user(user);
}

if let Some(password) = url.password() {
client = client.with_password(password);
}

let path = url.path();
if path != "/" && !path.is_empty() {
let database = path.strip_prefix("/").unwrap_or(path);
client = client.with_database(database);
}

for (name, value) in url.query_pairs() {
client = client.with_setting(name, value);
}

Ok(client)
}
```

Contributor guide

Open the contributing guide

Research direction

Start with the Client API and the existing url crate dependency, then review the with_url, with_user, with_password, with_database, and with_setting methods used in the example. Define how supported HTTP(S) URLs, credentials, ports, database paths, and query parameters map to Client configuration, and add tests covering the accepted URL forms and errors for unsupported schemes.

Written by the indexing model from the issue text.

Assessment

Tech stack
clickhouse, rust
Domain
backend-api-design, databases
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.