Skip to content

Commit

Permalink
Merge pull request #322 from penberg/open-remote-auth
Browse files Browse the repository at this point in the history
libsql/core: Add auth token parameter to open_remote() in V2
  • Loading branch information
LucioFranco authored Aug 25, 2023
2 parents feb93e8 + 8a191bd commit 9493502
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
7 changes: 6 additions & 1 deletion crates/core/examples/example_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ use libsql::v2::Database;
#[tokio::main]
async fn main() {
let db = if let Ok(url) = std::env::var("LIBSQL_HRANA_URL") {
Database::open_remote(url).unwrap()
let token = std::env::var("TURSO_AUTH_TOKEN").unwrap_or_else(|_| {
println!("TURSO_AUTH_TOKEN not set, using empty token...");
"".to_string()
});

Database::open_remote(url, token).unwrap()
} else {
Database::open_in_memory().unwrap()
};
Expand Down
12 changes: 10 additions & 2 deletions crates/core/src/v2/hrana.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod pipeline;
mod proto;

use hyper::header::AUTHORIZATION;
use pipeline::{
ClientMsg, Response, ServerMsg, StreamBatchReq, StreamExecuteReq, StreamRequest,
StreamResponse, StreamResponseError, StreamResponseOk,
Expand Down Expand Up @@ -56,15 +57,20 @@ impl InnerClient {
Self { inner }
}

async fn send(&self, url: String, _auth: String, body: String) -> Result<ServerMsg> {
async fn send(&self, url: String, auth: String, body: String) -> Result<ServerMsg> {
let req = hyper::Request::post(url)
.header(AUTHORIZATION, auth)
.body(hyper::Body::from(body))
.unwrap();

let res = self.inner.request(req).await.map_err(HranaError::from)?;

if res.status() != StatusCode::OK {
// TODO(lucio): Error branch!
let body = hyper::body::to_bytes(res.into_body())
.await
.map_err(HranaError::from)?;
let body = String::from_utf8(body.into()).unwrap();
return Err(HranaError::Api(body).into());
}

let body = hyper::body::to_bytes(res.into_body())
Expand All @@ -91,6 +97,8 @@ pub enum HranaError {
Json(#[from] serde_json::Error),
#[error("http error: `{0}`")]
Http(#[from] hyper::Error),
#[error("api error: `{0}`")]
Api(String),
}

impl Client {
Expand Down
19 changes: 13 additions & 6 deletions crates/core/src/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ enum DbType {
Memory,
File { path: String },
Sync { db: crate::Database },
Remote { url: String },
Remote { url: String, auth_token: String },
}

pub struct Database {
Expand All @@ -43,17 +43,24 @@ impl Database {
})
}

pub async fn open_with_sync(db_path: impl Into<String>, url: impl Into<String>, token: impl Into<String>) -> Result<Database> {
pub async fn open_with_sync(
db_path: impl Into<String>,
url: impl Into<String>,
token: impl Into<String>,
) -> Result<Database> {
let opts = crate::Opts::with_http_sync(url, token);
let db = crate::Database::open_with_opts(db_path, opts).await?;
Ok(Database {
db_type: DbType::Sync { db },
})
}

pub fn open_remote(url: impl Into<String>) -> Result<Self> {
pub fn open_remote(url: impl Into<String>, auth_token: impl Into<String>) -> Result<Self> {
Ok(Database {
db_type: DbType::Remote { url: url.into() },
db_type: DbType::Remote {
url: url.into(),
auth_token: auth_token.into(),
},
})
}

Expand Down Expand Up @@ -85,8 +92,8 @@ impl Database {
Ok(Connection { conn })
}

DbType::Remote { url } => {
let conn = Arc::new(hrana::Client::new(url, ""));
DbType::Remote { url, auth_token } => {
let conn = Arc::new(hrana::Client::new(url, auth_token));

Ok(Connection { conn })
}
Expand Down

0 comments on commit 9493502

Please sign in to comment.