Skip to content

Commit 2a82035

Browse files
committed
Add _raw, _string, and generic variants to async module for parity with sync
Bring the async module to feature parity with the sync module by adding missing endpoint variants that allow users to choose their response format: - client.rs: Add request_impl_string for raw string HTTP responses - http.rs: Add _string variants (global_info_string, global_info_with_resources_string, get_database_info_string, create_resource_string, read_resource_string, resource_history_string) and make global_info, global_info_with_resources, get_database_info generic over DeserializeOwned - sirix.rs: Add info_raw, info_string, info_with_resources_raw, info_with_resources_string methods - database.rs: Add info_raw, info_string methods - resource.rs: Add create_raw, create (returning Value), create_string, read_string, read_with_metadata_string, history_string methods https://claude.ai/code/session_01LgXiks4zqYLRVbkgmCNoGc
1 parent 4520f23 commit 2a82035

5 files changed

Lines changed: 518 additions & 12 deletions

File tree

src/asynchronous/client.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,51 @@ pub async fn request_impl<T: DeserializeOwned>(
9595
}
9696
}
9797

98+
pub async fn request_impl_string(
99+
channel: Sender<Message>,
100+
scheme: Scheme,
101+
authority: Authority,
102+
path_and_query: PathAndQuery,
103+
method: Method,
104+
headers: HeaderMap,
105+
body: Body,
106+
) -> SirixResult<SirixResponse<String>> {
107+
let uri = Uri::builder()
108+
.scheme(scheme)
109+
.authority(authority)
110+
.path_and_query(path_and_query)
111+
.build()
112+
.unwrap();
113+
// create request
114+
let mut request_builder = Request::builder().uri(uri).method(method);
115+
for header in headers {
116+
request_builder = request_builder.header(header.0.unwrap(), header.1);
117+
}
118+
let request = request_builder.body(body).unwrap();
119+
// create response channel
120+
let (tx, rx) = oneshot::channel::<ResultResponse>();
121+
// Perform request
122+
let _ = channel
123+
.send(Message {
124+
request: request,
125+
responder: tx,
126+
})
127+
.await;
128+
let response = rx.await.unwrap().unwrap();
129+
let status = response.status().clone();
130+
let headers = response.headers().clone();
131+
// Aggregate body
132+
let body = body::aggregate(response).await?;
133+
let mut buf: Vec<u8> = vec![];
134+
std::io::Read::read_to_end(&mut body.reader(), &mut buf).unwrap();
135+
136+
Ok(SirixResponse {
137+
headers: headers.to_owned(),
138+
status: status,
139+
body: String::from_utf8_lossy(&buf).into_owned(),
140+
})
141+
}
142+
98143
pub async fn request_impl_fire_no_response(
99144
channel: Sender<Message>,
100145
scheme: Scheme,

src/asynchronous/database.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
use super::super::info::TokenData;
44
use super::super::types::{DbInfo, DbType, Json, Xml};
55
use super::client::{Message, SirixResponse};
6-
use super::http::{create_database, delete_database, get_database_info};
6+
use super::http::{create_database, delete_database, get_database_info, get_database_info_string};
77
use super::resource::Resource;
88
use super::SirixResult;
99
use hyper::http::uri::{Authority, Scheme};
10+
use serde::de::DeserializeOwned;
1011
use tokio::sync::mpsc::Sender;
1112
use tokio::sync::watch::Receiver;
1213

@@ -30,6 +31,10 @@ pub struct Database<T> {
3031

3132
impl<T> Database<T> {
3233
pub async fn info(&self) -> SirixResult<SirixResponse<DbInfo>> {
34+
self.info_raw().await
35+
}
36+
37+
pub async fn info_raw<U: DeserializeOwned>(&self) -> SirixResult<SirixResponse<U>> {
3338
match self.auth_channel.clone() {
3439
Some(watcher) => {
3540
let token_data = watcher.borrow().as_ref().unwrap().clone();
@@ -56,6 +61,33 @@ impl<T> Database<T> {
5661
}
5762
}
5863

64+
pub async fn info_string(&self) -> SirixResult<SirixResponse<String>> {
65+
match self.auth_channel.clone() {
66+
Some(watcher) => {
67+
let token_data = watcher.borrow().as_ref().unwrap().clone();
68+
let token = token_data.token_type + " " + &token_data.access_token;
69+
get_database_info_string(
70+
self.scheme.clone(),
71+
self.authority.clone(),
72+
&self.db_name,
73+
Some(&token),
74+
self.channel.clone(),
75+
)
76+
.await
77+
}
78+
None => {
79+
get_database_info_string(
80+
self.scheme.clone(),
81+
self.authority.clone(),
82+
&self.db_name,
83+
None,
84+
self.channel.clone(),
85+
)
86+
.await
87+
}
88+
}
89+
}
90+
5991
pub async fn delete(&self) -> SirixResult<SirixResponse<()>> {
6092
match self.auth_channel.clone() {
6193
Some(watcher) => {

0 commit comments

Comments
 (0)