Skip to content

Commit e73eb56

Browse files
committed
Separate builder.rs from lib.rs
1 parent a36ef35 commit e73eb56

File tree

3 files changed

+54
-40
lines changed

3 files changed

+54
-40
lines changed

examples/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use postgrest::PostgrestClient;
1+
use postgrest::Postgrest;
22

33
#[tokio::main]
44
async fn main() -> Result<(), Box<dyn std::error::Error>> {
5-
let client = PostgrestClient::new("https://hacks.soedirgo.dev/postgrest");
5+
let client = Postgrest::new("https://hacks.soedirgo.dev/postgrest");
66
let resp = client
77
.from("todos")
88
.select("*")
99
.execute()
1010
.await?;
11-
println!("{}", resp);
11+
println!("{}", resp.text().await?);
1212
Ok(())
1313
}

src/builder.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use reqwest::{Client, Error, Method, Response};
2+
3+
pub struct Builder {
4+
method: Option<Method>,
5+
url: String,
6+
queries: Vec<(String, String)>,
7+
headers: Vec<(String, String)>,
8+
}
9+
10+
impl Builder {
11+
pub fn new(url: &str) -> Self {
12+
Builder {
13+
method: None,
14+
url: url.to_string(),
15+
queries: Vec::new(),
16+
headers: Vec::new(),
17+
}
18+
}
19+
20+
pub fn select(mut self, column: &str) -> Self {
21+
self.method = Some(Method::GET);
22+
self.queries.push(("select".to_string(), column.to_string()));
23+
self
24+
}
25+
26+
pub async fn execute(self) -> Result<Response, Error> {
27+
let mut req = Client::new().request(
28+
self.method.unwrap(),
29+
&self.url,
30+
);
31+
for (k, v) in &self.headers {
32+
req = req.header(k, v);
33+
}
34+
req = req.query(&self.queries);
35+
36+
let resp = req.send()
37+
.await?;
38+
39+
Ok(resp)
40+
}
41+
}

src/lib.rs

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,21 @@
1-
use reqwest::Method;
1+
use builder::Builder;
22

3-
pub struct Request {
4-
method: Option<Method>,
5-
url: String,
6-
}
7-
8-
impl Request {
9-
pub fn new(url: &str) -> Request {
10-
Request {
11-
method: None,
12-
url: url.to_owned(),
13-
}
14-
}
15-
16-
pub fn select(mut self, column_query: &str) -> Request {
17-
self.method = Some(Method::GET);
18-
self.url.push_str(&format!("?select={}", column_query));
19-
self
20-
}
21-
22-
pub async fn execute(self) -> Result<String, Box<dyn std::error::Error>> {
23-
let resp = reqwest::get(&self.url)
24-
.await?
25-
.text()
26-
.await?;
27-
Ok(resp)
28-
}
29-
}
3+
mod builder;
304

31-
pub struct PostgrestClient {
5+
pub struct Postgrest {
326
rest_url: String,
337
}
348

35-
impl PostgrestClient {
36-
pub fn new(rest_url: &str) -> PostgrestClient {
37-
PostgrestClient {
38-
rest_url: rest_url.to_owned(),
9+
impl Postgrest {
10+
pub fn new(rest_url: &str) -> Postgrest {
11+
Postgrest {
12+
rest_url: rest_url.to_string(),
3913
}
4014
}
4115

42-
pub fn from(&self, table: &str) -> Request {
16+
pub fn from(&self, table: &str) -> Builder {
4317
let mut url = self.rest_url.clone();
44-
url.push('/');
45-
url.push_str(table);
46-
Request::new(&url)
18+
url = format!("{}/{}", url, table);
19+
Builder::new(&url)
4720
}
4821
}

0 commit comments

Comments
 (0)