Skip to content

Commit 4887753

Browse files
committed
libsql: sync use auth_token from struct
1 parent 305b42b commit 4887753

File tree

1 file changed

+7
-28
lines changed

1 file changed

+7
-28
lines changed

libsql/src/sync.rs

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl SyncContext {
3232
frame_no + 1
3333
);
3434
let max_frame_no = self
35-
.push_with_retry(uri, &self.auth_token, frame.to_vec(), self.max_retries)
35+
.push_with_retry(uri, frame.to_vec(), self.max_retries)
3636
.await?;
3737

3838
Ok(max_frame_no)
@@ -41,46 +41,25 @@ impl SyncContext {
4141
async fn push_with_retry(
4242
&self,
4343
uri: String,
44-
auth_token: &Option<String>,
4544
frame: Vec<u8>,
4645
max_retries: usize,
4746
) -> Result<u32> {
4847
let mut nr_retries = 0;
4948
loop {
50-
// TODO(lucio): add custom connector + tls support here
51-
let client = hyper::client::Client::builder().build_http::<hyper::Body>();
52-
53-
let mut req = http::Request::post(uri.clone());
54-
55-
match auth_token {
49+
let client = reqwest::Client::new();
50+
let mut builder = client.post(uri.to_owned());
51+
match &self.auth_token {
5652
Some(ref auth_token) => {
57-
let auth_header =
58-
http::HeaderValue::try_from(format!("Bearer {}", auth_token.to_owned()))
59-
.unwrap();
60-
61-
req.headers_mut()
62-
.expect("valid http request")
63-
.insert("Authorization", auth_header);
53+
builder = builder.header("Authorization", format!("Bearer {}", auth_token));
6454
}
6555
None => {}
6656
}
67-
68-
// TODO(lucio): convert this to use bytes to make this clone cheap, it should be
69-
// to possible use BytesMut when reading frames from the WAL and efficiently use Bytes
70-
// from that.
71-
let req = req.body(frame.clone().into()).expect("valid body");
72-
73-
let res = client.request(req).await.unwrap();
74-
75-
// TODO(lucio): only retry on server side errors
57+
let res = builder.body(frame.to_vec()).send().await.unwrap();
7658
if res.status().is_success() {
77-
let res_body = hyper::body::to_bytes(res.into_body()).await.unwrap();
78-
let resp = serde_json::from_slice::<serde_json::Value>(&res_body[..]).unwrap();
79-
59+
let resp = res.json::<serde_json::Value>().await.unwrap();
8060
let max_frame_no = resp.get("max_frame_no").unwrap().as_u64().unwrap();
8161
return Ok(max_frame_no as u32);
8262
}
83-
8463
if nr_retries > max_retries {
8564
return Err(crate::errors::Error::ConnectionFailed(format!(
8665
"Failed to push frame: {}",

0 commit comments

Comments
 (0)